
一道c++编程题!! 高分悬赏!!
一、写出一个类,用来表现基于电脑屏幕的控制台。屏幕上的每个位置都将被作为一个无符号字节型存储在类里。这个类必须能够创建任何大小为200列和200行的屏幕,并且屏幕大小不允...
一、 写出一个类,用来表现基于电脑屏幕的控制台。屏幕上的每个位置都将被作为一个无符号字节型存储在类里。这个类必须能够创建任何大小为200列和 200行的屏幕,并且屏幕大小不允许低于20列和10行。当屏幕构造函数被调用时,如果企图创建一个在这一范围之外的屏幕,屏幕必须被设置为默认大小,对构造函数来说这一大小是指定的默认参数。 屏幕构造函数必须 接受行和列作为参数,并且构造函数的默认值是80列*25行。
这个类必须有一个变量来存储所有能够被写到屏幕上的字符(screendata),同时动态的保持屏幕的大小和光标的当前位置。 这个可变的屏幕上的字符必须被创建成根据当前屏幕大小在构造器里是动态的。各个位置的值必须被初始化为空位。在访问构造函数期间,提供一个析构函数来释放所有获得的资源。在初始化光标位置时,必须被设置到0,0 (左上角) 。在类中增加下面三个成员函数:(20 分)
SetPos - 设置光标的位置到指定位置。
GetPos – 返回当前位置。
Clear – 清除屏幕类中的内容。 展开
这个类必须有一个变量来存储所有能够被写到屏幕上的字符(screendata),同时动态的保持屏幕的大小和光标的当前位置。 这个可变的屏幕上的字符必须被创建成根据当前屏幕大小在构造器里是动态的。各个位置的值必须被初始化为空位。在访问构造函数期间,提供一个析构函数来释放所有获得的资源。在初始化光标位置时,必须被设置到0,0 (左上角) 。在类中增加下面三个成员函数:(20 分)
SetPos - 设置光标的位置到指定位置。
GetPos – 返回当前位置。
Clear – 清除屏幕类中的内容。 展开
2个回答
展开全部
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct Point
{
unsigned char col;
unsigned char row;
};
class SCreen
{
private:
unsigned char col;
unsigned char row;
Point curson;
char **ch;
void destory()
{
if ( ch == NULL )
return ;
for ( int i = 0; i < row; ++i )
delete[] ch[i];
delete[] ch;
}
void destory() const
{
if ( ch == NULL )
return ;
for ( int i = 0; i < row; ++i )
delete[] ch[i];
delete[] ch;
}
public:
SCreen(int col = 80, int row = 25)
{
if ( col > 200 || row >200 || col < 20 || row < 10 )
{
col = 80;
row = 25;
}
this->col = col;
this->row = row;
ch = new char *[this->row];
for ( int i = 0; i < this->row; ++i )
{
ch[i] = new char[col];
memset(ch[i],0,sizeof(char)*this->col);
}
curson.col = 0;
curson.row = 0;
}
~SCreen()
{
destory();
}
SCreen(const SCreen &s);
SCreen operator = ( const SCreen &s);
void SetPos(unsigned char col,unsigned char row); // - 设置光标的位置到指定位置。
void SetPos(Point p);
Point GetPos(); //– 返回当前位置。
Point GetPos() const;
void Clear();
};
SCreen::SCreen(const SCreen &s)
{
this->col = s.col;
this->row = s.row;
this->curson = s.curson;
ch = new char *[this->row];
for ( int i = 0; i < this->row; ++i )
{
ch[i] = new char[col];
memcpy(ch[i],s.ch[i],col);
}
}
SCreen SCreen::operator = ( const SCreen &s)
{
if ( this == &s )
{
return *this;
}
destory();
this->col = s.col;
this->row = s.row;
this->curson = s.curson;
ch = new char *[this->row];
for ( int i = 0; i < this->row; ++i )
{
ch[i] = new char[col];
memcpy(ch[i],s.ch[i],col);
}
return *this;
}
void SCreen::SetPos(unsigned char col,unsigned char row)
{
col = col<0?0:col>199?199:col;
row = row<0?0:row>199?199:row;
curson.col = col;
curson.row = row;
}
void SCreen::SetPos(Point p)
{
p.col = p.col<0?0:p.col>199?199:p.col;
p.row = p.row<0?0:p.row>199?199:p.row;
curson = p;
}
Point SCreen::GetPos()
{
return curson;
}
Point SCreen::GetPos() const
{
return curson;
}
void SCreen::Clear()
{
destory();
col = 0;
row = 0;
curson.col = 0;
curson.row = 0;
ch = NULL;
}
int main()
{
SCreen a(25,102);
SCreen b(258,1005);
b = a;
SCreen c(a);
return 0;
}
using std::cout;
using std::cin;
using std::endl;
struct Point
{
unsigned char col;
unsigned char row;
};
class SCreen
{
private:
unsigned char col;
unsigned char row;
Point curson;
char **ch;
void destory()
{
if ( ch == NULL )
return ;
for ( int i = 0; i < row; ++i )
delete[] ch[i];
delete[] ch;
}
void destory() const
{
if ( ch == NULL )
return ;
for ( int i = 0; i < row; ++i )
delete[] ch[i];
delete[] ch;
}
public:
SCreen(int col = 80, int row = 25)
{
if ( col > 200 || row >200 || col < 20 || row < 10 )
{
col = 80;
row = 25;
}
this->col = col;
this->row = row;
ch = new char *[this->row];
for ( int i = 0; i < this->row; ++i )
{
ch[i] = new char[col];
memset(ch[i],0,sizeof(char)*this->col);
}
curson.col = 0;
curson.row = 0;
}
~SCreen()
{
destory();
}
SCreen(const SCreen &s);
SCreen operator = ( const SCreen &s);
void SetPos(unsigned char col,unsigned char row); // - 设置光标的位置到指定位置。
void SetPos(Point p);
Point GetPos(); //– 返回当前位置。
Point GetPos() const;
void Clear();
};
SCreen::SCreen(const SCreen &s)
{
this->col = s.col;
this->row = s.row;
this->curson = s.curson;
ch = new char *[this->row];
for ( int i = 0; i < this->row; ++i )
{
ch[i] = new char[col];
memcpy(ch[i],s.ch[i],col);
}
}
SCreen SCreen::operator = ( const SCreen &s)
{
if ( this == &s )
{
return *this;
}
destory();
this->col = s.col;
this->row = s.row;
this->curson = s.curson;
ch = new char *[this->row];
for ( int i = 0; i < this->row; ++i )
{
ch[i] = new char[col];
memcpy(ch[i],s.ch[i],col);
}
return *this;
}
void SCreen::SetPos(unsigned char col,unsigned char row)
{
col = col<0?0:col>199?199:col;
row = row<0?0:row>199?199:row;
curson.col = col;
curson.row = row;
}
void SCreen::SetPos(Point p)
{
p.col = p.col<0?0:p.col>199?199:p.col;
p.row = p.row<0?0:p.row>199?199:p.row;
curson = p;
}
Point SCreen::GetPos()
{
return curson;
}
Point SCreen::GetPos() const
{
return curson;
}
void SCreen::Clear()
{
destory();
col = 0;
row = 0;
curson.col = 0;
curson.row = 0;
ch = NULL;
}
int main()
{
SCreen a(25,102);
SCreen b(258,1005);
b = a;
SCreen c(a);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询