Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,18 +1,25 @@
There are quite a number of temperature scales. For this task we will concentrate on 4 of the perhaps best-known ones: [[wp:Kelvin|Kelvin]], [[wp:Degree Celsius|Celsius]], [[wp:Fahrenheit|Fahrenheit]] and [[wp:Degree Rankine|Rankine]].
{{omit from|Lilypond}}
There are quite a number of temperature scales. <br>
For this task we will concentrate on 4 of the perhaps best-known ones: [[wp:Kelvin|Kelvin]], [[wp:Degree Celsius|Celsius]], [[wp:Fahrenheit|Fahrenheit]] and [[wp:Degree Rankine|Rankine]].
The Celsius and Kelvin scales have the same magnitude, but different null points.
:0 degrees Celsius corresponds to '''273.15''' kelvin.
:0 kelvin is absolute zero.
The Fahrenheit and Rankine scales also have the same magnitude, but different null points.
The Fahrenheit and Rankine scales also have the same magnitude,
but different null points.
:0 degrees Fahrenheit corresponds to '''459.67''' degrees Rankine.
:0 degrees Rankine is absolute zero.
The Celsius/Kelvin and Fahrenheit/Rankine scales have a ratio of '''5 : 9'''.
Write code that accepts a value of kelvin, converts it to values on the three other scales and prints the result. For instance:
Write code that accepts a value of kelvin, converts it
to values on the three other scales and prints the result.
For instance:
<pre>
K 21.00

View file

@ -0,0 +1,19 @@
# usage: gawk -f temperature_conversion.awk input.txt -
BEGIN { print("# Temperature conversion\n") }
BEGINFILE { print "# reading", FILENAME
if( FILENAME=="-" ) print "# Please enter temperature values in K:\n"
}
!NF { exit }
{ print "Input:" $0 }
$1<0 { print("K must be >= 0\n"); next }
{ K = 0+$1
printf("K = %8.2f Kelvin degrees\n",K)
printf("C = %8.2f\n", K - 273.15)
printf("F = %8.2f\n", K * 1.8 - 459.67)
printf("R = %8.2f\n\n",K * 1.8)
}
END { print("# Bye.") }

View file

@ -0,0 +1,12 @@
(defn to-celsius [k]
(- k 273.15))
(defn to-fahrenheit [k]
(- (* k 1.8) 459.67))
(defn to-rankine [k]
(* k 1.8))
(defn temperature-conversion [k]
(if (number? k)
(format "Celsius: %.2f Fahrenheit: %.2f Rankine: %.2f"
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format "Error: Non-numeric value entered.")))

View file

@ -0,0 +1,13 @@
(defun to-celsius (k)
(- k 273.15))
(defun to-fahrenheit (k)
(- (* k 1.8) 459.67))
(defun to-rankine (k)
(* k 1.8))
(defun temperature-conversion ()
(let ((k (read)))
(if (numberp k)
(format t "Celsius: ~d~%Fahrenheit: ~d~%Rankine: ~d~%"
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format t "Error: Non-numeric value entered."))))

View file

@ -0,0 +1,41 @@
program Temperature;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TTemp = class
private
fCelsius, fFahrenheit, fRankine: double;
public
constructor Create(aKelvin: double);
property AsCelsius: double read fCelsius;
property AsFahrenheit: double read fFahrenheit;
property AsRankine: double read fRankine;
end;
{ TTemp }
constructor TTemp.Create(aKelvin: double);
begin
fCelsius := aKelvin - 273.15;
fRankine := aKelvin * 9 / 5;
fFahrenheit := fRankine - 459.67;
end;
var
kelvin: double;
temp: TTemp;
begin
write('Kelvin: ');
readln(kelvin);
temp := TTemp.Create(kelvin);
writeln(Format('Celsius: %.2f', [temp.AsCelsius]));
writeln(Format('Fahrenheit: %.2f', [temp.AsFahrenheit]));
writeln(Format('Rankine: %.2f', [temp.AsRankine]));
temp.Free;
readln;
end.

View file

@ -0,0 +1,7 @@
include std/console.e
atom K
while 1 do
K = prompt_number("Enter temperature in Kelvin >=0: ",{0,4294967296})
printf(1,"K = %5.2f\nC = %5.2f\nF = %5.2f\nR = %5.2f\n\n",{K,K-273.15,K*1.8-459.67,K*1.8})
end while

View file

@ -0,0 +1,9 @@
A1 : Kelvin
B1 : Celsius
C1 : Fahrenheit
D1 : Rankine
Name A2 : K
B2 : =K-273.15
C2 : =K*1.8-459.67
D2 : =K*1.8
Input in A1

View file

@ -0,0 +1,26 @@
Program Temperature
implicit none
real :: kel, cel, fah, ran
write(*,*) "Input Kelvin temperature to convert"
read(*,*) kel
call temp_convert(kel, cel, fah, ran)
write(*, "((a10), f10.3)") "Kelvin", kel
write(*, "((a10), f10.3)") "Celsius", cel
write(*, "((a10), f10.3)") "Fahrenheit", fah
write(*, "((a10), f10.3)") "Rankine", ran
contains
subroutine temp_convert(kelvin, celsius, fahrenheit, rankine)
real, intent(in) :: kelvin
real, intent(out) :: celsius, fahrenheit, rankine
celsius = kelvin - 273.15
fahrenheit = kelvin * 1.8 - 459.67
rankine = kelvin * 1.8
end subroutine
end program

View file

@ -0,0 +1,18 @@
main = do
putStrLn "Please enter temperature in kelvin: "
input <- getLine
let kelvin = read input :: Double
if
kelvin < 0.0
then
putStrLn "error"
else
let
celsius = kelvin - 273.15
fahrenheit = kelvin * 1.8 - 459.67
rankine = kelvin * 1.8
in do
putStrLn ("kelvin: " ++ show kelvin)
putStrLn ("celsius: " ++ show celsius)
putStrLn ("fahrenheit: " ++ show fahrenheit)
putStrLn ("rankine: " ++ show rankine)

View file

@ -1,34 +1,22 @@
NB. Format matrix for printing
fmt =: '0.2' 8!:0 k2KCFR
NB. Tag each temp with scale, for human
NB. legibility.
kcfr =: 0 _1 |: 'KCFR' ,"0 1"_1 >@:fmt
NB. Format matrix for printing & tag each
NB. temp with scale, for human legibility
fmt =: [: (;:inv"1) 0 _1 |: 'KCFR' ;"0 1"_1 '0.2' 8!:0 ]
kcfr =: fmt@k2KCFR
kcfr 21
K 21.00
C-252.00
F-421.87
R 37.80
K 21.00
C -252.00
F -421.87
R 37.80
kcfr 0 NB. Absolute zero
K 0.00
C-273.00
F-459.67
R 0.00
K 0.00
C -273.00
F -459.67
R 0.00
kcfr 21 100 300 NB. List of temps works fine
K 21.00
C-252.00
F-421.87
R 37.80
K 100.00
C-173.00
F-279.67
R 180.00
K 300.00
C 27.00
F 80.33
R 540.00
K 21.00 100.00 300.00
C -252.00 -173.00 27.00
F -421.87 -279.67 80.33
R 37.80 180.00 540.00

View file

@ -1 +1,4 @@
cfr(k) = print("Kelvin: $k, Celsius: $(round(k-273.15,2)), Fahrenheit: $(round(k*1.8-459.67,2)), Rankine: $(round(k*1.8,2))")
cfr(k) = print("Kelvin: $k, ",
"Celsius: $(round(k-273.15,2)), ",
"Fahrenheit: $(round(k*1.8-459.67,2)), ",
"Rankine: $(round(k*1.8,2))")

View file

@ -0,0 +1,13 @@
function convert_temp(k)
local c = k - 273.15
local r = k * 1.8
local f = r - 459.67
return k, c, r, f
end
print(string.format([[
Kelvin: %.2f K
Celcius: %.2f °C
Rankine: %.2f °R
Fahrenheit: %.2f °F
]],convert_temp(21.0)))

View file

@ -0,0 +1,20 @@
let print_temp s t =
print_string s;
print_endline (string_of_float t);;
let kelvin_to_celsius k =
k -. 273.15;;
let kelvin_to_fahrenheit k =
(kelvin_to_celsius k)*. 9./.5. +. 32.00;;
let kelvin_to_rankine k =
(kelvin_to_celsius k)*. 9./.5. +. 491.67;;
print_endline "Enter a temperature in Kelvin please:";
let k = read_float () in
print_temp "K " k;
print_temp "C " (kelvin_to_celsius k);
print_temp "F " (kelvin_to_fahrenheit k);
print_temp "R " (kelvin_to_rankine k);;

View file

@ -1,10 +1,13 @@
/*REXX program converts temperature for: C, D, F, N, Ra, Re, Ro, and K.*/
/*REXX program converts temperatures for a number of temperature scales.*/
numeric digits 120 /*be able to support huge numbers*/
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=translate(x,'((',"[{") /*support other grouping symbols.*/
x=space(x); parse var x z '(' /*handle any comments (if any). */
parse upper var z z ' TO ' ! . /*separate the TO option from #*/
if !=='' then !='ALL'; all=!=='ALL' /*allow specification of "TO" opt*/
if z=='' then call serr 'no arguments were specified.'
_=verify(z, '+-.0123456789') /*a list of valid number thingys.*/
n=z
@ -13,87 +16,102 @@ parse arg tList /*get specified temperature lists*/
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.*/
else u='k' /*assume kelvin as per task req.*/
uU=translate(u,'eE',"éÉ"); upper uU /*uppercase version of temp unit.*/
if left(uU,7)=='DEGREES' then uU=substr(uU,8) /*redundant "degrees"? */
if left(uU,6)=='DEGREE' then uU=substr(uU,7) /* " "degree" ? */
uU=strip(uU)
if \datatype(n,'N') then call serr 'illegal number:' n
/*accept alternate spellings. */
select /*convert ──► ºF temperatures. */
when abbrev('CENTIGRADE' , uU) |,
abbrev('CENTRIGRADE', uU) |, /* 50% misspelled.*/
abbrev('CETIGRADE' , uU) |, /* 50% misspelled.*/
abbrev('CENTINGRADE', uU) |,
abbrev('CENTESIMAL' , uU) |,
abbrev('CELCIUS' , uU) |, /* 82% misspelled.*/
abbrev('CELCIOUS' , uU) |, /* 4% misspelled.*/
abbrev('CELCUIS' , uU) |, /* 4% misspelled.*/
abbrev('CELSUIS' , uU) |, /* 2% misspelled.*/
abbrev('CELCEUS' , uU) |, /* 2% misspelled.*/
abbrev('CELCUS' , uU) |, /* 2% misspelled.*/
abbrev('CELISUS' , uU) |, /* 1% misspelled.*/
abbrev('CELSUS' , uU) |, /* 1% misspelled.*/
abbrev('CELSIUS' , uU) then F=n * 9/5 + 32
if \all then do /*there is a TO ααα scale.*/
call name ! /*process the TO abbreviation*/
!=sn /*assign the full name to ! */
end /* ! now contains scale full name*/
call name u /*allow alternate scale spellings*/
when abbrev('DELISLE' , uU) then F=212 -(n * 6/5)
when abbrev('FARENHEIT' , uU) |, /* 39% misspelled.*/
abbrev('FARENHEIGHT', uU) |, /* 15% misspelled.*/
abbrev('FARENHITE' , uU) |, /* 6% misspelled.*/
abbrev('FARENHIET' , uU) |, /* 3% misspelled.*/
abbrev('FARHENHEIT' , uU) |, /* 3% misspelled.*/
abbrev('FARINHEIGHT', uU) |, /* 2% misspelled.*/
abbrev('FARENHIGHT' , uU) |, /* 2% misspelled.*/
abbrev('FAHRENHIET' , uU) |, /* 2% misspelled.*/
abbrev('FERENHEIGHT', uU) |, /* 2% misspelled.*/
abbrev('FEHRENHEIT' , uU) |, /* 2% misspelled.*/
abbrev('FERENHEIT' , uU) |, /* 2% misspelled.*/
abbrev('FERINHEIGHT', uU) |, /* 1% misspelled.*/
abbrev('FARIENHEIT' , uU) |, /* 1% misspelled.*/
abbrev('FARINHEIT' , uU) |, /* 1% misspelled.*/
abbrev('FARANHITE' , uU) |, /* 1% misspelled.*/
abbrev('FAHRENHEIT' , uU) then F=n
when abbrev('KELVINS' , uU) |, /* 46% misspelled.*/
abbrev('KALVIN' , uU) |, /* 27% misspelled.*/
abbrev('KERLIN' , uU) |, /* 18% misspelled.*/
abbrev('KEVEN' , uU) |, /* 9% misspelled.*/
abbrev('KELVIN' , uU) then F=n * 9/5 - 459.67
when abbrev('NEUTON' , uU) |, /*100% misspelled.*/
abbrev('NEWTON' , uU) then F=n * 60/11 + 32
/*a single R is taken as Rankine.*/
when abbrev('RANKINE' , uU) then F=n - 459.67
when abbrev('REAUMUR' , uU, 2) then F=n * 9/4 + 32
when abbrev('ROEMER' , uU, 2) |,
abbrev('ROMER' , uU, 2) then F=(n-7.5) * 27/4 + 32
otherwise call serr 'illegal temperature scale:' u
select /*convert ──► °F temperatures. */
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=='KELVIN' then F=n * 9/5 - 459.67
when sn=='NEWTON' then F=n * 60/11 + 32
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=='ROMER' then F=(n-7.5) * 27/4 + 32
otherwise call serr 'illegal temperature scale: ' u
end /*select*/
say right(' ' x,55,"") /*show original value&scale, sep.*/
say Tform( ( F - 32 ) * 5/9 ) 'Celsius'
say Tform( ( 212 - F ) * 5/6 ) 'Delisle'
say Tform( F ) 'Fahrenheit'
say Tform( ( F + 459.67 ) * 5/9 ) 'Kelvin'
say Tform( ( F - 32 ) * 11/60 ) 'Newton'
say Tform( F + 459.67 ) 'Rankine'
say Tform( ( F - 32 ) * 4/9 ) 'Reaumur'
say Tform( ( F - 32 ) * 7/24 + 7.5 ) 'Romer'
end /*until tlist='' */
K = (F + 459.67) * 5/9 /*compute temperature to kelvins.*/
say right(' ' x, 79, "") /*show original value &scale,sep.*/
if all | !=='CELSIUS' then say $( ( F - 32 ) * 5/9 ) 'Celsius'
if all | !=='DELISLE' then say $( ( 212 - F ) * 5/6 ) 'Delisle'
if all | !=='FAHRENHEIT' then say $( F ) 'Fahrenheit'
if all | !=='KELVIN' then say $( K ) 'kelvin's(K)
if all | !=='NEWTON' then say $( ( F - 32 ) * 11/60 ) 'Newton'
if all | !=='RANKINE' then say $( F + 349.67 ) 'Rankine'
if all | !=='REAUMUR' then say $( ( F - 32 ) * 4/9 ) 'Reaumur'
if all | !=='ROMER' then say $( ( F - 32 ) * 7/24 + 7.5 ) 'Romer'
end /*until*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────TFORM subroutine────────────────────*/
Tform: procedure; showDig=8 /*only show 8 significant digits.*/
_=format(arg(1),,showDig)/1 /*format arg 8 digs past the . */
p=pos('.',_) /*find position of decimal point.*/
/*──────────────────────────────────$ subroutine────────────────────────*/
$: procedure; showDig=8 /*only show 8 significant digits.*/
_=format(arg(1), , showDig)/1 /*format # 8 digs past dec point.*/
p=pos(.,_) /*find position of decimal point.*/
/* [↓] align integers with FP #s.*/
if p==0 then _=_ || left('',showDig+1) /*no decimal point.*/
else _=_ || left('',showDig-length(_)+p) /*has " " */
return right(_,20) /*return the re-formatted arg. */
/*──────────────────────────────────SERR subroutine─────────────────────*/
serr: say; say '***error!***'; say; say arg(1); say; exit 13
if p==0 then _=_ || left('',5+showDig+1) /*no decimal point.*/
else _=_ || left('',5+showDig-length(_)+p) /*has " " */
return right(_,50) /*return the re-formatted arg. */
/*──────────────────────────────────name subroutine─────────────────────*/
name: parse arg y /*abbreviations ──► shortname. */
yU=translate(y,'eE',"éÉ"); upper yU /*uppercase version of temp unit.*/
if left(yU,7)=='DEGREES' then yU=substr(yU,8) /*redundant "degrees"? */
if left(yU,6)=='DEGREE' then yU=substr(yU,7) /* " "degree" ? */
yU=strip(yU) /*elide blanks at ends.*/
_=length(yU) /*obtain the yU length.*/
if right(yU,1)=='S' & _>1 then yU=left(yU,_-1) /*elide trailing plural*/
select /*abbreviations ──► shortname. */
when abbrev('CENTIGRADE' , yU) |,
abbrev('CENTRIGRADE', yU) |, /* 50% misspelled.*/
abbrev('CETIGRADE' , yU) |, /* 50% misspelled.*/
abbrev('CENTINGRADE', yU) |,
abbrev('CENTESIMAL' , yU) |,
abbrev('CELCIU' , yU) |, /* 82% misspelled.*/
abbrev('CELCIOU' , yU) |, /* 4% misspelled.*/
abbrev('CELCUI' , yU) |, /* 4% misspelled.*/
abbrev('CELSUI' , yU) |, /* 2% misspelled.*/
abbrev('CELCEU' , yU) |, /* 2% misspelled.*/
abbrev('CELCU' , yU) |, /* 2% misspelled.*/
abbrev('CELISU' , yU) |, /* 1% misspelled.*/
abbrev('CELSU' , yU) |, /* 1% misspelled.*/
abbrev('CELSIU' , yU) then sn='CELSIUS'
when abbrev('DELISLE' , yU,2) then sn='DELISLE'
when abbrev('FARENHEIT' , yU) |, /* 39% misspelled.*/
abbrev('FARENHEIGHT', yU) |, /* 15% misspelled.*/
abbrev('FARENHITE' , yU) |, /* 6% misspelled.*/
abbrev('FARENHIET' , yU) |, /* 3% misspelled.*/
abbrev('FARHENHEIT' , yU) |, /* 3% misspelled.*/
abbrev('FARINHEIGHT', yU) |, /* 2% misspelled.*/
abbrev('FARENHIGHT' , yU) |, /* 2% misspelled.*/
abbrev('FAHRENHIET' , yU) |, /* 2% misspelled.*/
abbrev('FERENHEIGHT', yU) |, /* 2% misspelled.*/
abbrev('FEHRENHEIT' , yU) |, /* 2% misspelled.*/
abbrev('FERENHEIT' , yU) |, /* 2% misspelled.*/
abbrev('FERINHEIGHT', yU) |, /* 1% misspelled.*/
abbrev('FARIENHEIT' , yU) |, /* 1% misspelled.*/
abbrev('FARINHEIT' , yU) |, /* 1% misspelled.*/
abbrev('FARANHITE' , yU) |, /* 1% misspelled.*/
abbrev('FAHRENHEIT' , yU) then sn='FAHRENHEIT'
when abbrev('KALVIN' , yU) |, /* 27% misspelled.*/
abbrev('KERLIN' , yU) |, /* 18% misspelled.*/
abbrev('KEVEN' , yU) |, /* 9% misspelled.*/
abbrev('KELVIN' , yU) then sn='KELVIN'
when abbrev('NEUTON' , yU) |, /*100% misspelled.*/
abbrev('NEWTON' , yU) then sn='NEWTON'
when abbrev('RANKINE' , yU, 1) then sn='RANKINE'
when abbrev('REAUMUR' , yU, 2) then sn='REAUMUR'
when abbrev('ROEMER' , yU, 2) |,
abbrev('ROMER' , yU, 2) then sn='ROMER'
otherwise call serr 'illegal temperature scale:' y
end /*select*/
return
/*──────────────────────────────────one─liner subroutines───────────────*/
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

View file

@ -0,0 +1,27 @@
object TemperatureConversion extends App {
def kelvinToCelsius(k: Double) = k + 273.15
def kelvinToFahrenheit(k: Double) = k * 1.8 - 459.67
def kelvinToRankine(k: Double) = k * 1.8
if (args.length == 1) {
try {
val kelvin = args(0).toDouble
if (kelvin >= 0) {
println(f"K $kelvin%2.2f")
println(f"C ${kelvinToCelsius(kelvin)}%2.2f")
println(f"F ${kelvinToFahrenheit(kelvin)}%2.2f")
println(f"R ${kelvinToRankine(kelvin)}%2.2f")
} else println("%2.2f K is below absolute zero", kelvin)
} catch {
case e: NumberFormatException => System.out.println(e)
case e: Throwable => {
println("Some other exception type:")
e.printStackTrace()
}
}
} else println("Temperature not given.")
}