1 回答

TA貢獻1805條經(jīng)驗 獲得超9個贊
#include<iostream>
using namespace std;
class Shape
{
public:
virtual ~Shape();
virtual void draw() const =0;
};
Shape::~Shape()
{
}
class Star : public Shape
{
public:
virtual void draw() const;
};
void Star::draw() const
{
int t,i,j,sp,s;
cout<<"請輸入尺寸:";
cin>>s;
cout<<"繪制自選圖形:"<<endl;
sp=(2*s-1)/2;
for(i=sp;i>=-sp;i--)
{
t=i>0 ? i : -i;
for(j=0;j<t;j++)
{
cout<<" ";
}
for(j=0;j<2*s-1-2*t;j++)
{
cout<<"*";
}
cout<<endl;
}
}
class Rectangle : public Shape
{
public:
virtual void draw() const;
};
void Rectangle::draw() const
{
int i,j,w,h;
cout<<"請輸入 寬 高:";
cin>>w>>h;
cout<<w<<" "<<h<<endl;
cout<<"繪制自選圖形:"<<endl;
for(i=h;i>0;i--)
{
for(j=w;j>0;j--)
{
cout<<"*";
}
cout<<endl;
}
}
int main(int argc,char* argv[])
{
int m;
Shape *pt[]={new Star(),new Rectangle()};
while(true)
{
cout<<"1- 星"<<endl<<"2- 矩形"<<endl;
cout<<"選擇一種圖形或者選0退出:";
cin>>m;
if(0==m)
{
break;
}
else if(1==m || 2==m)
{
pt[m-1]->draw();
}
else
{
cout<<"無法處理的命令!"<<endl;
}
}
delete pt[0];
delete pt[1];
return 0;
}
- 1 回答
- 0 關注
- 699 瀏覽
添加回答
舉報