#include "stdafx.h"
#include <string>
using namespace std;
class Screen
{
friend istream&
operator>>(istream&,Screen&);
friend ostream&
operator<<(ostream&,const Screen&);
private:
//static const int _height=24;//行數
//static const int _width=80; //列數
string _screen;
string::size_type _cursor;//當前螢幕screen的位置
short _height,_width;
public:
void home(){_cursor=0;};//inline函式
void move(int,int);
char get(){return _screen[_cursor];};
char get(int,int);
bool checkRange(int,int);
int Height(){return _height;};
int Width(){return _width;};
void copy(const Screen &sobj);
}ptrScreen,myScreen;
class StackScreen
{
int topStack;
Screen *stack;
void(*handler)();
};
class LinkScreen
{
Screen window;
LinkScreen *next;
LinkScreen *prev;
};
#include "stdafx.h"
#include "Screen.h"
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;
}
inline void Screen::move(int r,int c)
{//將_cursor稱到絕對位置
if(checkRange(r,c))//位置合法嗎?
{
int row=(r-1)*_width;//行位置
_cursor=row+c-1;
}
}
char Screen::get(int r,int c)
{
move(r,c);//_cursor位置
return get();//另一個get()成員函式
}
void Screen::copy(const Screen &sobj)
{
//如果這個Screen物件與sobj是同一個物件,則無需拷貝
if (this!=&sobj)
{
_height=sobj._height;
_width=sobj._width;
_cursor=0;
//建立一個新字串,他的內容與sobj.screen相同
_screen=sobj._screen;
}
}
}
ostream& operator<<(ostream& os,const Screen &s)
{
os<<"<"<<s._height
<<","<<s._width<<">";
os<<s._screen;
return os;
}
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;jy<s2->Width();jy++)
if (s1.get(ix,jy)!=s2->get(ix,jy))
{
return false;
}
return true;
}