首页
学习
活动
专区
圈层
工具
发布

BMI计算
EN

Code Review用户
提问于 2012-05-18 08:25:20
回答 1查看 6.9K关注 0票数 5

我对C++非常陌生,我希望你们都能回顾一下我为一个程序编写的源代码,这个程序可以计算用户的体重指数,如果用户需要做额外的计算,可以将程序循环回起点。

代码语言:javascript
复制
#include <iostream>
#include <stdlib.h>
using namespace std;

float bmiNumber(float pounds, float inches) //BMI calculation
{
  return ((pounds/inches)*703);
  }

void chart(int mF)
{
     if(mF==2) {
 cout<<"\nHere is some info you might find useful...\n"
     <<"At the same BMI, women tend to have more body fat than men."<<endl
     <<"From cdc.gov\n"<<endl;
     }
}
void history()
{
 cout<<"Version History"<<endl;
 cout<<"\tVersion 1.0.1(05/18/2012)"<<endl
     <<"Optimized source code for better program flow,\nImproved calculation algorithms,\n"
     <<"Introduded looping capabilities for\nmultiple calculations without restarting the program."<<endl;
 cout<<"\tVersion 1.0.0(05/15/2012)"<<endl
     <<"Creation of the program, very rough outline of the basic structure."<<endl;
     system ("pause");
}

int main()
{
int changes;
int m;
m=1;

cout<<"Hello and welcome to the BMI Calculation Tool 1.0.1, or BMICT1.0.1 for short."<<endl;
cout<<"If you would like to see the changes from the previous version(1.0.0)\nplease press 3..."<<endl;
cout<<"If not press any other key.";
 cin>>changes;
 if(changes==3)
 {
               history();
               }
cout<<endl<<endl<<endl;               
cout<<"Why don't you first tell me a little bit about yourself,\nby answering just one simple question."<<endl;
cout<<"Are you a man or a woman?";


while(m==1)
{ 
cout<<"If you are a man, please press 1,\nif you are a woman, please press 2."<<endl;
cout<<"Please make your selection now..."<<endl;
 cin>>m;



 if(m==1)
 {
        cout<<"Our sources have indicated that you are in fact a man,\n" 
            <<"please check to confirm this and continue with the program"<<endl;
 }
 else if(m==2)
 {
     cout<<"Our sources have indicated that you are a woman,\n"
         <<"if this is true please continue with the program"<<endl;
}

  else
  {
      cout<<"You have entered an incorrect key..."<<endl;
      cout<<"The program will now close.\n";

      system ("pause");
      return 0;
}
      chart(m);

  float feet;
  float inch; 
  float totalinches;          
  cout<<"Please enter your height as follows..."<<endl;                 
  cout<<"Number of feet: "<<endl;
  cin>>feet;

  cout<<"\nNumber of inches: "<<endl;
  cin>>inch;

  totalinches=(feet*12)+inch;

 cout<<"Your total height in inches is: "<<totalinches<<endl;  
 float inchSqd;
 float lbs;
 float BMI;
 inchSqd=(totalinches*totalinches);
 cout<<"Please enter your weight in pounds: \n";
 cin>>lbs;
 cout<<"Okay, we will now calculate your BMI\n";            
 BMI=bmiNumber(lbs, inchSqd);                                    
  cout<<endl
  <<"Your Body Mass Index Number is: "<<BMI<<endl;

 cout<<endl<<"Once again your BMI is "<<BMI<<", and here is a chart for review."<<endl;
 cout<<endl
     <<     "\tWeight Status"<<endl
     <<" Below 18.5     Underweight"<<endl
     <<" 18.5 to 24.9   Normal"<<endl
     <<" 25.0 to 29.9   Overweight"<<endl
     <<" 30.0 and Above Obese"<<endl;
 cout<<"If you would like to make another calculation press 1, if not press 2..."<<endl;
 cin>>m;
 }

 cout<<endl<<"Thank you for using the BMICT1.0.1 and have a great day.\n";

system ("pause"); 
return 0;
}
EN

回答 1

Code Review用户

发布于 2014-11-02 19:28:37

我尽量不要重复太多@Konrad Rudolph的观点:

  • 这里非常重要:清理凹痕。它到处都是,让你的代码很难为别人阅读。我甚至不知道你想要使用的缩进程度,但不管是哪一种,只要坚持那个。
  • 703看起来像一个神奇的数字,应该是一个常量(使用const)。这将允许您在任何地方使用此值,而该值在此程序中的使用将是已知的。
  • chart()似乎没有做任何有用的事情,它的名称在这里也没有多大意义,所以可以删除它。函数不应该只打印一些硬编码的文本,因为这不被认为是有用的工作。
  • 您正在混合std::endl"\n",但是您确实需要后者,因为不需要特殊的刷新。
  • 请注意,system("PAUSE")是纯Windows的,因此是不可移植的.如果您想要进行可移植的暂停,请执行类似于std::cin.get()的操作。它确实有一些不同的工作方式,但在这里使用仍然更好。

总的来说,由于缩进,仍然很难对此进行回顾,而且这里可能还有更多的内容可供讨论。把注意力集中在清理这件事上,它应该看起来更好。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/11860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档