Old Russian measure of length
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.
D
<lang d>import std.stdio, std.string, std.algorithm, std.conv;
void main(string[] a) {
if (a.length == 3 && isNumeric(a[1])) { double[string] factor = [ "tochka" : 0.000254, "liniya" : 0.00254, "diuym" : 0.0254, "vershok" : 0.04445, "piad" : 0.1778, "fut" : 0.3048, "arshin" : 0.7112, "sazhen" : 2.1336, "versta" : 1066.8, "milia" : 7467.6, "centimeter" : 0.01, "meter" : 1.0, "kilometer" : 1000.0, ]; if (auto p = a[2] in factor) { immutable magnitude = to!double(a[1]); immutable meters = magnitude * (*p); writefln("%s %s to: \n", a[1], a[2]); auto keys = factor.keys; schwartzSort!(k => factor[k], "a < b")(keys); foreach (key; keys) writefln("%10s: %s", key, meters / factor[key]); return; } } writeln("Please provide a number and unit");
}</lang>
- Output:
1 meter to: tochka: 3937.01 liniya: 393.701 centimeter: 100 diuym: 39.3701 vershok: 22.4972 piad: 5.6243 fut: 3.28084 arshin: 1.40607 meter: 1 sazhen: 0.468691 kilometer: 0.001 versta: 0.000937382 milia: 0.000133911 1 milia to: tochka: 2.94e+07 liniya: 2.94e+06 centimeter: 746760 diuym: 294000 vershok: 168000 piad: 42000 fut: 24500 arshin: 10500 meter: 7467.6 sazhen: 3500 kilometer: 7.4676 versta: 7 milia: 1
Perl 6
Fairly straightfoward. Define a hash of conversion factors then apply them. Does some basic error trapping. Makes no attempt to do correct pluralization because I have no idea what the correct plurals are and little interest in researching them. Conversion factors from Wikipedia: Obsolete Russian units of measurement.
<lang perl6>convert(1, 'meter');
say '*' x 40, "\n";
convert(1, 'milia');
sub convert (Real $magnitude, $unit) {
my %factor = ( tochka => 0.000254, liniya => 0.00254, diuym => 0.0254, vershok => 0.04445, piad => 0.1778, fut => 0.3048, arshin => 0.7112, sazhen => 2.1336, versta => 1066.8, milia => 7467.6, centimeter => 0.01, meter => 1.0, kilometer => 1000.0, );
die "Unknown unit $unit\n" unless %factor.exists($unit.lc);
my $meters = $magnitude * %factor.delete($unit.lc);
say "$magnitude $unit to:\n", '_' x 40;
printf "%10s: %s\n", $_, $meters / %factor{$_} for %factor.keys.sort:{ +%factor{$^_} }
} </lang>
1 meter to: ________________________________________ tochka: 3937.007874 liniya: 393.700787 centimeter: 100 diuym: 39.370079 vershok: 22.497188 piad: 5.624297 fut: 3.280840 arshin: 1.406074 sazhen: 0.468691 kilometer: 0.001 versta: 0.000937 milia: 0.000134 **************************************** 1 milia to: ________________________________________ tochka: 29400000 liniya: 2940000 centimeter: 746760 diuym: 294000 vershok: 168000 piad: 42000 fut: 24500 arshin: 10500 meter: 7467.6 sazhen: 3500 kilometer: 7.4676 versta: 7
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