首页 编程C/C++一个计算标准体重的小程序

一个计算标准体重的小程序

匆匆忙忙,弄了一个计算身高的程序。实现这么简单的功能不需要100多行,这样写趁机熟悉一下C++的对象。当然程序还可以优化。不高兴写了,这个东西再写下去有点儿无聊。

#include <iostream>
//这是一个简单的程序,用来测试身材,用指针操作对象;

using namespace std;

enum Choice{
ShowMenu=1,
SetInfo,
ShowCalc,
Help,
Quit
};

class Human
{
public:
Human(int Age,double Height,double Weight);
~Human();
void SetAge(int Age){ItsAge=Age;}
void SetHeight(double Height) {ItsHeight=Height;}
void SetWeight(double Weight) {ItsWeight=Weight;}
void SetSex(int Sex) {ItsSex=Sex;}
int GetAge() const {return ItsAge;}
double GetHeight() const {return ItsHeight;}
double GetWeight() const {return ItsWeight;}
int GetSex() const {return ItsSex;}
private:
int ItsAge,ItsSex;
double ItsHeight;
double ItsWeight;

};

Human::Human(int a,double h,double w){
ItsAge=a;
ItsHeight=h;
ItsWeight=w;
}

Human::~Human(){}

Human *PM;

int DoMenu();
void DoInput();
void DoCalc();
void DoInfo();
void DoHelp();

void main(){

PM = new Human(24,172,86);

int Choice=ShowMenu;

int FQuit=false;

while(!FQuit)
{
int Choice=DoMenu();
if (Choice<ShowMenu || Choice>Quit){
cout<<“对不起,您的选择超出菜单范围,请重新输入…”<<endl;
continue;
}

switch (Choice)
{
case ShowMenu:
DoMenu();
break;

case SetInfo:
DoInput();
break;

case ShowCalc:
DoCalc();
break;

case Help:
DoHelp();
break;

case Quit:
FQuit=true;
cout<<“正在退出。。。”;
break;
}

}
delete PM;
PM=0;
}

void DoInput(){
int A,S;
double H,W;
cout<<“请输入您的年龄:”;
cin>>A;
cout<<“请输入您的身高(CM):”;
cin>>H;
cout<<“请输入您的体重(KG):”;
cin>>W;
cout<<“您是男性,输入1,女性输入0:”;
cin>>S;

if ((A>100) || (A<1) || (H>250) || (H<40) || (W>200) || (W<2) || (S!=1 && S!=0))
{
cout<<“乱输入,不给你算!”<<endl;
}
else{
PM->SetAge(A);
PM->SetHeight(H);
PM->SetWeight(W);
PM->SetSex(S);
cout<<“设置以保存,现在可以开始计算了!”<<endl;
}
}

void DoCalc(){
int A,S;
double H,W,Rate;

A=PM->GetAge();
H=PM->GetHeight();
W=PM->GetWeight();
S=PM->GetSex();

(H<165 && S==1)?(H-=105):(H-=100);

S==1?H=H*0.9:H=H*0.9-2.5;

Rate=H/((*PM).GetAge());

cout<<“您的体重是:”<<PM->GetWeight()<<“,”<<“标准体重是:”<<H<<endl<<endl;

}

void DoInfo(){

cout<<“程序信息”;
}

void DoHelp(){
cout<<“帮助信息”;
}

int DoMenu(){

int Choice;

cout<<“**********身材计算器**********”<<endl<<endl;
cout<<“1.显示主菜单;”<<endl;
cout<<“2.设置个人信息”<<endl;
cout<<“3.计算您的身材”<<endl;
cout<<“4.帮助信息”<<endl;
cout<<“5.退出程序”<<endl;
cout<<“请输入数字键选择:”<<“”;

cin>>Choice;
return Choice;

}