求不同的图形面积
求不同图形的面积#include
using namespace std;
class Figure {
protected:
double x,y;
public:
Figure(double a,double b) { x = a; y = b; }
virtual void showArea() {
cout<<"No area computation defined";
cout<<"For this class. \n";
}
};
class Triangle:public Figure {
public:
Triangle(double a,double b):Figure(a,b) {};
void showArea() {
cout<<"Triangle with height "<<X;
cout<<" and base "<<Y;
cout<<X*Y 2<<endl;
}
};
class Square:public Figure {
public:
Square(double a,double b):Figure(a,b) {};
void showArea() {
cout<<"Square with dimension "<<X;
cout<<" * "<<Y<<" HAS AN AREA OF ";
cout<<X*Y<<ENDL;
}
};
class Circle:public Figure {
public:
Circle(double a):Figure(a,a) {};
void showArea() {
cout<<"Circle with radius "<<X;
cout<<" has an area of ";
cout<<X*X*3.1416<<ENDL;
}
};
class Trapezoid:public Figure {
protected:
double z;
public:
Trapezoid(double a,double b,double c):Figure(a,b) { z = c; };
void showArea() {
cout<<"Trapezoid area is: "<<ENDL;
cout<<(x + y) * z / 2<<ENDL;
}
};
int main() {
int m;
double base,high;
double length,wide;
double radius;
double b1,b2,h1;
for(int j = 0; j <= 3; j++) {
cout<<" _______________________ "<<ENDL;
cout<<" | 请选择操作 | "<<ENDL;
cout<<" | 1.求三角形面积 | "<<ENDL;
cout<<" | 2.求矩形面积 | "<<ENDL;
cout<<" | 3.求圆面积 | "<<ENDL;
cout<<" | 4.求梯形面积 | "<<ENDL;
cout<<" | 请输入操作 | "<<ENDL;
cout<<" |_____________________| "<<ENDL;
cin>>m;
cout<<"请输入三角形底与高"<<ENDL;
cin>>base>>high;
Triangle t(base , high);
cout<<"请输入矩形长与宽"<<ENDL;
cin>>length>>wide;
Square s(length , wide);
cout<<"请输入圆的半径";
cin>>radius;
Circle c(radius);
cout<<"请输入梯形上下底与高"<<ENDL;
cin>>b1>>b2>>h1;
Trapezoid p(b1 , b2 , h1);
switch(m) {
case 1:
t.showArea();
break;
case 2:
s.showArea();
break;
case 3:
c.showArea();
break;
case 4:
p.showArea();
break;
}
}
return 0;
}
每次都出现操作界面,无论按什么操作都会出现“输入三角形底与高”等,这怎么改?
[此贴子已经被作者于2016-12-7 11:00编辑过]