-
-
Save 173210/a2a6ea0d3e96f1d85f50 to your computer and use it in GitHub Desktop.
BMI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<limits.h>//in gcc | |
#include<errno.h>//in gcc | |
#include<math.h> | |
double get_double_num(const double max, const double min){ | |
char s[200]; | |
if (NULL == fgets(s, 200, stdin)){ | |
if (feof(stdin)){ | |
return EOF; | |
} | |
size_t i; | |
for(i = 0; i < 100 && '\0' == s[i]; i++); | |
if('\n' != s[i - 1]) while(getchar() != '\n'); | |
return -2; | |
} | |
errno = 0; | |
const double t = strtod(s, NULL); | |
if (0 != errno || t < min || max < t) | |
return -2; | |
return t; | |
} | |
double get_num_dicition(const double max, const double min){ | |
if (max < min) return -1; | |
double flag0; | |
bool temp_judge; | |
do{ | |
flag0 = get_double_num(max, min); | |
temp_judge = (-2 == flag0); | |
if (temp_judge){ | |
puts("再入力してください。"); | |
} | |
} while (temp_judge); | |
return flag0; | |
} | |
double get_hight(){ | |
//身長入力関数 | |
double height_cm;//cmの身長 | |
double height_m;//mに直した身長 | |
printf("身長(cm)を入力してください。\n"); | |
height_cm = get_num_dicition(251,0); | |
height_m = height_cm/100; | |
printf("%5.2fcmで入力されました。\n\n",height_cm); | |
return height_m; | |
} | |
double get_weight(){ | |
//体重入力関数 | |
double weight;//kgの体重 | |
printf("体重(kg)を入力してください。\n"); | |
weight = get_num_dicition(700, 0); | |
printf("%5.2fkgで入力されました。\n\n",weight); | |
return weight; | |
} | |
int bmi_calculation (){ | |
//BMI計算関数 | |
double bmi_answer = get_weight()/(pow(get_hight(),2.0));//BMIを求める公式 | |
printf("あなたのBMI指数は%3.2fです。\n",bmi_answer); | |
return bmi_answer; | |
} | |
int main(){ | |
printf("あなたの身長(cm)と体重(kg)からBMI指数をだし、あなたが肥満かそうではないかを判定します。\n"); | |
double bmi = bmi_calculation(); | |
printf("つまりあなたは"); | |
if(bmi < 18.5){ | |
printf("低体重です。\n"); | |
} | |
else if(bmi < 25){ | |
printf("普通体重です。\n"); | |
} | |
else if(bmi < 30){ | |
printf("肥満(1度)です。\n"); | |
} | |
else if(bmi < 35){ | |
printf("肥満(2度)です。\n"); | |
} | |
else if(bmi < 40){ | |
printf("肥満(3度)です。\n"); | |
} | |
else if(bmi >= 40){ | |
printf("肥満(4度)です。\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment