반응형

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
/* 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 };
 
// Type Male, Female validity
int classifyType(int n, const char t[]) {
    if (n == 1)
        if (t[0== 'm' || t[0== 'M'return MEN;
        if (t[0== 'w' || t[0== 'W'return WOMEN;
    return INVALID;
}
 
// Convert type to integer
int convertType(int n, const char t[]) {
    return classifyType(n, t);
}
 
// Check height, weight input
bool isValid(int n, const char input[]) {
    if (n > 8return false;
    int count = 0;
    for (int i = 0; input[i] != '\0'; i++) {
        if (!isdigit(input[i])) {
            if (input[i] == '.') {
                count += 1;
                if (count > 1return 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;
}
 
// 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.8return Severe_Underweight;
    if (weight <= stdWeight * 0.9return Underweight;
    if (weight <= stdWeight * 1.1return Normal;
    if (weight <= stdWeight * 1.2return 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(0178.11);
    printf("%.4f\n", stdWeight);
    stdWeight = getStdWeight(0120);
    printf("%.4f\n", stdWeight);
    stdWeight = getStdWeight(0350);
    printf("%.4f\n", stdWeight);
    stdWeight = getStdWeight(1157.11);
    printf("%.4f\n", stdWeight);    
    stdWeight = getStdWeight(1130);
    printf("%.4f\n", stdWeight);
    stdWeight = getStdWeight(1155);
    printf("%.4f\n", stdWeight);
}
 
void test() {
    testHeightValid();
    printf("Test Height, Weight validity passed\n");
    
    testStdWeight();
    printf("Test Standard Weight passed\n");
}
// ---------------------------------------------------------------------------------------------
 
// Run
int main(int n, char *args[n]) {
    setbuf(stdout, NULL);
    if (n == 1) {
        test();
        printf("\n");
        printf("Try: .\\standardweight3 Sex (m, w) Height (cm) Weight (kg)\n");
        printf("ex.  .\\standardweight3 m 178.12 70.45\n");
        return 0;
    }
 
    // Get type from input
    int type = convertType(strlen(args[1]), args[1]);
    if (type == INVALID) {
        printf("Invalid Sex type !!\n");
        printf("ex.  .\\standardweight3 m 178.12 70.45\n");
        return 1;
    }
 
    // Get height, weight from input
    double height = convert(strlen(args[2]), args[2]);
    double weight = convert(strlen(args[3]), args[3]);
 
    if (!isHeightValid(height) || !isWeightValid(weight)) {
        printf("Invalid Height or Weight !!\n");
        printf("120 < Height (cm) < 350\n");
        printf(" 30 < Weight (kg) < 350\n");
        return 1;
    }
 
    // 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);
}
cs

 

 

반응형