UPC: Difference between revisions

419 bytes added ,  3 years ago
m
→‎{{header|REXX}}: added/changed whitespace and comments, checked for errors better, split some compound statements, spelled out some error conditions.
m (→‎{{header|REXX}}: added/changed whitespace and comments, checked for errors better, split some compound statements, spelled out some error conditions.)
Line 2,084:
#.7= ' ### ##'
#.8= ' ## ###' /* [↓] right─sided UPC digits.*/
#.9= ' # ##' ; do i=0 for 10; ##.i= translate(#.i, ' #', "# ")
end /*i*/
say center('UPC', 14, "─") ' ___---'copies(1234567, 6)"-----"copies(1234567, 6)'___---'
@.=.
@.1 = ' # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # '
Line 2,098:
@.9 = ' # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # '
@.10= ' # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # '
ends= '# #' if k//2 then sum= sum + 3 * d /*mult.define byENDS literal 3.const*/
do j=1 while @.j\==.; $= @.j
txt= else sum= sum + d /*initialize " "TXT 1.variable*/
if left($, 39)\=='# #' | right($, 39)\=="#'' #" then txt= 'bad fenceblanks'
$= substrstrip($,); 4, L -$$= 3$ - 3); sum= 0 /*elide blanks at ends. of $*/
$L= delstr($, length($) % 2 - 1, 5) /*obtain length "of $ middle.string*/
if left($, 3) \==ends | right($, 3) \==ends then txt= 'bad fence'
if L \== 95 & txt=='' then txt= 'bad lenlength'
$= substr($, 4, L - length(ends)*2) /*elide "ends". */
$= delstr($, length($) end% 2 - /*j*/1, 5) /*stick a fork in it,/* we're" all done middle. */</lang>
sum= 0 /*initialize SUM*/
if txt=='' then do k=1 for 12; parse var $ x +7 $ /*get UPC digdigit.*/
do d=0 for 10; if x==#.d | x==##.d then leave /*valid digit? */
end /*kd*/
if d==10 & k \==12 then do; txt= 'reversed' ; leave; end
if d==10 then do; txt= 'bad digdigit'; leave; end
if sumk //10 \== 02 then txtsum= 'badsum chksum'+ d * 3 /*mult. by 3. /*invalid sum?*/
else sum= sum + d /* " " 1. */
txt= txt || d
end end /*dk*/
 
if do j=left(txt,1 while @.j)\==.;"b" then if sum//10\==0 then $txt= @.j;'bad checksum' /*invalid sum? txt=*/
say center( if leftstrip($txt), 915)\= ' ' $$ | right($, 9)\='' /*show thenchksum txt=(or 'baderr blanks'msg) with the UPC*/
end /*j*/ /*stick a fork in it, we're all done. */</lang>
$= strip($); $$= $; L= length($)
if left($, 3)\=='# #' | right($, 3)\=="# #" then txt= 'bad fence'
if L\==95 & txt=='' then txt= 'bad len'
$= substr($, 4, L - 3 - 3); sum= 0 /*elide ends.*/
$= delstr($, length($) % 2 - 1, 5) /* " middle.*/
if txt=='' then do k=1 for 12; parse var $ x +7 $ /*get UPC dig.*/
do d=0 for 10; if x==#.d | x==##.d then leave /*valid digit?*/
end /*d*/
if d==10 & k\==12 then do; txt= 'reversed'; leave; end
if d==10 then do; txt= 'bad dig'; leave; end
if k//2 then sum= sum + 3 * d /*mult. by 3.*/
else sum= sum + d /* " " 1.*/
txt= txt || d
end /*k*/
if sum//10 \== 0 then txt= 'bad chksum' /*invalid sum?*/
say center( strip(txt), 15) ' ' $$
end /*j*/ /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
─────UPC────── ---123456712345671234567123456712345671234567-----123456712345671234567123456712345671234567---
─────UPC────── ___123456712345671234567123456712345671234567-----123456712345671234567123456712345671234567___
924773271019 # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # #
403944441050 # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # #
reversed # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # #
reversed # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # #
bad dig digit # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # #
reversed # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # #
214575875608 # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # #
reversed # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # #
706466743030 # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # #
bad len length # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### #
</pre>