![]() |
#2
rjsp2016-05-17 08:47
代码抄的吧,而且还是抄了一个对C++狗屁不通的人的代码。各种C++规范不遵守,风格和用法不遵守
![]() #include <iostream> #include <string> class Screen { public: using pos = std::string::size_type; Screen() = default; Screen( pos height, pos width, char c ) :height_(height), width_(width), content_(height_*width_,c) { } Screen& move( pos r, pos s ) { cursor_ = r*width_ + s; return *this; } char at() const { return content_[cursor_]; } char& at() { return content_[cursor_]; } char at( pos r, pos c ) const { return content_[r*width_+c]; } char& at( pos r, pos c ) { return content_[r*width_+c]; } private: pos height_ = 0; pos width_ = 0; std::string content_; pos cursor_ = 0; friend std::ostream& operator<<( std::ostream& os, const Screen& scr ); }; std::ostream& operator<<( std::ostream& os, const Screen& scr ) { for( auto r=0; r!=scr.height_; ++r ) { os.write( scr.content_.c_str()+r*scr.width_, scr.width_ ); os.write( "\n", 1 ); } return os; } using namespace std; int main( void ) { Screen scr( 5, 5, 'X' ); cout << scr << '\n'; scr.move(4,0).at() = '#'; cout << scr << '\n'; scr.at(4,4) = '#'; cout << scr << '\n'; return 0; } 输出 XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX #XXXX XXXXX XXXXX XXXXX XXXXX #XXX# |
头文件
*****************************************************************8
#ifndef TOUTOU_H
#define TOUTOU_H
#include<iostream>
#include<string>
using namespace std;
using std::iostream;
using std::string;
using std::cout;
class Screen {
public:
using pos = std::string::size_type;
Screen() = default;
Screen(pos ht, pos wd) :height(ht), width(wd), conents(ht*wd, ' ') {}
Screen(pos ht, pos wd, char c) :height(ht), width(wd), conents(ht*wd, c) {}
char get() const { return conents[cursor]; } *********** cursor未定义
char get(pos r, pos s) const { return conents[r*width + s]; }
Screen &move(pos r, pos s);
Screen &set(char c);
Screen &set(pos r, pos s, char c);
const Screen &display(std::ostream &os) const { do_display(os); return *this; }
Screen &display(std::ostream &os) { do_display(os); return *this; }
private:
void do_display const (ostream &os) {
os << conents;
}
private:
pos cursor = 0;
pos height = 0;
std::string conents;
pos width = 0;
};
inline Screen& Screen::move(pos r, pos s) {
cursor = r*width + s;
return *this;
}
inline Screen& Screen::set(char c) {
conents[cursor] = c;
return *this;
}
inline Screen& Screen::set(pos r, pos s, char c) {
conents[r*width + s] = c;
return *this;
}
#endif
程序
**********************************************************************
#include"C:\Users\Jin\Desktop\toutou.h"
int main()
{
Screen myScreen(5,5,'X'); ******************* Screen未定义
myScreen.move(4, 0).set('#').display(cout); ******************* cout未定义
cout << "\n";
myScreen.display(cout);
cout << "\n";
return 0;
}