Temperature conversion: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (added Arturo) |
m (→{{header|REXX}}: added/changed whitespace and comments, used a template for the output sections.) |
||
Line 3,335: | Line 3,335: | ||
do until tList='' /*process the list of temperatures. */ |
do until tList='' /*process the list of temperatures. */ |
||
parse var tList x ',' tList /*temps are separated by commas. */ |
parse var tList x ',' tList /*temps are separated by commas. */ |
||
x=translate(x,'((',"[{") |
x= translate(x, '((', "[{") /*support other grouping symbols. */ |
||
x=space(x); parse var x z '(' |
x= space(x); parse var x z '(' /*handle any comments (if any). */ |
||
parse upper var z z ' TO ' ! . /*separate the TO option from number.*/ |
parse upper var z z ' TO ' ! . /*separate the TO option from number.*/ |
||
if !=='' then !='ALL'; all=!=='ALL' |
if !=='' then != 'ALL'; all= !=='ALL' /*allow specification of "TO" opt*/ |
||
if z=='' then call serr "no arguments were specified." /*oops-ay. */ |
if z=='' then call serr "no arguments were specified." /*oops-ay. */ |
||
_=verify(z, '+-.0123456789') |
_= verify(z, '+-.0123456789') /*list of valid numeral/number thingys.*/ |
||
n=z |
n= z |
||
if _\==0 then do |
if _\==0 then do |
||
if _==1 then call serr 'illegal temperature:' z |
if _==1 then call serr 'illegal temperature:' z |
||
n=left(z, _-1) |
n= left(z, _ - 1) /*pick off the number (hopefully). */ |
||
u=strip(substr(z, _ |
u= strip( substr(z, _) ) /*pick off the temperature unit. */ |
||
end |
end |
||
else u='k' |
else u= 'k' /*assume kelvin as per task requirement*/ |
||
if \datatype(n, 'N') then call serr 'illegal number:' n |
if \datatype(n, 'N') then call serr 'illegal number:' n |
||
if \all then do /*is there is a TO ααα scale? */ |
if \all then do /*is there is a TO ααα scale? */ |
||
call name ! /*process the TO abbreviation. */ |
call name ! /*process the TO abbreviation. */ |
||
!= |
!= s n /*assign the full name to ! */ |
||
end /*!: now contains temperature full name*/ |
end /*!: now contains temperature full name*/ |
||
call name u /*allow alternate scale (miss)spellings*/ |
call name u /*allow alternate scale (miss)spellings*/ |
||
select /*convert ──► °Fahrenheit temperatures.*/ |
select /*convert ──► °Fahrenheit temperatures.*/ |
||
when sn=='CELSIUS' then F=n * 9/5 + 32 |
when sn=='CELSIUS' then F= n * 9/5 + 32 |
||
when sn=='DELISLE' then F=212 -(n * 6/5) |
when sn=='DELISLE' then F= 212 -(n * 6/5) |
||
when sn=='FAHRENHEIT' then F=n |
when sn=='FAHRENHEIT' then F= n |
||
when sn=='KELVIN' then F=n * 9/5 - 459.67 |
when sn=='KELVIN' then F= n * 9/5 - 459.67 |
||
when sn=='NEWTON' then F=n * 60/11 + 32 |
when sn=='NEWTON' then F= n * 60/11 + 32 |
||
when sn=='RANKINE' then F=n - 459.67 |
when sn=='RANKINE' then F= n - 459.67 /*a single R is taken as Rankine.*/ |
||
when sn=='REAUMUR' then F=n * 9/4 + 32 |
when sn=='REAUMUR' then F= n * 9/4 + 32 |
||
when sn=='ROMER' then F=(n-7.5) * 27/4 + 32 |
when sn=='ROMER' then F= (n-7.5) * 27/4 + 32 |
||
otherwise call serr 'illegal temperature scale: ' u |
otherwise call serr 'illegal temperature scale: ' u |
||
end /*select*/ |
end /*select*/ |
||
Line 3,382: | Line 3,382: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) |
s: if arg(1)==1 then return arg(3); return word( arg(2) 's', 1) |
||
serr: say; say '***error!***'; say; say arg(1); say; exit 13 |
serr: say; say '***error!***'; say; say arg(1); say; exit 13 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
$: procedure; |
$: procedure; showDig= 8 /*only show eight significant digits.*/ |
||
_=format(arg(1), , showDig) / 1 |
_= format( arg(1), , showDig) / 1 /*format number 8 digs past dec, point.*/ |
||
p=pos(., _); |
p= pos(., _); L= length(_) /*find position of the decimal point. */ |
||
/* [↓] align integers with FP numbers.*/ |
/* [↓] align integers with FP numbers.*/ |
||
if p==0 then _=_ || left('',5+showDig+1) /*the number has no decimal point. |
if p==0 then _= _ || left('', 5+showDig+1) /*the number has no decimal point. */ |
||
else _=_ || left('',5+showDig-L+p) /* " " " a " " |
else _= _ || left('', 5+showDig-L+p) /* " " " a " " */ |
||
return right(_, 50) /*return the re-formatted number (arg).*/ |
return right(_, 50) /*return the re-formatted number (arg).*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
name: parse arg y /*abbreviations ──► shortname.*/ |
name: parse arg y /*abbreviations ──► shortname.*/ |
||
yU=translate(y, 'eE', "éÉ"); |
yU= translate(y, 'eE', "éÉ"); upper yU /*uppercase the temperature unit*/ |
||
if left(yU, 7)=='DEGREES' then yU=substr(yU, 8) |
if left(yU, 7)=='DEGREES' then yU= substr(yU, 8) /*redundant "degrees" after #? */ |
||
if left(yU, 6)=='DEGREE' then yU=substr(yU, 7) |
if left(yU, 6)=='DEGREE' then yU= substr(yU, 7) /* " "degree" " " */ |
||
yU=strip(yU) |
yU= strip(yU) /*elide blanks at front and back*/ |
||
_=length(yU) |
_= length(yU) /*obtain the yU length. */ |
||
if right(yU, |
if right(yU,1)=='S' & _>1 then yU= left(yU, _-1) /*elide trailing plural, if any.*/ |
||
select /*abbreviations ──► shortname.*/ |
select /*abbreviations ──► shortname.*/ |
||
when abbrev('CENTIGRADE' , yU) |, |
when abbrev('CENTIGRADE' , yU) |, |
||
Line 3,414: | Line 3,415: | ||
abbrev('CELISU' , yU) |, /* 1% misspelled.*/ |
abbrev('CELISU' , yU) |, /* 1% misspelled.*/ |
||
abbrev('CELSU' , yU) |, /* 1% misspelled.*/ |
abbrev('CELSU' , yU) |, /* 1% misspelled.*/ |
||
abbrev('CELSIU' , yU) then sn='CELSIUS' |
abbrev('CELSIU' , yU) then sn= 'CELSIUS' |
||
when abbrev('DELISLE' , yU,2) then sn='DELISLE' |
when abbrev('DELISLE' , yU,2) then sn= 'DELISLE' |
||
when abbrev('FARENHEIT' , yU) |, /* 39% misspelled.*/ |
when abbrev('FARENHEIT' , yU) |, /* 39% misspelled.*/ |
||
abbrev('FARENHEIGHT', yU) |, /* 15% misspelled.*/ |
abbrev('FARENHEIGHT', yU) |, /* 15% misspelled.*/ |
||
Line 3,431: | Line 3,432: | ||
abbrev('FARINHEIT' , yU) |, /* 1% misspelled.*/ |
abbrev('FARINHEIT' , yU) |, /* 1% misspelled.*/ |
||
abbrev('FARANHITE' , yU) |, /* 1% misspelled.*/ |
abbrev('FARANHITE' , yU) |, /* 1% misspelled.*/ |
||
abbrev('FAHRENHEIT' , yU) then sn='FAHRENHEIT' |
abbrev('FAHRENHEIT' , yU) then sn= 'FAHRENHEIT' |
||
when abbrev('KALVIN' , yU) |, /* 27% misspelled.*/ |
when abbrev('KALVIN' , yU) |, /* 27% misspelled.*/ |
||
abbrev('KERLIN' , yU) |, /* 18% misspelled.*/ |
abbrev('KERLIN' , yU) |, /* 18% misspelled.*/ |
||
abbrev('KEVEN' , yU) |, /* 9% misspelled.*/ |
abbrev('KEVEN' , yU) |, /* 9% misspelled.*/ |
||
abbrev('KELVIN' , yU) then sn='KELVIN' |
abbrev('KELVIN' , yU) then sn= 'KELVIN' |
||
when abbrev('NEUTON' , yU) |, /*100% misspelled.*/ |
when abbrev('NEUTON' , yU) |, /*100% misspelled.*/ |
||
abbrev('NEWTON' , yU) then sn='NEWTON' |
abbrev('NEWTON' , yU) then sn= 'NEWTON' |
||
when abbrev('RANKINE' , yU, 1) then sn='RANKINE' |
when abbrev('RANKINE' , yU, 1) then sn= 'RANKINE' |
||
when abbrev('REAUMUR' , yU, 2) then sn='REAUMUR' |
when abbrev('REAUMUR' , yU, 2) then sn= 'REAUMUR' |
||
when abbrev('ROEMER' , yU, 2) |, |
when abbrev('ROEMER' , yU, 2) |, |
||
abbrev('ROMER' , yU, 2) then sn='ROMER' |
abbrev('ROMER' , yU, 2) then sn= 'ROMER' |
||
otherwise call serr 'illegal temperature scale:' y |
otherwise call serr 'illegal temperature scale:' y |
||
end /*select*/ |
end /*select*/ |
||
return</lang> |
return</lang> |
||
{{out|output|text= when using the input of: <tt> 98.6F to C, -40C, 0 c (water freezes), 37C (body temp), 100 C (water boils), 21 degrees Kelvin, 0 K (outer space?) </tt>}} |
|||
<pre> |
<pre> |
||
─────────────────────────────────────────────────────────────────── 98.6F to C |
─────────────────────────────────────────────────────────────────── 98.6F to C |
||
Line 3,517: | Line 3,518: | ||
<br> |
<br> |
||
⚫ | |||
⚫ | |||
<pre> |
<pre> |
||
───────────────────────────────────────────────────────────────── 0 Fahrenheit |
───────────────────────────────────────────────────────────────── 0 Fahrenheit |
||
Line 3,580: | Line 3,580: | ||
-11.53701838 Wedgwood |
-11.53701838 Wedgwood |
||
</pre> |
</pre> |
||
{{out|output|text= when using the input of: <tt> 0 kelvin </tt>}} |
|||
<pre> |
<pre> |
||
───────────────────────────────────────────────────────────────────── 0 kelvin |
───────────────────────────────────────────────────────────────────── 0 kelvin |