all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
24
Task/Temperature-conversion/0DESCRIPTION
Normal file
24
Task/Temperature-conversion/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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|Celcius]], [[wp:Fahrenheit|Fahrenheit]] and [[wp:Degree Rankine|Rankine]].
|
||||
|
||||
The Celcius and Kelvin scales have the same magnitude, but different null points.
|
||||
|
||||
:0 degrees Celcius corresponds to '''273.15''' kelvin.
|
||||
:0 kelvin is absolute zero.
|
||||
|
||||
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 Celcius/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:
|
||||
<pre>
|
||||
K 21.00
|
||||
|
||||
C -252.15
|
||||
|
||||
F -421.87
|
||||
|
||||
R 37.80
|
||||
</pre>
|
||||
19
Task/Temperature-conversion/AWK/temperature-conversion.awk
Normal file
19
Task/Temperature-conversion/AWK/temperature-conversion.awk
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# syntax: AWK -f TEMPERATURE_CONVERSION.AWK
|
||||
BEGIN {
|
||||
while (1) {
|
||||
printf("\nKelvin degrees? ")
|
||||
getline K
|
||||
if (K ~ /^$/) {
|
||||
break
|
||||
}
|
||||
if (K < 0) {
|
||||
print("K must be > 0")
|
||||
continue
|
||||
}
|
||||
printf("K = %.2f\n",K)
|
||||
printf("C = %.2f\n",K - 273.15)
|
||||
printf("F = %.2f\n",K * 1.8 - 459.67)
|
||||
printf("R = %.2f\n",K * 1.8)
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
10 REM TRANSLATION OF AWK VERSION
|
||||
20 INPUT "KELVIN DEGREES",K
|
||||
30 IF K <= 0 THEN END: REM A VALUE OF ZERO OR LESS WILL END PROGRAM
|
||||
40 LET C = K - 273.15
|
||||
50 LET F = K * 1.8 - 459.67
|
||||
60 LET R = K * 1.8
|
||||
70 PRINT K; " KELVIN IS EQUIVALENT TO"
|
||||
80 PRINT C; " DEGREES CELCIUS"
|
||||
90 PRINT F; " DEGREES FAHRENHEIT"
|
||||
100 PRINT R; " DEGREES RANKINE"
|
||||
110 GOTO 20
|
||||
29
Task/Temperature-conversion/C/temperature-conversion.c
Normal file
29
Task/Temperature-conversion/C/temperature-conversion.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
double kelvinToCelsius(double k){
|
||||
return k - 273.15;
|
||||
}
|
||||
|
||||
double kelvinToFahrenheit(double k){
|
||||
return k * 1.8 - 459.67;
|
||||
}
|
||||
|
||||
double kelvinToRankine(double k){
|
||||
return k * 1.8;
|
||||
}
|
||||
void convertKelvin(double kelvin) {
|
||||
printf("K %.2f\n", kelvin);
|
||||
printf("C %.2f\n", kelvinToCelsius(kelvin));
|
||||
printf("F %.2f\n", kelvinToFahrenheit(kelvin));
|
||||
printf("R %.2f", kelvinToRankine(kelvin));
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[])
|
||||
{
|
||||
if (argc > 1) {
|
||||
double kelvin = atof(argv[1]);
|
||||
convertKelvin(kelvin);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
33
Task/Temperature-conversion/D/temperature-conversion.d
Normal file
33
Task/Temperature-conversion/D/temperature-conversion.d
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
double kelvinToCelsius(in double k) pure nothrow @safe {
|
||||
return k - 273.15;
|
||||
}
|
||||
|
||||
double kelvinToFahrenheit(in double k) pure nothrow @safe {
|
||||
return k * 1.8 - 459.67;
|
||||
}
|
||||
|
||||
double kelvinToRankine(in double k) pure nothrow @safe {
|
||||
return k * 1.8;
|
||||
}
|
||||
|
||||
unittest {
|
||||
import std.math: approxEqual;
|
||||
assert(approxEqual(kelvinToCelsius(21.0), -252.15));
|
||||
assert(approxEqual(kelvinToFahrenheit(21.0), -421.87));
|
||||
assert(approxEqual(kelvinToRankine(21.0), 37.8));
|
||||
}
|
||||
|
||||
void main(string[] args) {
|
||||
import std.stdio, std.conv, std.string;
|
||||
|
||||
if (args.length == 2 && isNumeric(args[1])) {
|
||||
immutable kelvin = to!double(args[1]);
|
||||
if (kelvin >= 0) {
|
||||
writefln("K %2.2f", kelvin);
|
||||
writefln("C %2.2f", kelvinToCelsius(kelvin));
|
||||
writefln("F %2.2f", kelvinToFahrenheit(kelvin));
|
||||
writefln("R %2.2f", kelvinToRankine(kelvin));
|
||||
} else
|
||||
writefln("%2.2f K is below absolute zero", kelvin);
|
||||
}
|
||||
}
|
||||
11
Task/Temperature-conversion/J/temperature-conversion-1.j
Normal file
11
Task/Temperature-conversion/J/temperature-conversion-1.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
NB. Temp conversions are all linear polynomials
|
||||
K2K =: 0 1 NB. K = (1 *k) + 0
|
||||
K2C =: _273 1 NB. C = (1 *k) - 273
|
||||
K2F =: _459.67 1.8 NB. F = (1.8*k) - 459.67
|
||||
K2R =: 0 1.8 NB. R = (1.8*k) + 0
|
||||
|
||||
NB. Do all conversions at once (eval
|
||||
NB. polynomials in parallel). This is the
|
||||
NB. numeric matrix J programs would manipulate
|
||||
NB. directly.
|
||||
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]
|
||||
34
Task/Temperature-conversion/J/temperature-conversion-2.j
Normal file
34
Task/Temperature-conversion/J/temperature-conversion-2.j
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
|
||||
kcfr 21
|
||||
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
|
||||
|
||||
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
|
||||
31
Task/Temperature-conversion/Java/temperature-conversion.java
Normal file
31
Task/Temperature-conversion/Java/temperature-conversion.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
public class TemperatureConversion {
|
||||
public static void main(String args[]) {
|
||||
if (args.length == 1) {
|
||||
try {
|
||||
double kelvin = Double.parseDouble(args[0]);
|
||||
if (kelvin >= 0) {
|
||||
System.out.printf("K %2.2f\n", kelvin);
|
||||
System.out.printf("C %2.2f\n", kelvinToCelcius(kelvin));
|
||||
System.out.printf("F %2.2f\n", kelvinToFahrenheit(kelvin));
|
||||
System.out.printf("R %2.2f\n", kelvinToRankine(kelvin));
|
||||
} else {
|
||||
System.out.printf("%2.2f K is below absolute zero", kelvin);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static double kelvinToCelcius(double k) {
|
||||
return k + 273.15;
|
||||
}
|
||||
|
||||
public static double kelvinToFahrenheit(double k) {
|
||||
return k * 1.8 - 459.67;
|
||||
}
|
||||
|
||||
public static double kelvinToRankine(double k) {
|
||||
return k * 1.8;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
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))")
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
tempConvert[t_] :=
|
||||
Grid[Transpose@{{"K", "C", "F", "R"},
|
||||
Round[{t, t - 273.15, 9 t/5 - 459.67, 9 t/5}, .01]}]
|
||||
|
||||
tempConvert[21]
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
class Temperature {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
k := System.IO.Console->ReadString()->ToFloat();
|
||||
c := KelvinToCelcius(k);
|
||||
f := KelvinToFahrenheit(k);
|
||||
r := KelvinToRankine(k);
|
||||
|
||||
"K: {$k}"->PrintLine();
|
||||
"C: {$c}"->PrintLine();
|
||||
"F: {$f}"->PrintLine();
|
||||
"R: {$r}"->PrintLine();
|
||||
}
|
||||
|
||||
function : KelvinToCelcius(k : Float) ~ Float {
|
||||
return k - 273.15;
|
||||
}
|
||||
|
||||
function : KelvinToFahrenheit(k : Float) ~ Float {
|
||||
return k * 1.8 - 459.67;
|
||||
}
|
||||
|
||||
function : KelvinToRankine(k : Float) ~ Float {
|
||||
return k * 1.8;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
int main(int argc, const char * argv[])
|
||||
{
|
||||
@autoreleasepool {
|
||||
if(argc > 1)
|
||||
{
|
||||
NSString *arg1 = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
|
||||
// encoding shouldn't matter in this case
|
||||
double kelvin = [arg1 doubleValue];
|
||||
|
||||
NSLog(@"K %.2f",kelvin);
|
||||
NSLog(@"C %.2f\n", kelvin - 273.15);
|
||||
NSLog(@"F %.2f\n", (kelvin * 1.8) - 459.67);
|
||||
NSLog(@"R %.2f", kelvin * 1.8);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
f(x)=[x,x-273.15,1.8*x-459.67,1.8*x]
|
||||
20
Task/Temperature-conversion/PHP/temperature-conversion.php
Normal file
20
Task/Temperature-conversion/PHP/temperature-conversion.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
error_reporting(E_ALL & ~ ( E_NOTICE | E_WARNING ));
|
||||
|
||||
while (true) {
|
||||
echo "\nEnter a value in kelvin (q to quit): ";
|
||||
if (($kelvin = trim(fgets(STDIN))) !== false) {
|
||||
if ($kelvin == 'q') {
|
||||
echo 'quitting';
|
||||
break;
|
||||
}
|
||||
if (is_numeric($kelvin)) {
|
||||
$kelvin = floatVal($kelvin);
|
||||
if ($kelvin >= 0) {
|
||||
printf(" K %2.2f\n", $kelvin);
|
||||
printf(" C %2.2f\n", $kelvin - 273.15);
|
||||
printf(" F %2.2f\n", $kelvin * 1.8 - 459.67);
|
||||
printf(" R %2.2f\n", $kelvin * 1.8);
|
||||
} else printf(" %2.2f K is below absolute zero\n", $kelvin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
while '' ne my $answer = prompt 'Temperature: ' {
|
||||
my $k = do given $answer {
|
||||
when s/:i C $// { $_ + 273.15 }
|
||||
when s/:i F $// { ($_ + 459.67) / 1.8 }
|
||||
when s/:i R $// { $_ / 1.8 }
|
||||
when s/:i K $// { $_ }
|
||||
default { $_ }
|
||||
}
|
||||
say " { $k }K";
|
||||
say " { $k - 273.15 }℃";
|
||||
say " { $k * 1.8 - 459.67 }℉";
|
||||
say " { $k * 1.8 }R";
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
>>> while True:
|
||||
k = float(input('K ? '))
|
||||
print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees."
|
||||
% (k, k - 273.15, k * 1.8 - 459.67, k * 1.8))
|
||||
|
||||
|
||||
K ? 21.0
|
||||
21 Kelvin = -252.15 Celsius = -421.87 Fahrenheit = 37.8 Rankine degrees.
|
||||
K ? 222.2
|
||||
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
|
||||
K ?
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
>>> toK = {'C': (lambda c: c + 273.15),
|
||||
'F': (lambda f: (f + 459.67) / 1.8),
|
||||
'R': (lambda r: r / 1.8),
|
||||
'K': (lambda k: k) }
|
||||
>>> while True:
|
||||
magnitude, unit = input('<value> <K/R/F/C> ? ').split()
|
||||
k = toK[unit](float(magnitude))
|
||||
print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees."
|
||||
% (k, k - 273.15, k * 1.8 - 459.67, k * 1.8))
|
||||
|
||||
|
||||
<value> <K/R/F/C> ? 222.2 K
|
||||
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
|
||||
<value> <K/R/F/C> ? -50.95 C
|
||||
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
|
||||
<value> <K/R/F/C> ? -59.71 F
|
||||
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
|
||||
<value> <K/R/F/C> ? 399.96 R
|
||||
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
|
||||
<value> <K/R/F/C> ?
|
||||
55
Task/Temperature-conversion/REXX/temperature-conversion.rexx
Normal file
55
Task/Temperature-conversion/REXX/temperature-conversion.rexx
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/*REXX program converts temperature for: C, D, F, N, Ra, Re, Ro, and 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') /*a list of valid number thingys.*/
|
||||
n=z
|
||||
if _\==0 then do
|
||||
if _==1 then call serr 'illegal temperature:' z
|
||||
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=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,5)=='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('CENTESIMAL', uU) |,
|
||||
abbrev('CELSIUS' , uU) |,
|
||||
abbrev('CELCIUS' , uU) then F=n * 9/5 + 32
|
||||
when abbrev('DELISLE' , uU) then F=212 -(n * 6/5)
|
||||
when abbrev('FAHRENHEIT', uU) then F=n
|
||||
when abbrev('KELVIN' , uU) then F=n * 9/5 - 459.67
|
||||
when abbrev('NEWTON' , uU) then F=n * 60/11 + 32
|
||||
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
|
||||
end /*select*/
|
||||
say right(' ' x,55,"─")
|
||||
say Tform( ( F - 32 ) * 5/9 ) 'Celcius'
|
||||
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=''*/
|
||||
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────TFORM subroutine────────────────────*/
|
||||
Tform: procedure; showDig=8; _=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
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
[loop]
|
||||
input "Lelvin Degrees";kelvin
|
||||
if kelvin <= 0 then end ' zero or less ends the program
|
||||
celcius = kelvin - 273.15
|
||||
fahrenheit = kelvin * 1.8 - 459.67
|
||||
rankine = kelvin * 1.8
|
||||
print kelvin;" kelvin is equal to ";celcius; " degrees celcius and ";fahrenheit;" degrees fahrenheit and ";rankine; " degrees rankine"
|
||||
goto [loop]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
proc temps {k} {
|
||||
set c [expr {$k - 273.15}]
|
||||
set r [expr {$k / 5.0 * 9.0}]
|
||||
set f [expr {$r - 459.67}]
|
||||
list $k $c $f $r
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
puts -nonewline "Enter a temperature in K: "
|
||||
flush stdout
|
||||
lassign [temps [gets stdin]] k c f r
|
||||
puts [format "K: %.2f" $k]
|
||||
puts [format "C: %.2f" $c]
|
||||
puts [format "F: %.2f" $f]
|
||||
puts [format "R: %.2f" $r]
|
||||
10
Task/Temperature-conversion/XPL0/temperature-conversion.xpl0
Normal file
10
Task/Temperature-conversion/XPL0/temperature-conversion.xpl0
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
include c:\cxpl\codes;
|
||||
real K, C, F, R;
|
||||
[ChOut(0, ^K); K:= RlIn(0);
|
||||
C:= K - 273.15;
|
||||
ChOut(0, ^C); RlOut(0, C); CrLf(0);
|
||||
F:= 1.8*C + 32.0;
|
||||
ChOut(0, ^F); RlOut(0, F); CrLf(0);
|
||||
R:= F + 459.67;
|
||||
ChOut(0, ^R); RlOut(0, R); CrLf(0);
|
||||
]
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
10 REM Translation of traditional basic version
|
||||
20 INPUT "Kelvin Degrees? ";k
|
||||
30 IF k <= 0 THEN STOP: REM A value of zero or less will end program
|
||||
40 LET c = k - 273.15
|
||||
50 LET f = k * 1.8 - 459.67
|
||||
60 LET r = k * 1.8
|
||||
70 PRINT k; " Kelvin is equivalent to"
|
||||
80 PRINT c; " Degrees Celcius"
|
||||
90 PRINT f; " Degrees Fahrenheit"
|
||||
100 PRINT r; " Degrees Rankine"
|
||||
110 GO TO 20
|
||||
Loading…
Add table
Add a link
Reference in a new issue