運算符重載之火柴棒問題

題目:
用火柴棒擺成的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表手動進行數據存儲時,代碼過於繁瑣,此種情況,使用二維數組更爲清晰明瞭。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章