-
Notifications
You must be signed in to change notification settings - Fork 7
/
TempConverter.cs
110 lines (102 loc) · 3.44 KB
/
TempConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Linq;
using System.Collections;
class Converter{
static void Main(string[] args){
Console.WriteLine("<--------Temperature Converter-------->");
Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)");
Console.WriteLine("Enter temperature below. For example 50 C or 24,2 F.");
Hashtable tempInfo = GetTemp();
char scale = ChooseScale((char)tempInfo["scale"]);
string convertedTemp = Convert(float.Parse(tempInfo["value"].ToString()), (char) tempInfo["scale"], scale);
Console.WriteLine("Result: " + convertedTemp);
}
static string Convert(float temp, char scale, char choosenScale){
float convertedTempValue;
string convertedTemp = "";
//If choosen scale is equal to temp scale, it doesn't need to convert.
if(scale == choosenScale){
convertedTemp = temp.ToString() + ' ' + scale;
return convertedTemp;
}
//I used switch to show how it works, nobody use switch anymore :(
switch (scale){
//Convert Fahrenheit.
case 'F':
//Convert Fahrenheit to Celsius.
convertedTempValue = (temp - 32f)/1.8f;
if (choosenScale == 'C')
convertedTemp = convertedTempValue.ToString() + " C";
else
//Add 253.15 to make Kelvin from Celsius.
convertedTemp = (convertedTempValue + 253.15f).ToString() + " K";
break;
//Convert Kelvin.
case 'K':
// Convert to Celsius.
convertedTempValue = temp - 253.13f;
if (choosenScale == 'C')
convertedTemp = convertedTempValue.ToString() + " C";
else{
//Convert to Fahrenheit.
convertedTempValue = convertedTempValue*1.8f + 32f;
convertedTemp = convertedTempValue.ToString() + " F";
}
break;
//Convert Celsius.
case 'C':
//Convert to Kelvin
if (choosenScale == 'K'){
convertedTempValue = temp + 253.15f;
convertedTemp = convertedTempValue.ToString() + " K";
} else {
convertedTempValue = temp*1.8f + 32f;
convertedTemp = convertedTempValue.ToString() + " F";
}
break;
//It is always good to make default, just in case.
default:
Console.WriteLine("Wrong option.");
break;
}
return convertedTemp;
}
static char ChooseScale(char originScale){
Console.Write("1. Kelvin\n2. Celsius\n3. Fahrenheit\nChoose scale: ");
char numConvertScale = char.Parse(Console.ReadLine());
char convertScale = '0';
// Change number to char.
if (numConvertScale == '1')
convertScale = 'K';
else if (numConvertScale == '2')
convertScale = 'C';
else if (numConvertScale == '3')
convertScale = 'F';
return convertScale;
}
static Hashtable GetTemp(){
Hashtable tempHashtable = new Hashtable();
float tempValue = 0;
string[] temp;
//Check if temperature format is valid.
while (true){
string input = Console.ReadLine();
temp = input.Split(' ');
if(temp.Length == 2){
if(new [] {"F", "K", "C"}.Contains(temp[1])){
//Check if first value is int
try{
tempValue = float.Parse(temp[0]);
break;
}catch{
Console.WriteLine("Not a valid number");
}
}
}
Console.WriteLine("Wrong data");
}
tempHashtable.Add("scale", char.Parse(temp[1]));
tempHashtable.Add("value", tempValue);
return tempHashtable;
}
}