-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
135 lines (120 loc) · 3.3 KB
/
main.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
/*
Temperature converter
-----------------
converts temperature between Celsius, Fahrenheit and Kelvin
Celsius to Fahrenheit: F = C * 9/5 + 32
Celsius to Kelvin: K = C + 273.15
Fahrenheit to Celsius: C = (F - 32) * 5/9
Fahrenheit to Kelvin: K = (F - 32) * 5/9 + 273.15
Kelvin to Celsius: C = K - 273.15
Kelvin to Fahrenheit: F = (K - 273.15) * 9/5 + 32
*/
// function prototypes
float celsius_to_fahrenheit(float celsius);
float celsius_to_kelvin(float celsius);
float fahrenheit_to_celsius(float fahrenheit);
float fahrenheit_to_kelvin(float fahrenheit);
float kelvin_to_celsius(float kelvin);
float kelvin_to_fahrenheit(float kelvin);
void start_converting();
float get_temperature(void);
bool convert_again(void);
int main(void)
{
do
{
start_converting();
} while (convert_again());
printf("Thank you for using temperature converter.\n");
return 0;
}
// functions definitions
float celsius_to_fahrenheit(float celsius)
{
float fahrenheit = celsius * 9 / 5.0 + 32;
return fahrenheit;
}
float celsius_to_kelvin(float celsius)
{
float kelvin = celsius + 273.15;
return kelvin;
}
float fahrenheit_to_celsius(float fahrenheit)
{
float celsius = (fahrenheit - 32) * 5 / 9.0;
return celsius;
}
float fahrenheit_to_kelvin(float fahrenheit)
{
float kelvin = (fahrenheit - 32) * 5 / 9.0 + 273.15;
return kelvin;
}
float kelvin_to_celsius(float kelvin)
{
float celsius = kelvin - 273.15;
return celsius;
}
float kelvin_to_fahrenheit(float kelvin)
{
float fahrenheit = (kelvin - 273.15) * 9 / 5.0 + 32;
return fahrenheit;
}
void start_converting()
{
float temperature = get_temperature();
printf("Choose the conversion you want to make:\n");
printf("1. Celsius to Fahrenheit\n");
printf("2. Celsius to Kelvin\n");
printf("3. Fahrenheit to Celsius\n");
printf("4. Fahrenheit to Kelvin\n");
printf("5. Kelvin to Celsius\n");
printf("6. Kelvin to Fahrenheit\n");
int choice;
do
{
choice = get_int("Enter your choice: ");
} while (choice < 1 || choice > 6);
switch (choice)
{
case 1:
printf("%.2f Celsius is %.2f Fahrenheit\n", temperature, celsius_to_fahrenheit(temperature));
break;
case 2:
printf("%.2f Celsius is %.2f Kelvin\n", temperature, celsius_to_kelvin(temperature));
break;
case 3:
printf("%.2f Fahrenheit is %.2f Celsius\n", temperature, fahrenheit_to_celsius(temperature));
break;
case 4:
printf("%.2f Fahrenheit is %.2f Kelvin\n", temperature, fahrenheit_to_kelvin(temperature));
break;
case 5:
printf("%.2f Kelvin is %.2f Celsius\n", temperature, kelvin_to_celsius(temperature));
break;
case 6:
printf("%.2f Kelvin is %.2f Fahrenheit\n", temperature, kelvin_to_fahrenheit(temperature));
break;
}
}
float get_temperature(void)
{
float temperature;
do
{
temperature = get_float("Enter temperature: ");
} while (temperature < -273.15); // absolute zero
return temperature;
}
bool convert_again(void)
{
char answer;
do
{
answer = get_char("Do you want to convert another temperature? (y/n): ");
answer = tolower(answer);
} while (answer != 'y' && answer != 'n');
return answer == 'y';
}