本文带你分析简单代码片段。
/*UTF-8*///BMI.h#include<stdio.h>#include<math.h> #include<stdlib.h> #include<windows.h>double Height;double Weight; double Height_Square;double BMI;
这里包含了头文件,声明了变量。
/*UTF-8*///BMI.c#include"BMI.h"int main(void){printf("您的身高(米)?\n");scanf("%lf",&Height); printf("您的体重(公斤)?\n");scanf("%lf",&Weight);//计算BMI指数Height_Square = pow(Height,2); //pow函数在math.h头文件中 BMI = Weight / Height_Square;//判断体重与身高是否>0if (Height > 0 && Weight > 0){printf("您的BMI指数:%.2f。", BMI);if (BMI < 18.5){printf("您偏瘦。");}else if (BMI <= 24.9){printf("您的BMI指数正常。");}else{printf("您超重。");}}else{printf("输入的数值必须大于0。三秒后暂停。\n");}//我们希望窗口不要立即关闭Sleep(3000);//延迟三秒 头文件windows.hsystem("cls");//清屏stdlib.hsystem("pause");//暂停return 0;}
本文代码仓库:/linshu-gitee/bmi-calculator