반응형
c언어 표준체중 계산하여 구하기 및 비만도 분류
Calculate standard weight and classify obesity
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/* Classify Obesity */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
// Use integer constants to represent classes
enum type { MEN, WOMEN, INVALID };
enum obesity { Severe_Underweight, Underweight, Normal, Overweight, Obese, Invalid };
// Get input for type
void inputType(const char t[]) {
printf("Input Type\n");
printf("[1] Male \n");
printf("[2] Female \n>>> ");
scanf("%s", t);
}
// Get string input height
void getInputHeight(char height[]) {
printf("Input Height (cm): ");
scanf("%s", height);
}
// Get string input weight
void getInputWeight(char weight[]) {
printf("Input Weight (kg): ");
scanf("%s", weight);
}
// Type Male, Female validity
bool isValidType(int n, const char t[]) {
if (n == 1)
if (t[0] == '1' || t[0] == '2') return true;
return false;
}
// Convert type to integer
int convertType(int n, const char t[]) {
return isValidType(n, t) ? atoi(t) : -1;
}
// Classify type Male, Female
int classifyType(int type) {
if (type == -1) return INVALID;
if (type == 1) return MEN;
if (type == 2) return WOMEN;
}
// Get Male or Female
int getType() {
char t[20]; // Array for string input type
int type;
while (true) {
inputType(t); // Get input type from terminal
type = convertType(strlen(t), t);
if (type != -1) break;
}
type = classifyType(type);
return type;
}
// Check height, weight input
bool isValid(int n, const char input[]) {
if (n > 8) return false;
int count = 0;
for (int i = 0; input[i] != '\0'; i++) {
if (!isdigit(input[i])) {
if (input[i] == '.') {
count += 1;
if (count > 1) return false;
} else {
return false;
}
}
}
return true;
}
// Convert string to double
double convert(int n, const char input[]) {
return isValid(n, input) ? atof(input) : -1;
}
// Check height validity
bool isHeightValid(double height) {
bool b = height <= 350 && height >= 120;
return b;
}
// Check weight validity
bool isWeightValid(double weight) {
bool b = weight <= 350 && weight >= 30;
return b;
}
// Get height
double getHeight() {
char h[20];
double height;
while (true) {
getInputHeight(h);
height = convert(strlen(h), h);
if (isHeightValid(height)) break;
}
return height;
}
// Get weight
double getWeight() {
char w[20];
double weight;
while (true) {
getInputWeight(w);
weight = convert(strlen(w), w);
if (isWeightValid(weight)) break;
}
return weight;
}
// Calculate standard weight
double getStdWeight(int type, double height) {
int stdWeight;
if (type == MEN) stdWeight = (height - 80) * 0.7;
if (type == WOMEN) stdWeight = (height - 70) * 0.6;
if (type == INVALID) stdWeight = -1;
return stdWeight;
}
// Classify obesity using weight and standard weight
int classifyObesity(double weight, double stdWeight) {
if (weight <= stdWeight * 0.8) return Severe_Underweight;
if (weight <= stdWeight * 0.9) return Underweight;
if (weight <= stdWeight * 1.1) return Normal;
if (weight <= stdWeight * 1.2) return Overweight;
else return Obese;
}
// Print result
void printResult(int type, double height, double weight, double stdWeight, int obesity) {
switch (obesity) {
case Severe_Underweight:
printf("You are 'Severly Underweight' !!\n");
break;
case Underweight:
printf("You are 'Underweight' !!\n");
break;
case Normal:
printf("You are 'Normal' !!\n");
break;
case Overweight:
printf("You are 'Overweight' !!\n");
break;
case Obese:
printf("You are 'Obese' !!\n");
break;
}
switch (type) {
case MEN:
printf("Sex: Male, ");
break;
case WOMEN:
printf("Sex: Female, ");
break;
}
printf("Height: %.2f (cm), Weight: %.2f (kg), Standard Weight: %.1f (kg)\n", height, weight, stdWeight);
}
// ---------------------------------------------------------------------------------------------
// Testing
void testHeightValid() {
if (isHeightValid(70.11)) printf("true\n");
else printf("false\n");
if (isHeightValid(17)) printf("true\n");
else printf("false\n");
if (isHeightValid(1)) printf("true\n");
else printf("false\n");
if (isHeightValid(350)) printf("true\n");
else printf("false\n");
if (isHeightValid(177.78)) printf("true\n");
else printf("false\n");
if (isHeightValid(-1)) printf("true\n");
else printf("false\n");
}
void testStdWeight() {
double stdWeight;
stdWeight = getStdWeight(0, 178.11);
printf("%.4f\n", stdWeight);
stdWeight = getStdWeight(0, 120);
printf("%.4f\n", stdWeight);
stdWeight = getStdWeight(0, 350);
printf("%.4f\n", stdWeight);
stdWeight = getStdWeight(1, 157.11);
printf("%.4f\n", stdWeight);
stdWeight = getStdWeight(1, 130);
printf("%.4f\n", stdWeight);
stdWeight = getStdWeight(1, 155);
printf("%.4f\n", stdWeight);
}
void test() {
printf("Test Height, Weight validity\n");
testHeightValid();
printf("Test Standard Weight\n");
testStdWeight();
}
// ---------------------------------------------------------------------------------------------
void execute() {
// Get type from input
int type = getType();
// Get height, weight from input
double height = getHeight();
double weight = getWeight();
// Get standard weight
double stdWeight = getStdWeight(type, height);
// Get Obesity from height, weight
int obesityClass = classifyObesity(weight, stdWeight);
// Print result (sex, height, weight, standard weight, obesity)
printResult(type, height, weight, stdWeight, obesityClass);
}
// Run
int main() {
setbuf(stdout, NULL);
// test();
execute();
}
|
cs |
반응형
'C 언어 > C언어 기초' 카테고리의 다른 글
[C언어 #41] 배열에 입력한 숫자의 합 (0) | 2020.07.12 |
---|---|
[C언어 #40] 표준체중 비만도 (obesity) 구하기 (0) | 2020.07.11 |
[C언어 #39] 포인터 (pointer) 로 구조체(structure) 활용 (0) | 2020.07.08 |
[C언어 #38] 포인터 (pointer) segfault (0) | 2020.07.07 |
[C언어 #37] 포인터 (pointer) (0) | 2020.07.07 |