|
发表于 2013-11-15 09:21:59
|
显示全部楼层
# include<iostream>
#include <windows.h>
# define PI 3.14
using namespace std;
//几何图形
class jhtx
{
public:
jhtx()
{}
virtual void show()
{
cout<<"无规则不成方圆"<<endl;
}
};
//矩形
class rectangle:public jhtx
{
public:
rectangle()
{}
rectangle(float x,float y)
{
length=x;
width=y;
}
void number()
{
cout<<"输入数据"<<endl;
cin>>length>>width;
}
//求周长
float zc()
{
return (length+width)*2;
}
//求面积
float eare()
{
return length*width;
}
void show()
{
cout<<"矩形面积"<<eare()<<endl;
cout<<"矩形周长"<<zc()<<endl;
}
protected:
float length,width;
};
//正方体
class square:public rectangle
{
public:
square()
{}
square(float x,float y,float z):rectangle(x,y)
{
height=z;
}
void number()
{
rectangle::number();
cin>>height;
}
//求体积
float tj()
{
return eare()*height;
}
void show()
{
cout<<"体积"<<tj()<<endl;
}
private:
float height;
};
//圆
class circle:public jhtx
{
protected:
float radius;
public:
circle()
{}
circle(float r)
{
radius=r;
}
void number()
{
cout<<"请输入半径"<<endl;
cin>>radius;
}
//求周长
float zc()
{
return 2.0*radius*PI;
}
//求面积
float eare()
{
return PI*radius*radius;
}
void show()
{
cout<<"圆的周长"<<zc()<<endl;
cout<<"圆的面积"<<eare()<<endl;
}
};
//球
class earth:public circle
{
public:
earth()
{}
//求体积
float tj()
{
return 4.0*PI*radius*radius*radius;
}
void show()
{
cout<<"球的体积"<<tj()<<endl;
}
};
//三角形
class triangle:public jhtx
{
protected:
float a,b;
public:
triangle()
{}
triangle(float x,float y)
{
a=x;
b=y;
}
void number()
{
cout<<"输出三角形的数据"<<endl;
cin>>a>>b;
}
//求面积
float eare()
{
return a*b/2;
}
void show()
{
cout<<"三角形的面积"<<eare()<<endl;
}
};
//三角柱
class ltriangle:public triangle
{
private:
float a,b,c;
public:
ltriangle()
{}
ltriangle(float x,float y,float z):triangle(x,y)
{
c=z;
}
void number()
{
triangle::number();
cin>>c;
}
//求体积
float tj()
{
return eare()*c;
}
void show()
{
cout<<"三角柱的体积"<<tj()<<endl;
}
};
int main()
{
jhtx *p;
char ch;
while(ch!='0')
{
cout<<"1 几何图形"<<"..."<<"2 矩形"<<"..."<<"3 长方体"<<"..."
<<"4 圆"<<"..."<<"5 球"<<"..."
<<"6 三角形"<<"..."<<"7 三角柱"
<<"..."<<"0 退出"<<endl;
cout<<"请输入一个字符"<<endl;
cin>>ch;
switch(ch)
{
case '1':
{
jhtx z;
p=&z;
p->show();
}
break;
case '2':
{
rectangle x;
x.number();
p=&x;
p->show();
}
break;
case '3':
{
square n;
n.number();
p=&n;
p->show();
}
break;
case '4':
{
circle v;
v.number();
p=&v;
p->show();
}
break;
case '5':
{
earth b;
b.number();
p=&b;
p->show();
}
break;
case '6':
{
triangle c;
c.number();
p=&c;
p->show();
}
break;
case '7':
{
ltriangle n;
n.number();
p=&n;
p->show();
}
break;
case '0':
break;
default:
cout<<"输入的不合法,请重新输入"<<endl;
break;
}
//实现清屏,时间间隔为1秒
Sleep(1000);
system("cls");
}
return 0;
} |
|