pre4
cpp
posted: Aug, 21st 2012 | jump to bottom
#include <iostream> #include <string> #ifdef _MSC_VER #include <crtdbg.h> #endif using namespace std; class piece{ string name; char abv; int player; int id; static int count; public: piece(string a = " ",char b = ' ',int c = 1){name = a; abv = b; player = c; count++; id = count;} void print()const{ if (name != " ")cout << name << " p" << player << endl; else cout << ""; } void printId()const{cout << id;} int CheckAbv(); void printInfo(); }; int piece::count = 0; int piece::CheckAbv() { if (abv == ' ') return 1; else return 0; } void piece::printInfo() { cout << name << " p" << player << endl; } class table{ int wide,high; int id; static int num; piece** alpha; public: table(int w = 3,int h = 3); table(const table& src); table& operator=(const table& rhs); table& operator+(const table& rhs); void put(const piece& inchar,int wp,int hp); void print(); }; int table::num = 0; table::table(int w, int h) { if (w>100 || h>100) {wide = 100; high = 100;} else {wide = w; high = h;} alpha = new piece*[wide]; for(int i=0 ; i<wide ; i++) { alpha[i] = new piece[high]; } num++; id = num; } table::table(const table& src) { int i,j; wide = src.wide; high = src.high; alpha = new piece*[wide]; for(i = 0; i < wide; i++){alpha[i] = new piece[high];} for(i = 0; i < wide; i++) { for(j = 0; j < high; j++) { alpha[i][j] = src.alpha[i][j]; } } num++; id = num; } table& table::operator=(const table& rhs) { int i,j; if(this == &rhs){ return *this; } else { for(i=0;i<wide;i++){delete[] alpha[i];} delete[] alpha; wide = rhs.wide; high = rhs.high; alpha = new piece*[wide]; for(i=0;i<wide;i++) {alpha[i] = new piece[high];} for(i=0;i<wide;i++) { for(j=0;j<high;j++) { alpha[i][j] = rhs.alpha[i][j]; } } return *this; } } table& table::operator+(const table& rhs) { int i,j; int maxWide,maxHigh; if(wide < rhs.wide) maxWide = rhs.wide; else maxWide = wide; if(high < rhs.high) maxHigh = rhs.high; else maxHigh = high; table TEMP(maxWide,maxHigh); TEMP.alpha = new piece*[wide]; for(i=0;i<wide;i++){TEMP.alpha[i] = new piece[high];} for(i=0;i<wide;i++){ for(j=0;j<high;j++){TEMP.alpha[i][j] = rhs.alpha[i][j];} } for(i=0;i<wide;i++){ for(j=0;j<high;j++){TEMP.alpha[i][j] = alpha[i][j];} } return TEMP; } void table::print() { cout << "\ntable " << id << "(" << wide << "," << high << ")" << endl; for(int i = 0; i<wide ; i++) { for(int j = 0; j<high ; j++) { int check; check = alpha[i][j].CheckAbv(); if(check) cout << ""; else { cout << "("<< i+1 << "," << j+1 << ") : "; alpha[i][j].printInfo(); } } } } void table::put(const piece& inchar,int wp,int hp) { if (wp > wide || hp > high) cout << "Put Fail: Out of range. (" << wp << "," << hp <<")" << " > (" << wide << "," << high <<") \n"; else { int check; check = alpha[wp-1][hp-1].CheckAbv(); if(check) alpha[wp-1][hp-1] = inchar; else cout << "Put Fail: (" << wp << "," << hp << ") No vacency" << endl; } } int main(){ #ifdef _DEBUG #ifdef _MSC_VER ::_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif #endif const piece noPiece; noPiece.print(); const piece k1("King",'K'); k1.print(); const piece k2("King",'k',2); k2.print(); noPiece.printId(); k1.printId(); k2.printId(); cout << "\n"; piece o("O",'O'); piece x("X",'X',2); piece n1("Knight",'N'); piece n2("Kinght",'n',2); piece r1("Rook",'R'); piece q2("Queen",'q',2); cout<<"\n---3 pts---\n"; table ox; table chess1(8,8); ox.put(o,2,2); ox.put(x,3,3); ox.print(); ox.put(o,4,3); ox.put(x,2,2); chess1.put(k1,2,7); chess1.put(n1,2,8); chess1.put(r1,3,6); chess1.print(); cout << "\n---1 pts: copy ctor---\n"; table t(chess1); t.print(); cout << "\n---1 pts: operator=---\n"; t = ox; t.print(); table chess2(8,8); chess2.put(q2,6,3); chess2.put(n2,6,5); chess2.put(k2,7,3); chess2.print(); cout << "\n---1 operator+() pts---\n"; t = chess1 + chess2; t.print(); chess1.print(); chess2.print(); return 0; }
12 views




