Temperature conversion: Difference between revisions
Content added Content deleted
m (→{{header|PHP}}: remove blank line) |
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
||
Line 235: | Line 235: | ||
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees. |
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees. |
||
<value> <K/R/F/C> ? </lang> |
<value> <K/R/F/C> ? </lang> |
||
=={{header|REXX}}== |
|||
This REXX version supports: |
|||
* (alternate spellings with optional ''degrees''): |
|||
** Centingrade, Celsius, Celcius |
|||
** Fahrenheit |
|||
** Kelvin |
|||
** Rankine |
|||
** Reaumur |
|||
* multiple temperatures in a list |
|||
* comments within the list |
|||
* aligned output (whole numbers and decimal fractions) |
|||
<lang rexx>/*REXX program converts units of temperature for: C, F, R, Re, K. */ |
|||
parse arg tList /*get specified temperature lists*/ |
|||
do until tList='' /*process a list of temperatures.*/ |
|||
parse var tList x ',' tList /*temps are separated by commas. */ |
|||
x=space(x); parse var x z '(' /*handle any comments (if any). */ |
|||
if z=='' then call serr 'no arguments were specified.' |
|||
_ = verify(z, '+-.0123456789') /*all the legal number thingys. */ |
|||
if _\==0 then do |
|||
if _==1 then call serr 'illegal temperature:' # |
|||
n= left(z,_-1) /*pick off the number (hopefully)*/ |
|||
u= strip(substr(z,_)) /*pick off the temperature unit. */ |
|||
end |
|||
else u= 'k' /*assume Kelvin as per task req.*/ |
|||
uU=u; upper uU /*uppercase version of temp unit.*/ |
|||
if left(uU,7)=='DEGREES' then uU=substr(uU,8) /*reduntant "degrees"? */ |
|||
if left(uU,5)=='DEGREE' then uU=substr(uU,7) /*reduntant "degree" ? */ |
|||
uU=strip(uU) |
|||
if \datatype(n,'N') then call serr 'illegal number:' n |
|||
/*accept alternate spellings. */ |
|||
select /*convert ───◄ common temperature*/ |
|||
when abbrev('CENTIGRADE', uU) |, |
|||
abbrev('CELSIUS' , uU) |, |
|||
abbrev('CELCIUS' , uU) then F = n * 1.8 + 32 |
|||
when abbrev('FAHRENHEIT', uU) then F = n |
|||
when abbrev('KELVIN' , uU) then F = n * 1.8 - 459.67 |
|||
when abbrev('RANKINE' , uU) then F = n - 459.67 |
|||
when abbrev('REAUMUR' , uU, 2) then F = n * 2.25 + 32 |
|||
otherwise call serr 'illegal temperature scale:' u |
|||
end /*select*/ |
|||
say right(' ' x,55,"─") |
|||
say Tform((F - 32) / 1.8 ) 'Celcius' |
|||
say Tform( F ) 'Fahrenheit' |
|||
say Tform((F + 459.67) * 1.8 ) 'Kelvin' |
|||
say Tform( F + 459.67 ) 'Rankine' |
|||
say Tform((F - 32 ) / 2.25 ) 'Reaumur' |
|||
end /*until tlist=''*/ |
|||
exit /*stick a fork in it, we're done.*/ |
|||
/*──────────────────────────────────TFORM subroutine────────────────────*/ |
|||
Tform: procedure; showDig=3; _=format(arg(1),,showDig)/1; p=pos('.',_) |
|||
if p==0 then _=_ || left('',showDig+1) |
|||
else _=_ || left('',showDig-length(_)+p); return right(_,20) |
|||
/*──────────────────────────────────SERR subroutine─────────────────────*/ |
|||
serr: say; say '***error!***'; say; say arg(1); say; exit 13</lang> |
|||
'''output''' when using the input of: <tt> -40C, 0 c (water freezes), 37C (body temp), 100 C (water boils), 21 degrees Kelvin, 0K (outer space?) </tt> |
|||
<pre style="overflow:scroll"> |
|||
───────────────────────────────────────────────── -40C |
|||
-40 Celcius |
|||
-40 Fahrenheit |
|||
755.406 Kelvin |
|||
419.67 Rankine |
|||
-32 Reaumur |
|||
────────────────────────────────── 0 c (water freezes) |
|||
0 Celcius |
|||
32 Fahrenheit |
|||
885.006 Kelvin |
|||
491.67 Rankine |
|||
0 Reaumur |
|||
────────────────────────────────────── 37C (body temp) |
|||
37 Celcius |
|||
98.6 Fahrenheit |
|||
1004.886 Kelvin |
|||
558.27 Rankine |
|||
29.6 Reaumur |
|||
────────────────────────────────── 100 C (water boils) |
|||
100 Celcius |
|||
212 Fahrenheit |
|||
1209.006 Kelvin |
|||
671.67 Rankine |
|||
80 Reaumur |
|||
──────────────────────────────────── 21 degrees Kelvin |
|||
-252.15 Celcius |
|||
-421.87 Fahrenheit |
|||
68.04 Kelvin |
|||
37.8 Rankine |
|||
-201.72 Reaumur |
|||
──────────────────────────────────── 0K (outer space?) |
|||
-273.15 Celcius |
|||
-459.67 Fahrenheit |
|||
0 Kelvin |
|||
0 Rankine |
|||
-218.52 Reaumur |
|||
</pre> |