主题:关于矩阵类C++
shuyan50
[专家分:0] 发布于 2008-05-01 16:29:00
#include<iostream>
#include<iomanip>
using namespace std;
class rectangle1{
private:
int left,right,top,bottom;
public:
void set(int l,int r,int t,int b)
{
left=l;right=r;top=t;bottom=b;
}
void print(){
for(int i=top;i<=bottom;i++)
cout<<setw(right-left)<<setfill('*')<<'*'<<endl;
}
};
void main(){
rectangle1 a;
int l,r,t,b;
int x,y;
cin>>l>>r>>t>>b;
a.set(l,r,t,b);
a.print();
}
这是我现在写的一个草稿,要求是输出左上角坐标,计算面积,移动矩形,输出矩形,问题是我不知道怎么移动这个矩形才好啊,在这里求助一下,还有能否帮我想个更好的输出方法,能是矩形中间没有东西,就只是由四边组成就更好的,谢谢啊
最后更新于:2008-05-01 16:35:00
回复列表 (共1个回复)
沙发
shuyan50 [专家分:0] 发布于 2008-05-03 09:34:00
都没人回复的啊,自己做出来了,给自己顶一下吧,可怜啊
#include<iostream>
#include<iomanip>
#include <windows.h>
using namespace std;
class rectangle1{
private:
int left,right,top,bottom;
public:
void Set(int l,int r,int t,int b);
void Move(int x,int y);
void Where();
void Area();
void Print();
};
void rectangle1::Set(int l,int r,int t,int b)
{
if(l>=r||t>=b)
cout<<"对不起,您的输入有误,无法创建矩形"<<'\n';
else
left=l;right=r;top=t;bottom=b;
}
void rectangle1::Move(int x,int y){
for(int i=0;i<y;i++)
cout<<'\n'<<endl;
cout<<setw(x)<<setfill(' ')<<' ';
cout<<setw(right-left)<<setfill('*')<<'*'<<endl;
for(int i=1;i<bottom-top-1;i++)
{
cout<<setw(x)<<setfill(' ')<<' ';
cout<<'*'<<setw(right-left-2)<<setfill(' ')<<' '<<'*'<<endl;
}
cout<<setw(x)<<setfill(' ')<<' ';
cout<<setw(right-left)<<setfill('*')<<'*'<<endl;
left+=x;right+=x;
top+=y;bottom+=y;
}
void rectangle1::Where(){
cout<<'('<<left<<','<<top<<')'<<endl;
}
void rectangle1::Area(){
int length,width,area;
length=right-left;
width=bottom-top;
area=length*width;
cout<<area<<endl;
}
void rectangle1::Print(){
for(int i=0;i<top;i++)
cout<<'\n'<<endl;
cout<<setw(left)<<setfill(' ')<<' ';
cout<<setw(right-left)<<setfill('*')<<'*'<<endl;
for(int i=1;i<bottom-top-1;i++)
{
cout<<setw(left)<<setfill(' ')<<' ';
cout<<'*'<<setw(right-left-2)<<setfill(' ')<<' '<<'*'<<endl;
}
cout<<setw(left)<<setfill(' ')<<' ';
cout<<setw(right-left)<<setfill('*')<<'*'<<endl;
}
void main(){
rectangle1 a;
int l,r,t,b,choice;
int x,y;
while(1){
cout<<" ***菜单***"<<'\n';
cout<<"1.创建一个新的矩形"<<'\n';
cout<<"2.移动矩形"<<'\n';
cout<<"3.改变矩形的大小"<<'\n';
cout<<"4.显示矩形左上角的坐标"<<'\n';
cout<<"5.计算矩形面积"<<'\n';
cout<<"6.显示矩形"<<'\n';
cout<<"7.退出"<<'\n';
cout<<"如果需要清屏,请选择0,谢谢使用"<<endl;
cout<<"请输入菜单号:";
cin>>choice;
switch(choice){
case 1:cout<<"请输入所要创建的矩形的边界值:";
cin>>l>>r>>t>>b;
a.Set(l,r,t,b);break;
case 2:cout<<"请输入移动矩阵的距离:";
cin>>x>>y;
for(int i=1;i<10;i++)
{Sleep(200);
system("cls");
a.Move(i*x,i*y);
}
break;
case 3:cout<<"请输入改变后矩阵的边界值:";
cin>>l>>r>>t>>b;
a.Set(l,r,t,b);break;
case 4:cout<<"现在矩形左上角的坐标是:";
a.Where();break;
case 5:cout<<"现在矩形的面积是:";
a.Area();break;
case 6:cout<<"现在的矩形如下:"<<'\n';
a.Print();break;
case 7:exit(0);
case 0:system("cls");break;
default:cout<<"对不起,您的输入是错误的"<<'\n';break;
}
}
}
有人能帮我优化一下也好,谢谢啊
我来回复