匆匆忙忙,弄了一个计算身高的程序。实现这么简单的功能不需要100多行,这样写趁机熟悉一下C++的对象。当然程序还可以优化。不高兴写了,这个东西再写下去有点儿无聊。
#include
//这是一个简单的程序,用来测试身材,用指针操作对象;
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 (ChoiceQuit){
cout<
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<
cin>>H;
cout<
cin>>W;
cout<
cin>>S;
if ((A>100) || (A<1) || (H>250) || (H<40) || (W>200) || (W<2) || (S!=1 && S!=0))
{
cout<
}
else{
PM->SetAge(A);
PM->SetHeight(H);
PM->SetWeight(W);
PM->SetSex(S);
cout<
}
}
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<GetWeight()<
}
void DoInfo(){
cout<
}
void DoHelp(){
cout<
}
int DoMenu(){
int Choice;
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cin>>Choice;
return Choice;
}