Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
一点都不简短的代码(9K)#include <iostream> #include <vector> #include <string> #include <sstream> #include <stdexcept> #include <map> #include <stdlib.h> using namespace std; typedef vector<vector<char> > matrix; typedef unsigned int uint; namespace entity { class Number { protected: static const char vert; static const char horz; matrix *data; uint row, col, mid; virtual void fill_up() = 0; void draw_v11() { for (uint i = 1; i < mid; ++i) { get_data()[i][0] = vert; } } void draw_v12() { for (uint i = mid + 1; i < row - 1; ++i) { get_data()[i][0] = vert; } } void draw_v21() { for (uint i = 1; i < mid; ++i) { get_data()[i][col - 1] = vert; } } void draw_v22() { for (uint i = mid + 1; i < row - 1; ++i) { get_data()[i][col - 1] = vert; } } void draw_h1() { for (uint j = 1; j < col - 1; ++j) { get_data()[0][j] = horz; } } void draw_h2() { for (uint j = 1; j < col - 1; ++j) { get_data()[mid][j] = horz; } } void draw_h3() { for (uint j = 1; j < col - 1; ++j) { get_data()[row - 1][j] = horz; } } public: Number(uint size) { //data set is initialized here. if (size == 0) { throw range_error("size is out of range.\n"); } row = 2 * size + 3; col = size + 2; mid = (row - 1) / 2; data = new matrix; get_data().resize(row); for (uint i = 0; i < row; ++i) { get_data()[i].resize(col, ' '); } //run_fill_up(); } virtual ~Number() { //resource collected in the base destructor delete data; }; inline matrix &get_data() { return *data; }; string to_string() { stringstream ss; for (uint i = 0; i < row; ++i) { for (uint j = 0; j < col; ++j) { ss << get_data()[i][j]; } if (i < row - 1)ss << '\n'; } return ss.str(); } friend ostream &operator<<(ostream &os, Number &num) { os << num.to_string(); return os; } }; const char Number::vert = '|'; const char Number::horz = '-'; class One : public Number { protected: virtual void fill_up() { draw_v21(); draw_v22(); } public: One(uint size) : Number(size) { fill_up(); }; }; class Two : public Number { protected: virtual void fill_up() { draw_v12(); draw_v21(); draw_h1(); draw_h2(); draw_h3(); } public: Two(uint size) : Number(size) { fill_up(); } }; class Three : public Number { protected: virtual void fill_up() { draw_v21(); draw_v22(); draw_h1(); draw_h2(); draw_h3(); } public: Three(uint size) : Number(size) { fill_up(); } }; class Four : public Number { protected: void fill_up() { draw_v11(); draw_v21(); draw_v22(); draw_h2(); } public: Four(uint size) : Number(size) { fill_up(); } }; class Five : public Number { protected: virtual void fill_up() { draw_v11(); draw_v22(); draw_h1(); draw_h2(); draw_h3(); } public: Five(uint size) : Number(size) { fill_up(); } }; class Six : public Number { protected: virtual void fill_up() { draw_v11(); draw_v12(); draw_v22(); draw_h1(); draw_h2(); draw_h3(); } public: Six(uint size) : Number(size) { fill_up(); } }; class Seven : public Number { protected: virtual void fill_up() { draw_v21(); draw_v22(); draw_h1(); } public: Seven(uint size) : Number(size) { fill_up(); } }; class Eight : public Number { protected: virtual void fill_up() { draw_v11(); draw_v12(); draw_v21(); draw_v22(); draw_h1(); draw_h2(); draw_h3(); } public: Eight(uint size) : Number(size) { fill_up(); } }; class Nine : public Number { protected: virtual void fill_up() { draw_v11(); draw_v21(); draw_v22(); draw_h1(); draw_h2(); draw_h3(); } public: Nine(uint size) : Number(size) { fill_up(); } }; class Zero : public Number { protected: virtual void fill_up() { draw_v11(); draw_v12(); draw_v21(); draw_v22(); draw_h1(); draw_h3(); } public: Zero(uint size) : Number(size) { fill_up(); } }; } class Concatenator { private: uint row, col; vector<entity::Number *> *res; stringstream ss; char char_temp[1]; inline int char_to_int(const char &c) { char_temp[0] = c; return atoi(char_temp); } public: Concatenator(uint size) { res = new vector<entity::Number *>; //POJ does not support C++11, therefore the macro NULL is used here... res->resize(10, NULL); //A pretty "naive" mapping is used here. res->at(0) = new entity::Zero(size); res->at(1) = new entity::One(size); res->at(2) = new entity::Two(size); res->at(3) = new entity::Three(size); res->at(4) = new entity::Four(size); res->at(5) = new entity::Five(size); res->at(6) = new entity::Six(size); res->at(7) = new entity::Seven(size); res->at(8) = new entity::Eight(size); res->at(9) = new entity::Nine(size); row = 2 * size + 3; col = size + 2; } //Numeric string please... string operator()(string input) { size_t pos; //eliminate invalid characters string::iterator iter; for (iter = input.begin(); iter != input.end();) { if (*iter < '0' || *iter > '9') { iter = input.erase(iter); } else { ++iter; } } //generate temp array uint ch_col = input.length() * col + (input.length()); vector<vector<char> > ch; ch.resize(row); for (uint i = 0; i < row; i++) { ch[i].resize(ch_col, ' '); } pos = 0; int res_pos; entity::Number *source; for (uint i = 0; i < input.length(); ++i) { //fill character res_pos = char_to_int(input[i]); char tar = input[i]; source = res->at(res_pos); for (uint j = 0; j < row; ++j) { for (uint k = pos; k < pos + col; ++k) { ch[j][k] = source->get_data()[j][k - pos]; } } pos += (col + 1); } //convert into std::string ss.str(""); for (uint i = 0; i < row; ++i) { for (uint j = 0; j < ch_col; ++j) { ss << ch[i][j]; } if (i < row - 1)ss << '\n'; } return ss.str(); } ~Concatenator() { for (uint i = 0; i < 10; ++i) { delete res->at(i); } delete res; } }; typedef map<uint, Concatenator *> map_class; class NumStrConverter { private: map_class data; map_class::iterator iter; public: NumStrConverter() {}; string operator()(uint size, const string &str) { iter = data.find(size); if (iter != data.end()) { return (*(iter->second))(str); } else { Concatenator *con = new Concatenator(size); data.insert(map_class::value_type(size, con)); return (*con)(str); } } ~NumStrConverter() { for (iter = data.begin(); iter != data.end(); ++iter) { delete iter->second; } } }; int main() { NumStrConverter conv; int size; string num; string temp; while (true) { cin >> size >> num; if (size == 0)break; temp = conv(size, num); cout<<temp<<endl<<endl; } #ifndef ONLINE_JUDGE system("pause"); #endif return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator