运算符重载笔试题——火柴棒问题

来源:互联网 发布:笔记本主板交换 知乎 编辑:程序博客网 时间:2024/06/05 15:56
题目:
用火柴棒摆成的0-9间的数字,横向由一根火柴构成,纵向都是由两根火柴构成,
可按如下规则进行变化:
1.数字移动一根火柴棒可变成其它0到9之间合法的数字
2.数字添加一根火柴棒可变成其它0到9之间合法的数字
3.数字去掉一根火柴棒可变成其它0到9之间合法的数字
现在给出一个带有两个操作数的+,-,*,/的算术式子,需要你判断该式子是否
成立,如给出式子9-6=3,这个符合正常结果,正确;如给出式子
9-6=2,其判断规则如下:
1.变换左操作数可以使式子成立的话,打印出来变化后的算术等式
2.变换右操作数可以使式子成立的话,打印出来变化后的算术等式
3.变化左右操作数可以使式子成立的话,打印出来变化后的算术等式
4.以上变化都无法让等式成立,打印等式无法成立
完成代码注意事项:
1.请用C++面向对象语言来完成代码书写,书写代码要依
据编程规范,代码质量和代码功能实现各占一半的分数。

2.不考虑负数的情况

方案一:使用map表存储

#include <vector>#include <iterator>#include <map>#include <iostream>using namespace std;static map<int,vector<int>>initmap(){map<int,vector<int>> mymap;vector<int> vec;vec.push_back(8);vec.push_back(6);vec.push_back(9);mymap[0]=vec;vec.clear();vec.push_back(7);mymap[1]=vec;vec.clear();vec.push_back(3);mymap[2]=vec;vec.clear();vec.push_back(9);vec.push_back(2);vec.push_back(5);mymap[3]=vec;vec.clear();mymap[4]=vec;vec.clear();vec.push_back(6);vec.push_back(9);vec.push_back(3);mymap[5]=vec;vec.clear();vec.push_back(8);vec.push_back(5);vec.push_back(9);vec.push_back(0);mymap[6]=vec;vec.clear();vec.push_back(1);mymap[7]=vec;vec.clear();vec.push_back(6);vec.push_back(9);vec.push_back(0);mymap[8]=vec;vec.clear();vec.push_back(8);vec.push_back(3);vec.push_back(5);vec.push_back(6);vec.push_back(0);mymap[9]=vec;vec.clear();return mymap;}class CMatch{public:void parse(char *input){char *p = input;mleft = *p-'0';p++;msign = *p;p++;mright = *p-'0';p++;p++;mresult = *p-'0';//cout<<mleft<<msign<<mright<<"="<<mresult<<endl;}int makeResult(int left, char sign, int right){switch(sign){case '+':return left+right;case '-':return left-right;case '*':return left*right;case '/':return left/right;default:throw "invalid sign!";break;}}void match(){int result = 0;result = makeResult(mleft, msign, mright);if(mresult == result){cout<<"no change!"<<endl;cout<<mleft<<msign<<mright<<"="<<result<<endl;return;}else{map<int,vector<int>>::iterator it;vector<int> vec_left;vector<int> vec_right;for (it = mmap.begin();it != mmap.end();++it){if(mleft == it->first){vec_left = it->second;for (vector<int>::iterator vecit = vec_left.begin();vecit != vec_left.end();++vecit){if (makeResult(*vecit, msign, mright) == mresult){cout<<"change left number success!"<<endl;cout<<*vecit<<msign<<mright<<"=" <<mresult<<endl;return ;}}}}for (it = mmap.begin();it != mmap.end();++it){if(mright == it->first){vec_right = it->second;for (vector<int>::iterator vecit = vec_right.begin();vecit != vec_right.end();++vecit){if (makeResult(mleft, msign, *vecit) == mresult){cout<<"change right number success!"<<endl;cout<<mleft<<msign<<*vecit<<"=" <<mresult<<endl;return ;}}}}bool flag_left = false;bool flag_right = false;for (it = mmap.begin();it != mmap.end();++it){if(mleft == it->first){vec_left = it->second;flag_left = true;}if(mright == it->first){vec_right = it->second;flag_right = true;}if(flag_left && flag_right){for (vector<int>::iterator vec_left_it = vec_left.begin();vec_left_it != vec_left.end();++vec_left_it){for (vector<int>::iterator vec_right_it = vec_right.begin();vec_right_it != vec_right.end();++vec_right_it){if (makeResult(*vec_left_it, msign, *vec_right_it) == mresult){cout<<"change left and right number success!"<<endl;cout<<*vec_left_it<<msign<<*vec_right_it<<"=" <<mresult<<endl;return ;}}}}}}cout<<"no valid change!"<<endl;}private:int mleft;int mright;char msign;int mresult;static map<int,vector<int>> mmap;};map<int,vector<int>> CMatch::mmap = initmap();int main(){CMatch match;cout<<"input string:"<<endl;char buffer[1024]={0};cin>>buffer;  match.parse(buffer);match.match();return 0;}


方案二:使用二维数组

#include <iostream>using namespace std;class CInt{public:CInt(int num=0):mvalue(num){}void setValue(int value){mvalue = value;}void beginChange(){mindex = 0;}bool bcanChange(){return mNumMap[mvalue][mindex] != -1;}int nextValue(){return mNumMap[mvalue][mindex++];}operator int(){return mvalue;}private:int mvalue;//真实值int mindex;//对应变换值的索引static int mNumMap[10][10];//变换表friend ostream& operator<<(ostream &out, const CInt &intObj);};int CInt::mNumMap[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}};ostream& operator<<(ostream &out, const CInt &intObj){out<<intObj.mvalue;return out;}class CMatch{public:void parse(char *input){char *p = input;mleft.setValue(*p-'0');p++;msign = *p;p++;mright.setValue(*p-'0');p++;p++;mresult.setValue(*p-'0');//cout<<mleft<<msign<<mright<<"="<<mresult<<endl;}int makeResult(int left, char sign, int right){switch(sign){case '+':return left+right;case '-':return left-right;case '*':return left*right;case '/':return left/right;default:throw "invalid sign!";break;}}void match(){int value = 0;int result = 0;result = makeResult(mleft, msign, mright);if(mresult == result){cout<<"no change!"<<endl;cout<<mleft<<msign<<mright<<"="<<result<<endl;return;}mleft.beginChange();while(mleft.bcanChange()){value = mleft.nextValue();result = makeResult(value, msign, mright);if(mresult == result){cout<<"change left number success!"<<endl;cout<<value<<msign<<mright<<"="<<result<<endl;return;}}mright.beginChange();while(mright.bcanChange()){value = mright.nextValue();result = makeResult(mleft, msign, value);if(mresult == result){cout<<"change right number success!"<<endl;cout<<mleft<<msign<<value<<"="<<result<<endl;return;}}int leftval = 0;int rightval = 0;mleft.beginChange();while(mleft.bcanChange()){leftval = mleft.nextValue();mright.beginChange();while(mright.bcanChange()){rightval = mright.nextValue();result = makeResult(leftval, msign, rightval);if(mresult == result){cout<<"change left and right number success!"<<endl;cout<<leftval<<msign<<rightval<<"="<<result<<endl;return;}}}cout<<"no valid change!"<<endl;}private:CInt mleft;CInt mright;char msign;CInt mresult;};int main(){CMatch match;cout<<"input string:"<<endl;char buffer[1024]={0};cin>>buffer;   // cin>>scanf  "hello world"   gets()  cin.getline()match.parse(buffer);match.match();return 0;}

此处运行结果不再粘贴,使用map表手动进行数据存储时,代码过于繁琐,此种情况,使用二维数组更为清晰明了。


0 0
原创粉丝点击