Old Russian measure of length: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: changed wording one of the comments in the section header. -- ~~~~)
m (→‎{{header|REXX}}: added a comment about columnarized output instead of horizontal stream.)
Line 26: Line 26:
* does error checking on the user input
* does error checking on the user input
* uses the correct length unit names when not plural
* uses the correct length unit names when not plural
* columnarized the output   (instead of a horizontal stream).
<lang rexx>/*REXX program to read a config file and assign VARs as found within. */
<lang rexx>/*REXX program to read a config file and assign VARs as found within. */
numeric digits 25
numeric digits 25

Revision as of 00:51, 12 February 2013

Old Russian measure of length is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The program should make the conversion of the old Russian measures of length in the metric and vice versa.

Is an example of the transformation of several variables that are linearly. The program accepts a single value in the selected unit, and return it in the rest: vershoks, arshins, sazhens, versts, meters, centimeters and kilometers.

МК-61/52

<lang>П7 1 0 0 * П8 1 ВП 5 / П9 ИП7 1 0 6 7 / П0 5 0 0 * ПC 3 * ПA 1 6 * ПB С/П ПB 1 6 / ПA 3 / ПC 5 0 0 / П0 1 0 6 7 / БП 00</lang>

Instruction: l, meters = РX В/О С/П; l, vershoks = БП 3 1 С/П; l, arshins = РX БП 3 5 С/П; l, sazhens = РX БП 3 8 С/П; l, versts = РX БП 4 3 С/П; РX = РB = l, vershoks; РA = l, arshins; РB = l, versts; РC = l, sazhens; Р7 = l, meters; Р8 = l, centimeters; Р9 = l, kilometers.

Example: 100 m = 2249,2971 vershoks = 140,58107 arshins = 46,860366 sazhens = 0,093790712 versts; 3 vershoks = 13,3375 cm; 2 sazhens = 96 vershoks = 6 arshins = 4,268 m; 1 verst = 1,067 km.

REXX

Program features:

  • shows all other units of measurements when any unit is specified.
  • accepts abbreviations of the length units
  • does rounding for the two arithmetic operations performed
  • does error checking on the user input
  • uses the correct length unit names when not plural
  • columnarized the output   (instead of a horizontal stream).

<lang rexx>/*REXX program to read a config file and assign VARs as found within. */ numeric digits 25

                                      vershoks = 2249.2971       / 100
                                      arshins  =  140.58107      / 100
                                      sazhens  =   46.860366     / 100
                                      versts   =    0.093790712  / 100

parse arg N what _ . /*obtain the user's input from CL*/ if N == then call err 'no arguments specified.' if \datatype(N,'N') then call 'units not numeric: ' N if _\== then call err 'too many arguments specified.' n=n/1 /*normalize it (004──►4 7.──►7)*/ if what== then what='meters' /*None specified? Assume meters.*/ parse upper what whatU /*an uppercase version for ABBREV*/

                     select           /*convert the length ───► meters.*/
                     when abbrev('METERS'  ,whatU  )  then m=N
                     when abbrev('VERSHOKS',whatU,2)  then m=N / vershoks
                     when abbrev('ARSHINS' ,whatU  )  then m=N / arshins
                     when abbrev('SAZHENS' ,whatU  )  then m=N / sazhens
                     when abbrev('VERSTS'  ,whatU,2)  then m=N / versts
                     otherwise     call err 'invalid measure name: ' what
                     end   /*select*/

say n what ' is:'

                 ;  if          m\=N  then say right(m,40) 'meter's(m)

x = m * vershoks  ; if rounder(x)\=N then say right(x,40) 'vershok's(x) x = m * arshins  ; if rounder(x)\=N then say right(x,40) 'arshin's(x) x = m * sazhens  ; if rounder(x)\=N then say right(x,40) 'sazhen's(x) x = m * versts  ; if rounder(x)\=N then say right(x,40) 'verst's(x) exit /*stick a fork in it, we're done.*/ /*───────────────────────────────ERR subroutine─────────────────────────*/ err: say; say; say center(' error! ', max(40, linesize()%2), "*"); say

               do j=1  for arg();  say arg(j);  say;  end;  say;  exit 13

/*───────────────────────────────ROUNDER subroutine─────────────────────*/ rounder: procedure; parse arg x; _=pos('.',x); if _==0 then return x parse arg '.' f; return format(x,,length(f)-2) /*handle rounding.*/ /*───────────────────────────────S subroutine───────────────────────────*/ s: if arg(1)=1 then return arg(3); return word(arg(2) 's',1) /*plural*/</lang> output when using the input of: 100 meter

100 meter  is:
                             2249.297100 vershoks
                             140.5810700 arshins
                             46.86036600 sazhens
                           0.09379071200 versts

output when using the input of: 1.4058107 arshins

1.4058107 arshins  is:
                                       1 meter
                               22.492971 vershoks
                               1.4058107 arshins
                              0.46860366 sazhens
                           0.00093790712 versts

output when using the input of: -46.860366 sazhens

-46.860366 sazhens  is:
                                    -100 meters
                              -2249.2971 vershoks
                              -140.58107 arshins
                              -46.860366 sazhens
                            -0.093790712 versts