Old Russian measure of length

From Rosetta Code
Revision as of 07:02, 12 February 2013 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: rewrote the REXX program. -- ~~~~)
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 so results are more meaningful and recognizable
  • does error checking on the user input
  • added other old Russian units of measurements
  • 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 200
 /*──translation──*/
 /*tip, top*/                vershok  = 22.492971          /*1.75 inch.*/
 /*palm, quarter*/           piad     = vershok    / 4     /*≡chetvert.*/
 /*yard*/                    arshin   = vershok    / 16
 /*fathom*/                  sazhen   = arshin     / 3
 /*turn (of a plow)*/        verst    = sazhen     / 500   /*≡versta.  */
 /*mile*/                    milia    = verst      / 1.5
 /*inch*/                    diuym    = arshin     * 28
 /*foot*/                    fut      = diuym      / 12
 /*line*/                    liniya   = diuym      * 10
 /*point*/                   tochka   = diuym      * 100

KM=1000; CM=100 /*define a couple of multipliers.*/ 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 err '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.*/ whatU=what; upper whatU /*an uppercase version for ABBREV*/

                  select              /*convert the length ───► meters.*/
                  when abbrev('METRES'     ,whatU   ) |,
                       abbrev('METERS'     ,whatU   )    then m=N
                  when abbrev('KILOMETRES' ,whatU,2 ) |,
                       abbrev('KILOMETERS' ,whatU,2 ) |,
                       abbrev('KMS'        ,whatU,  )    then m=N*KM
                  when abbrev('CENTIMETRES',whatU,2 ) |,
                       abbrev('CENTIMETERS',whatU,2 ) |,
                       abbrev('CMS'        ,whatU,2 )    then m=N/CM
                  when abbrev('ARSHINS'    ,whatU   )    then m=N/arshin
                  when abbrev('DIUYM'      ,whatU   )    then m=N/diuym
                  when abbrev('FUT'        ,whatU   )    then m=N/fut
                  when abbrev('LINIYA'     ,whatU   )    then m=N/liniya
                  when abbrev('PIADS'      ,whatU   ) |,
                       abbrev('CHETVERTS'  ,whatU,2 )    then m=N/piad
                  when abbrev('SAZHENS'    ,whatU   )    then m=N/sazhen
                  when abbrev('TOCHKA'     ,whatU   )    then m=N/tochka
                  when abbrev('VERSHOKS'   ,whatU,5 )    then m=N/vershok
                  when abbrev('VERSTAS'    ,whatU,5 ) |,
                       abbrev('VERSTS'     ,whatU,2 )    then m=N/verst
                  when abbrev('MILIA'      ,whatU,2 )    then m=N/milia
                  otherwise    call err 'invalid measure name: ' what
                  end   /*select*/
                                         say centre('metric',79,"─")

call saym m/KM , 'kilometer' call saym m , 'meter' call saym m*CM , 'centimeter'

                                         say centre('old Russian',79,"─")

call saym m*milia , 'milia' call saym m*verst , 'verst' call saym m*sazhen , 'sazhen' call saym m*arshin , 'arshin' call saym m*fut , 'fut' call saym m*piad , 'piad' call saym m*vershok , 'vershok' call saym m*diuym , 'diuym' call saym m*liniya , 'liniya' call saym m*tochka , 'tochka' 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

/*───────────────────────────────S subroutine───────────────────────────*/ s: if arg(1)=1 then return arg(3); return word(arg(2) 's',1) /*plural*/ /*───────────────────────────────SAYM subroutine────────────────────────*/ saym: parse arg _; numeric digits digits()%25; _=_/1

      say right(_,40) arg(2)s(_);         return</lang>

output when using the input of: 100 metres

────────────────────────────────────metric─────────────────────────────────────
                                     0.1 kilometers
                                     100 meters
                                   10000 centimeters
──────────────────────────────────old Russian──────────────────────────────────
                             0.062480475 milias
                             0.093720713 versts
                               46.860356 sazhens
                               140.58107 arshins
                               328.02249 futs
                               562.32428 piads
                               2249.2971 vershoks
                               3936.2699 diuyms
                               39362.699 liniyas
                               393626.99 tochkas

output when using the input of: 1.4058107 arshins

────────────────────────────────────metric─────────────────────────────────────
                                   0.001 kilometers
                                       1 meter
                                     100 centimeters
──────────────────────────────────old Russian──────────────────────────────────
                           0.00062480476 milias
                           0.00093720713 versts
                              0.46860357 sazhens
                               1.4058107 arshins
                                3.280225 futs
                               5.6232428 piads
                               22.492971 vershoks
                                 39.3627 diuyms
                                 393.627 liniyas
                                 3936.27 tochkas

output when using the input of: -46.860366 sazhens

────────────────────────────────────metric─────────────────────────────────────
                             -0.10000002 kilometers
                              -100.00002 meters
                              -10000.002 centimeters
──────────────────────────────────old Russian──────────────────────────────────
                            -0.062480488 milias
                            -0.093720732 versts
                              -46.860366 sazhens
                               -140.5811 arshins
                              -328.02256 futs
                              -562.32439 piads
                              -2249.2976 vershoks
                              -3936.2707 diuyms
                              -39362.707 liniyas
                              -393627.07 tochkas