如何用C语言和Python编写一个BMI指数计算器 您所在的位置:网站首页 c语言计算bmi值 如何用C语言和Python编写一个BMI指数计算器

如何用C语言和Python编写一个BMI指数计算器

2024-03-28 05:57| 来源: 网络整理| 查看: 265

写在前面

  前些日子在学习C语言和Python的循环语句的时候,我心血来潮,想编写一个实用性较高的程序。想来想去,我决定编写一个BMI指数计算器。接下来,我将分享一下我编写这个程序的过程,希望给大家带来一些启发。

BMI指数计算器1.0

  首先,我们先来编写BMI指数计算器的核心部分。我们对这个程序进行分析,可以知道这个程序需要用户输入自己的身高(m)和体重(kg)值,再用体重除以身高的平方计算出BMI,最后使用printf()将其打印输出。话不多说,我们现在就开始吧。   首先我们使用Python 3.6来编写它:

print('**********BMI指数计算器**********') weight = float(input('Please input your weight by kilograms: ')) height = float(input('Please input your height by metres: ')) BMI = weight / (height**2) print('Your BMI is %.1f. ' %BMI) if(BMI < 18.5): print('偏轻') elif(BMI = 18.5): print('正常') elif(BMI >=24 and BMI = 28): print('肥胖')

  使用C语言编写它:

#include int main (void) { printf ("********************BMI指数计算器********************\n"); float weight, height, BMI; char a[0]; printf ("Please input your weight by kilograms: "); scanf ("%f", &weight); printf ("Please input your height by metres: "); scanf ("%f", &height); BMI = weight / (height * height); printf ("Your BMI is %.1f. \n", BMI); if (BMI < 18.5) { printf ("偏轻\n"); } else if (BMI >= 18.5 && BMI = 24 && BMI = 28) { printf ("肥胖\n"); } return 0; } 改进——BMI指数计算器2.0

  我们不难发现,在运行这个程序的时候,程序在计算一次之后就会结束运行。结束一次计算之后倘若还想继续新一次计算,我们需要再重新运行它。这在实际应用的时候是没有可操作性的。针对这个问题,我们来进行一次改进。   相信大多数人会想到while循环语句,我们不妨试一试。   下面是用Python 3.6编写的改进版程序。

print('**********BMI指数计算器**********') while True: weight = float(input('Please input your weight by kilograms: ')) height = float(input('Please input your height by metres: ')) BMI = weight / (height**2) print('Your BMI is %.1f. ' %BMI) if(BMI < 18.5): print('偏轻') elif(BMI = 18.5): print('正常') elif(BMI >=24 and BMI = 28): print('肥胖')

  我们用C语言也同样可以实现。

#include int main (void) { printf ("********************BMI指数计算器********************\n"); while (1) { float weight, height, BMI; printf ("Please input your weight by kilograms: "); scanf ("%f", &weight); printf ("Please input your height by metres: "); scanf ("%f", &height); BMI = weight / (height * height); printf ("Your BMI is %.1f. \n", BMI); if (BMI < 18.5) { printf ("偏轻\n"); } else if (BMI >= 18.5 && BMI = 24 && BMI = 28) { printf ("肥胖\n"); } } return 0; } 新的问题与解决方案

  改进版的程序解决了上面提出的只能进行一次计算的问题,这很好。但新的问题又出现了——这个循环会一直进行下去。当我们想要结束计算的时候,我们没有可以退出程序的方法(除非我们强制关闭这个程序)。这又该怎么办呢?   我想到的解决方案是在C程序中使用if语句。一次计算结束后,我们可以用printf()来输出“是否要继续”的字样。在用户键入“Y”之后,程序会执行continue继续下一次运行;相反地,如果键入的是“N”,程序执行break退出循环。当然了,我们还要考虑到用户不小心按错“Y”或者“N”的情况。   改进Python程序的思路和改进C程序的思路类似。

再一次改进——BMI指数计算器3.0

  既然有了想法,让我们动手做起来,写出完美无缺的程序吧!   我们先对Python程序进行改进,使它成为完美的最终版本。

print('**********BMI指数计算器**********') while True: weight = float(input('Please input your weight by kilograms: ')) height = float(input('Please input your height by metres: ')) BMI = weight / (height**2) print('Your BMI is %.1f. ' %BMI) if(BMI < 18.5): print('偏轻') elif(BMI = 18.5): print('正常') elif(BMI >=24 and BMI = 28): print('肥胖') a = input('是否要继续?(Y/N)') if(a == 'Y'): continue elif(a == 'N'): break else: print('Error! \n') break

  下面是C语言编写的BMI指数计算器3.0最终版。

#include int main (void) { printf ("********************BMI指数计算器********************\n"); while (1) { float weight, height, BMI; char a[0]; printf ("Please input your weight by kilograms: "); scanf ("%f", &weight); printf ("Please input your height by metres: "); scanf ("%f", &height); BMI = weight / (height * height); printf ("Your BMI is %.1f. \n", BMI); if (BMI < 18.5) { printf ("偏轻\n"); } else if (BMI >= 18.5 && BMI = 24 && BMI = 28) { printf ("肥胖\n"); } printf ("是否要继续?(Y/N)"); scanf ("%s", a); if (a[0] == 'Y') continue; else if (a[0] == 'N') break; else { printf ("Error! \n"); } break; } return 0; } 写在最后

  希望我的这篇文章可以给大家带来一些启发。如果文章有一些错误,或者是有什么意见和建议的话,欢迎联系我。大家一起学习,共同进步!   我的邮箱:[email protected]



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有