-
Notifications
You must be signed in to change notification settings - Fork 330
/
AllTempScalesConv.c
98 lines (90 loc) · 2.95 KB
/
AllTempScalesConv.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
#include <stdio.h>
#include <stdlib.h>
// Function that performs the conversion
double convertTemp(double initValue, int initScale, int finalScale){
double finalValue;
switch(initScale){
// Celsius
case 1:
// Celsius to Kelvin
if(finalScale == 1){
finalValue = initValue + 273.15;
}
// Celsius to Fahrenheit
else if(finalScale == 2){
finalValue = (initValue * 9 / 5) + 32;
}
break;
case 2:
// Kelvin to Celsius
if(finalScale == 1){
finalValue = initValue - 273.15;
}
// Kelvin to Fahrenheit
else if(finalScale == 2){
finalValue = ((initValue - 273.15) * 9 / 5) + 32;
}
break;
case 3:
// Fahrenheit to Celsius
if(finalScale == 1){
finalValue = (initValue - 32) * 5 / 9;
}
// Fahrenheit to Kelvin
else if(finalScale == 2){
finalValue = ((initValue - 32) * 5 / 9) + 273,15;
}
break;
}
return finalValue;
}
int main(){
int option;
double initialValue, finalValue;
while(1){
// main menu
printf("\n0 - Exit\n");
printf("1 - Convert from Celsius to Kelvin\n");
printf("2 - Convert from Celsius to Fahrenheit\n");
printf("3 - Convert from Kelvin to Fahrenheit\n");
printf("4 - Convert from Kelvin to Celsius\n");
printf("5 - Convert from Fahrenheit to Celsius\n");
printf("6 - Convert from Fahrenheit to Kelvin\n");
printf("Select a number: ");
scanf("%d",&option);
if(!option){
printf("Ending program\n");
return 0;
}
printf("Please enter the initial value: ");
scanf("%lf",&initialValue);
switch(option){
case 1:
finalValue = convertTemp(initialValue,1,1);
printf("Valor em Kelvin: %.2lf",finalValue);
break;
case 2:
finalValue = convertTemp(initialValue,1,2);
printf("Valor em Fahrenheit: %.2lf",finalValue);
break;
case 3:
finalValue = convertTemp(initialValue,2,1);
printf("Valor em Celsius: %.2lf",finalValue);
break;
case 4:
finalValue = convertTemp(initialValue,2,2);
printf("Valor em Fahrenheit: %.2lf",finalValue);
break;
case 5:
finalValue = convertTemp(initialValue,3,1);
printf("Valor em Celsius: %.2lf",finalValue);
break;
case 6:
finalValue = convertTemp(initialValue,3,1);
printf("Valor em Kelvin: %.2lf",finalValue);
break;
}
printf("\n");
}
return 0;
}