반응형
C언어 비만도 (BMI) 계산 프로그램
키 (m), 몸무게 (㎏) 터미널로 문자열 입력하여 BMI 계산 및 분류
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
|
/* Classify BMI */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
// Use Integer constants to represent classes BMI
enum { Underweight, Normal, Overweight, Obese, Invalid };
// Check input
bool isValid(int n, const char input[]) {
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 invalid height or weight
bool isInvalid(double height, double weight) {
bool b = (height >= 3.5 || height <= 0.2 || weight >= 350 || weight <= 15);
return b;
}
// Calculate BMI: weight(kg) ÷ height²(m)
double getBmi(double height, double weight) {
double bmi;
if (isInvalid(height, weight)) bmi = -1;
else bmi = weight / (height * height);
return bmi;
}
// Classify BMI
int classifyBmi(double bmi) {
if (bmi < 0) return Invalid;
if (bmi < 18.5) return Underweight;
if (bmi < 25) return Normal;
if (bmi < 30) return Overweight;
else return Obese;
}
// Print if input is invalid
void printInvalid(int n) {
if (n == -1) printf("Invalid input!!\n");
}
// Print BMI, BMI class
void print(double bmi, int bmiClass) {
switch (bmiClass) {
case Obese: printf("BMI: %.4f - Obese\n", bmi); break;
case Overweight: printf("BMI: %.4f - Overweight\n", bmi); break;
case Normal: printf("BMI: %.4f - Normal\n", bmi); break;
case Underweight: printf("BMI: %.4f - Underweight\n", bmi); break;
case Invalid: printInvalid(bmi); break;
}
}
// Print height, weight
void printHW(double height, double weight) {
printf("Height: %.4f (m), Weight: %.4f (kg)\n", height, weight);
}
// Print height, weight, BMI, BMI class
void printResult(double height, double weight, double bmi, int bmiClass) {
switch (bmiClass) {
case Invalid: printInvalid(bmi); break;
case Obese:
printHW(height, weight);
printf("BMI: %.4f - Obese\n", bmi);
break;
case Overweight:
printHW(height, weight);
printf("BMI: %.4f - Overweight\n", bmi);
break;
case Normal:
printHW(height, weight);
printf("BMI: %.4f - Normal\n", bmi);
break;
case Underweight:
printHW(height, weight);
printf("BMI: %.4f - Underweight\n", bmi);
break;
}
}
// Get string input
void getInputs(char height[], char weight[]) {
printf("Input Height (m): ");
scanf("%s", height);
printf("Input Weight (kg): ");
scanf("%s", weight);
}
int main() {
setbuf(stdout, NULL);
char h[20]; // Array for string input height
char w[20]; // Array for string input weight
getInputs(h, w); // Get input from terminal
// Convert and get height, weight
double height = convert(strlen(h), h);
double weight = convert(strlen(w), w);
// Get BMI rating, class from height, weight
double bmi = getBmi(height, weight);
int bmiClass = classifyBmi(bmi);
// Print result
printResult(height, weight, bmi, bmiClass);
}
|
cs |
반응형
'C 언어 > C언어 기초' 카테고리의 다른 글
[C언어 #36] 구조체 (struct) 함수 활용 (0) | 2020.07.07 |
---|---|
[C언어 #35] 구조체 (struct) (0) | 2020.07.07 |
[C언어 #34] 10진수를 2진수로 변환 (decimal and binary conversion) (0) | 2020.07.03 |
[C언어 #33] Segfault (Segmentation Fault) (0) | 2020.07.03 |
[C언어 #32] 다른 문자열 포함 여부 (0) | 2020.07.01 |