用火柴棒摆成的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; }