C语言入门项目——BMI指数计算器

本文带你分析简单代码片段。

/*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;
  //判断体重与身高是否>0
  if (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.h
  system("cls");//清屏stdlib.h
  system("pause");//暂停
  return 0;
}

本文代码仓库:https://gitee.com/linshu-gitee/bmi-calculator

猜你喜欢

转载自blog.csdn.net/weixin_56810624/article/details/129105292
今日推荐