65,186
社区成员




//Screen.h
#include<string>
#include<iostream>
using namespace std;
class Screen{
friend istream& operator>>(istream&,Screen&);
friend ostream& operator<<(ostream&,const Screen&);
private:
string _screen;
mutable string::size_type _cursor; //_cursor 被声明为mutable,因为move函数中要修改它,而move函数被声明为const
short _height,_width;
inline int remainingSpace();
public:
Screen(int hi=8,int wid=40,char bkground='#');
Screen& clear(char bkground='#');
Screen& set(const string &s);
Screen& set(char ch);
int height(){return _height;}
int width(){return _width;}
void home(){_cursor=0;}
inline Screen& move(int,int) const; //这里将move函数声明为const
char get() const{return _screen[_cursor];}
inline char get(int,int);
bool checkRange(int,int);
void copy(const Screen& sobj);
Screen& display();
Screen& reSize(int h,int w,char bkground='#');
};
Screen& Screen::move(int r,int c) const{ //move函数定义
if(checkRange(r,c)){
int row=(r-1)*_width;
_cursor=row+c-1;
}
return *this;
}
char Screen::get(int r,int c){
move(r,c);
return get();
}
inline int Screen::remainingSpace(){
int sz=_width*_height;
return(sz-_cursor);
}
//ScreenDef.cpp
#include "Screen.h"
ostream& operator<< (ostream& os,const Screen& s){
os<<"<"<<s._height<<","<<s._width<<">";
os<<s._screen;
return os;
}
istream& operator>> (istream& is, Screen& s){
is>>s._height>>s._width;
return is;
}
bool isEqual(Screen& s1, Screen * s2){
if(s1.height()!=s2->height()||s1.width()!=s2->width())
return false;
for(int ix=0;ix<s1.height();++ix)
for(int jy=0;jy<s2->width();++jy)
if(s1.get(ix,jy)!=s2->get(ix,jy))
return false;
return true;
}
Screen::Screen(int hi,int wid,char bk):_height(hi),_width(wid),_cursor(0),_screen(hi*wid,bk){}
bool Screen::checkRange(int row,int col){
if(row<1||row>_height||col<1||col>_width){
cerr<<"Screen coordinates("<<row<<","<<col<<")out of bounds.\n";
return false;
}
return true;
}
void Screen::copy(const Screen& sobj){
if(this!=&sobj)
{
_height=sobj._height;
_width=sobj._width;
_cursor=0;
_screen=sobj._screen;
}
}
Screen& Screen::set(const string &s){
int space=remainingSpace();
int len=s.size();
if(space<len){
cerr<<"Screen:warning:truncation:"<<"Space:"<<space<<"string length:"<<len<<endl;
len=space;
}
_screen.replace(_cursor,len,s);
_cursor +=len-1;
return *this;
}
Screen& Screen::set(char ch){
if(ch=='\0')
cerr<<"Screen:warning:"<<"null character(ignored).\n";
else
_screen[_cursor]=ch;
return *this;
}
Screen& Screen::clear(char bkground='#'){
_cursor=0;
_screen.assign(_screen.size(),bkground);
return *this;
}
Screen& Screen::display(){
typedef string::size_type idx_type;
for(idx_type ix=0;ix<_height;++ix){
idx_type offset=_width*ix;
for(idx_type iy=0;iy<_width;++iy)
cout<<_screen[offset+iy];
cout<<endl;
}
return *this;
}
Screen& Screen::reSize(int h,int w,char bkground='#'){
string local(_screen);
_screen.assign(h*w,bkground);
typedef string::size_type idx_type;
idx_type local_pos=0;
for(idx_type ix=0;ix<_height;++ix){
idx_type offset=w*ix;
for(idx_type iy=0;iy<_width;++iy)
_screen[offset+iy]=local[local_pos++];
}
_height=h;
_width=w;
return *this;
}
//Screen.cpp
#include "Screen.h"
int main(){
Screen sobj(3,3);
string init("abcdefghi");
cout<<"Screen Object("<<sobj.height()<<","<<sobj.width()<<")\n\n";
string::size_type initpos=0;
for(int ix=1;ix<=sobj.width();++ix)
for(int iy=1;iy<=sobj.height();++iy){
sobj.move(ix,iy);
sobj.set(init[initpos++]);
}
for(int ix=1;ix<=sobj.width();++ix){
for(int iy=1;iy<=sobj.width();++iy)
cout<<sobj.get(ix,iy);
cout<<"\n";
}
Screen myScreen(3,3),bufScreen;
myScreen.clear().move(2,2).set('*').display();
bufScreen.copy(myScreen);
bufScreen.reSize(5,5).display();
system("pause");
}