-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.c
188 lines (173 loc) · 5.09 KB
/
conversions.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <ctype.h>
// checks for initial invalid input
void invalidInput(char userInput) {
if (isalpha(userInput) == 0) {
printf("Invalid formatting. Ending program.\n");
} else {
printf("Unknown conversion type %c chosen. Ending program.\n ", userInput);
}
}
// uses inch as standard unit for conversions
double inchToUnit(double distance, char newUnit) {
double conversion;
switch (newUnit) {
case 'I':
conversion = distance;
break;
case 'F':
conversion = distance / 12;
break;
case 'Y':
conversion = distance / 36;
break;
case 'M':
conversion = distance / 63360;
break;
default:
conversion = distance;
break;
}
return conversion;
}
// converts distances to inches and calls inchToUnit() for final conversion
double distUnitConvert(char initialUnit, char newUnit, double distance) {
double newDistance;
switch (initialUnit) {
case 'I':
newDistance = inchToUnit(distance, newUnit);
return newDistance;
break;
case 'F':
newDistance = inchToUnit(distance * 12, newUnit);
return newDistance;
break;
case 'Y':
newDistance = inchToUnit(distance * 36, newUnit);
return newDistance;
break;
case 'M':
newDistance = inchToUnit(distance * 63360, newUnit);
break;
default:
newDistance = distance;
break;
}
return newDistance;
}
// checks for errors and prints results
void distanceConversion() {
char initialUnit;
char newUnit;
double distance;
printf("Enter the distance followed by its suffix (I,F,Y,M): ");
scanf(" %lf %c", &distance, &initialUnit);
initialUnit = toupper(initialUnit);
if ((initialUnit == 'I' || initialUnit == 'F' || initialUnit == 'Y' || initialUnit == 'M') && distance != 0) {
printf("Enter the new unit type (I,F,Y,M): ");
scanf(" %c", &newUnit);
newUnit = toupper(newUnit);
if ((newUnit == 'I' || newUnit == 'F' || newUnit == 'Y' || newUnit == 'M')) {
printf("%.2lf%c is %.2lf%c\n", distance, initialUnit, distUnitConvert(initialUnit, newUnit, distance), newUnit);
} else {
printf("%c is not a valid distance type. Ending program.\n", newUnit);
}
} else if (initialUnit == '\0') {
printf("Invalid formatting. Ending program.\n");
} else {
printf("%c is not a valid distance type. Ending program.\n", initialUnit);
}
}
// uses fahrenheit as standard unit
double fahrenheitToUnit(double temperature, char newUnit) {
double conversion;
switch (newUnit) {
case 'F':
conversion = temperature;
break;
case 'C':
conversion = (temperature - 32) * 5 / 9;
break;
case 'K':
conversion = (temperature - 32) * 5 / 9 + 273.15;
break;
default:
conversion = temperature;
break;
}
return conversion;
}
// converts units to fahrenheit and uses fahrenheitToUnit() for final conversion
double tempUnitConvert(char initialUnit, char newUnit, double temperature) {
double newTemperature;
switch (initialUnit) {
case 'F':
newTemperature = fahrenheitToUnit(temperature, newUnit);
break;
case 'C':
newTemperature = fahrenheitToUnit(temperature * 9 / 5 + 32, newUnit);
break;
case 'K':
newTemperature = fahrenheitToUnit((temperature - 273.15) * 9 / 5 + 32, newUnit);
break;
default:
newTemperature = temperature;
break;
}
return newTemperature;
}
// checks for errors and prints results
void temperatureConversion() {
char initialUnit;
char newUnit;
double temperature;
printf("Enter the temperature followed by its suffix (F, C, or K): ");
scanf(" %lf %c", &temperature, &initialUnit);
initialUnit = toupper(initialUnit);
if (initialUnit == 'F' || initialUnit == 'C' || initialUnit == 'K') {
printf("Enter the new unit type (F, C, or K): ");
scanf(" %c", &newUnit);
newUnit = toupper(newUnit);
if (newUnit == 'F' || newUnit == 'C' || newUnit == 'K') {
printf("%.2lf%c is %.2lf%c\n", temperature, initialUnit,
tempUnitConvert(initialUnit, newUnit, temperature), newUnit);
} else {
printf("%c is not a valid temperature type. Ending program.\n", newUnit);
}
} else if (initialUnit == '\0') {
printf("%c is not a valid temperature type. Ending program.\n", initialUnit);
} else {
printf("Invalid formatting. Ending program.\n");
}
}
// determines whether user picked distance, temperature, or neither
void distanceOrTemperature(char userInput) {
switch (userInput) {
case 'D':
distanceConversion();
break;
case 'T':
temperatureConversion();
break;
default:
invalidInput(userInput);
break;
}
}
// gathers initial user input
char getUserInput() {
char userInput;
printf("Pick the type of conversion that you would like to do.\n");
printf("T or t for temperature\n");
printf("D or d for distance\n");
printf("Enter your choice: ");
scanf(" %c", &userInput);
userInput = toupper(userInput);
return userInput;
}
// calls other functions
int main() {
char userInput = getUserInput();
distanceOrTemperature(userInput);
return 0;
}