各位大侠,帮忙改下,我是初学
#include <math.h>
#include<iostream>
using namespace std;
class Data1{
double
length,width ;
public:
void PrintMenu();
void setdata_1(double l,double w);
double Girth_1();
double
Area_1();
void
ErrMsg_1(double nCode);
};
void Data1::PrintMenu()
{
cout<<"
MENU\n"<<endl;
cout<<"========================"<<endl;
cout<<"1.矩形的面积\n"<<endl;
cout<<"2.三角形的面积\n"<<endl;
cout<<"3.梯形的面积\n"<<endl;
cout<<"========================"<<endl;
cout<<"choice(1,2,3):"<<endl;
}
void
Data1::setdata_1(double l,double w){
length=l;width=w;
}
double Data1::Girth_1(){
double g;
g=(length+width)*2;
return g;
}
double Data1::Area_1()
{
double s;
s=length*width;
if (length<=0||width<=0)
return -1000;
else
return s;
}
void
Data1::ErrMsg_1(int nCode) // 错误处理函数
{
switch(nCode)
{
//根据错误码输出错误信息
case -1000:cout<<"矩形任意一边不能小于或等于0!Error Code="<<nCode<<endl;break;
}
class Data2{
double
a,b,c;
public:
void setdata_2(double a1,double a2,double a3);
double Girth_2();
double Area_2();
void
ErrMsg_2(double nCode);
};
void
Data2::setdata_2(double a1,double a2,double a3){
a=a1,b=a2,c=a3;
}
double Data2::Girth_2(){
double h;
h=a+b+c;
return h;
}
double Data2::Area_2(){
double s,l;
l=(a+b+c)/2;
if (a<=0||b<=0||c<=0||a+b<c)
return -1001;
else
return sqrt(l*(l-a)*(l-b)*(l-c));
}
void
Data2::ErrMsg_2(int nCode) // 错误处理函数
{
switch(nCode)
{
//根据错误码输出错误信息
case -1001:cout<<"三角形任意一边不能小于或等于0,或输入数据不能构成三角形!Error Code="<<nCode<<endl;break;
}
class Data3{
double
a,b,c;
public:
void setdata_3(double a1,double a2,double h);
double Girth_3();
double Area_3();
void
ErrMsg_3(int nCode);
};
void
Data3::setdata_3(double a1,double a2,double h){
a=a1,b=a2,c=h;
}
double Data3::Girth_3(){
double g,m;
m=sqrt(((b-a)/2)*((b-a)/2)+c*c);
g=a+b+2*m;
if(a<=0||b<=0||c<=0||a==b)
return -1002;
else
return g;
}
double Data3::Area_3(){
double s;
s=(a+b)*c/2;
if(a<=0||b<=0||c<=0||a==b)
return -1002;
else
return s;
}
void
Data3::ErrMsg_3(int nCode) // 错误处理函数
{
switch(nCode)
{
//根据错误码输出错误信息
case -1002:cout<<"梯形底或高不能小于0!或上底和下底相等!Error Code="<<nCode<<endl;break;
}
}
int main()
// 主函数
{
Data1 Rect;
Data2 Triangle;
Data3 Trapezia;
int
i;
double a1, a2,a3, s, l, w, h;
Rect.PrintMenu();
cin>>i;
switch(i)
{
case 1:
while(1)
{
cout<<"请输入矩形的长和宽:";
cin>>l>>w;
Rect.setdata_1(l,w);
s=Rect.Area_1(); // 通过r求面积
if(s > 0) //
有效输入,输出面积
{
cout<<"矩形的面积为:"<<s<<endl;
cout<<"周长为:"<<Rect.Girth_1()<<endl;
break; // 跳出循环
}
else
// 无效的输入,返回错误码
{
Rect.ErrMsg_1(s);
}
}
break;
case 2:
while(1)
{
cout<<"请输入三边;";
cin>>a1>>a2>>a3;
Triangle.setdata_2(a1,a2,a3);
s=Triangle.Area_2(a1, a2, a3); // 通过r求面积
if(s>0) //
有效输入,输出面积
{
cout<<"三角形的面积为:"<<s<<endl;
cout<<"周长为:"<<Triangle.Girth_2()<<endl;
break; // 跳出循环
}
else
// 无效的输入,返回错误码
{
Triangle.ErrMsg_2(s);
}
}
break;
case 3:
while(1)
{
cout<<"请输入梯形的上底下底和高:";
cin>>a1>>a2>>h;
Trapezia.setdata_3(a1,a2,h);
s=Trapezia.Area_3(a1, a2, h); // 通过r求面积
if(s > 0) //
有效输入,输出面积
{
cout<<"梯形的面积为:"<<s<<endl;
cout<<"周长为:"<<Trapezia.Girth_3()<<endl;
break; // 跳出循环
}
else
// 无效的输入,返回错误码
{
Trapezia.ErrMsg_3(s);
}
}
break;
default:
break;
}
return 0;
}