注册 登录
编程论坛 C++教室

screen类的使用,

未未来 发布于 2013-04-22 16:27, 625 次点击
程序代码:
#include<iostream>
#include<string>
using namespace std;
class Screen{
    public:
    typedef std::string::size_type index;
     char get()const {return contents[cursor];}
       inline    char get(index ht,index wd)const;
    index get_cursor()const;
    Screen(index hght,index wdth,const string &cntnts);
   
    Screen& move(index r,index c);
     Screen& set(char );
        
     Screen& display(ostream &os);
        private:
    std::string contents;
    index cursor;
    index height,width;
};
Screen::Screen(index hght,index wdth,const string &cntnts=" "):contents(cntnts),
cursor(0),height(hght),width(wdth){
    contents.assign(hght*wdth,' ');
    if(cntnts.size()!=0)
    contents.replace(0,cntnts.size(),cntnts);
   
}

char Screen::get(index ht,index wd)const{
    index row=ht*width;
    return contents[row+wd];
}
inline Screen::index Screen::get_cursor() const{
    return cursor;
}
Screen& Screen::set(char c){
    contents[cursor]=c;
    return *this;
}
Screen& Screen::move(index r,index x)
{
    if(r>=height||x>=width){
        cerr<<":invelid row or c"<<endl;
        throw EXIT_FAILURE;
    }
    index row=r*width;
    cursor=row+x;
    return *this;
}
Screen& Screen::display(ostream &os){
string::size_type index=0;
while(index!=contents.size()){
    os<<contents[index];
    if((index+1)%width==0){
        os<<'\n';
    }++index;
}
    return *this;
}

int main(){
    Screen myscreen(5,6,"aaaaa\naaaaa\naaaaa\naaaaa\naaaaa\n");
    myscreen.move(4,0).set('#').display(cout);

    return 0;
}



为什么(4,0)运行程序修改的是第五行的第一个,
4 回复
#2
rjsp2013-04-23 08:37
为什么(4,0)运行程序修改的是第五行的第一个
------ 没看你代码,我猜 4,0 是 base 0 的,就是从 0 开始计数的,而“第五行的第一个”是base 1的,就是从 1开始计数的。
#3
peach54602013-04-23 08:53
以下是引用rjsp在2013-4-23 08:37:45的发言:

为什么(4,0)运行程序修改的是第五行的第一个
------ 没看你代码,我猜 4,0 是 base 0 的,就是从 0 开始计数的,而“第五行的第一个”是base 1的,就是从 1开始计数的。

这个不用看代码,一定是这样的...下标从0开始
#4
xgp7584921932013-04-23 23:20
倒数第二行display后为何加cout
#5
bme_zxl2013-05-02 18:58
1.明显的是move函数里面应该是r-1
2.我不太知道构造函数里面的那个作用是什么
3.display里面也不用这么复杂,用cout就可以了


#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
class Screen{
public:
    typedef std::string::size_type index;
    char get()const {
        return contents[cursor];
    }
    inline char get(index ht,index wd)const;
    index get_cursor()const;
   
    //构造函数
    Screen(index hght,index wdth,const string &cntnts);

    Screen &move(index r,index c);
    Screen &set(char);
    Screen &display();
private:
    string contents;
    index cursor;
    index height,width;
};

Screen::Screen(index hght,index wdth,const string &cntnts=" "):contents(cntnts),cursor(0),height(hght),width(wdth){}


char Screen::get(index ht,index wd)const{
    index row=ht*width;
    return contents[row+wd];
}


inline Screen::index Screen::get_cursor() const{
    return cursor;
}


Screen& Screen::set(char c){
    contents[cursor]=c;
    return *this;
}


Screen& Screen::move(index r,index x)
{
    if(r>=height||x>=width){
        cerr<<":invelid row or c"<<endl;
        throw EXIT_FAILURE;
    }
    index row=(r-1)*width;
    cursor=row+x;
    return *this;
}


Screen& Screen::display(){
    cout<<contents;
    return *this;
}

int main(){
    Screen myscreen(5,6,"aaaaa\naaaaa\naaaaa\naaaaa\naaaaa\n");
    myscreen.display();
    cout<<endl;
    myscreen.move(4,0).set('#').display();
    _getch();
    return 0;
   
}
1