用火柴棒擺成的0-9間的數字,橫向由一根火柴構成,縱向都是由兩根火柴構成,
可按如下規則進行變化:
1.數字移動一根火柴棒可變成其它0到9之間合法的數字
2.數字添加一根火柴棒可變成其它0到9之間合法的數字
3.數字去掉一根火柴棒可變成其它0到9之間合法的數字
現在給出一個帶有兩個操作數的+,-,*,/的算術式子,需要你判斷該式子是否
成立,如給出式子9-6=3,這個符合正常結果,正確;如給出式子
9-6=2,其判斷規則如下:
1.變換左操作數可以使式子成立的話,打印出來變化後的算術等式
2.變換右操作數可以使式子成立的話,打印出來變化後的算術等式
3.變化左右操作數可以使式子成立的話,打印出來變化後的算術等式
4.以上變化都無法讓等式成立,打印等式無法成立
完成代碼注意事項:
1.請用C++面嚮對象語言來完成代碼書寫,書寫代碼要依
據編程規範,代碼質量和代碼功能實現各佔一半的分數。
2.不考慮負數的情況
class Index { public: Index(int col = 0,int row=0) : _col(col),_row(row){}; int getIndexVal() { if (array[_col][_row] != -1) { return array[_col][_row++]; } else { _row = 0; return -1; } } private: int _col; int _row; static int array[10][10]; }; int Index::array[10][10] = { { 6, 8, 9, -1 }, { 7, -1 }, { 3, -1 }, { 2, 5, 9, -1 }, { -1 }, { 3, 6, 9, -1 }, { 0, 5, 8, 9, -1 }, { 1, -1 }, { 0, 6, 9, -1 }, { 0, 3, 5, 6, 8, -1 }, }; class Match { public: //+, -, *, / Match(char* str = NULL) { if (str == NULL) return; else { mleft =*str - '0'; str++; msign = *str; str++; mright = *str - '0'; str++; str++; mresult = *str - '0'; IsTrue(); //cout << mleft << msign << mright << mresult << endl; } } int makeResult( const int left, const int right) { switch (msign) { case '+': return left + right; case '-': return left - right; case '*': return left*right; case '/': return left / right; default: throw "invalid sign!"; break; } } void IsTrue() { int result = makeResult(mleft, mright); if (mresult == result) { print(mleft, mright); } Index IL(mleft), IR(mright); int Lflag = -1, Rflag = -1; while ((Lflag=IL.getIndexVal() )!= -1) { if (mresult == makeResult(Lflag, mright)) { print(Lflag, mright); } } while ((Rflag = IR.getIndexVal()) != -1) { if (mresult == makeResult(mleft, Rflag)) { print(mleft, Rflag); } } while ((Lflag = IL.getIndexVal()) != -1) { while ((Rflag = IR.getIndexVal()) != -1) { if (mresult == makeResult(Lflag,Rflag)) { print(Lflag,Rflag); } } } } void print(int left,int right) { cout << "is right" << endl; cout << left << msign << right << "=" << mresult << endl; } private: int mleft; int mright; char msign; int mresult; }; int main() { char buff[102] = { 0 }; cin >> buff; Match st(buff); return 0; }