1.全排列(通过加括号让它从左到右运算)
2.除法分顺序
3.加括号
a.左邻括号的运算符为“/”,则括号必须保留,即…/(s1 op s2)…形式。左邻括号的预算符为“*”或“-”。
b.而op为“+”或“-”,则保留括号,即…*(s1+s2)…或…-(s1+s2)…或…*(s1-s2)…或…-(s1-s2)…。
c.右邻括号的运算符为“*”或“/”,而op为“+”或“-”,原式中的op运算必须优先进行,因此括号不去除,即(s1+s2)*…除上述情况外,可以括号去除.。
#include<iostream>
#include<string>
#include<set>
#include<strstream>
using namespace std;
typedef struct Node
{
char notation;
int Numerator, Denominator;
string expression;
}NODE ;
NODE Num[4];
set<string> FinalStrings;
int count;
NODE operator + ( const NODE x, const NODE y )
{
NODE temp;
temp.notation = '+';
temp.Numerator = x.Numerator * y.Denominator + y.Numerator * x.Denominator;
temp.Denominator = x.Denominator * y.Denominator;
temp.expression = x.expression + " + " + y.expression;
return temp;
}
NODE operator - ( const NODE x, const NODE y )
{
NODE temp;
temp.notation = '-';
temp.Numerator = x.Numerator * y.Denominator - y.Numerator * x.Denominator;
temp.Denominator = x.Denominator * y.Denominator;
if( y.notation == '+' || y.notation == '-' )
temp.expression = x.expression + " - " + '(' + y.expression + ')';
else
temp.expression = x.expression + " - " + y.expression;
return temp;
}
NODE operator * ( const NODE x, const NODE y )
{
NODE temp;
temp.notation = '*';
temp.Numerator = x.Numerator * y.Numerator;
temp.Denominator = x.Denominator * y.Denominator;
if( x.notation == '+' || x.notation == '-' )
temp.expression = '(' + x.expression + ')' + " x " + y.expression;
else if(y.notation == '+' || y.notation == '-' )
temp.expression = x.expression + " x " + '(' + y.expression + ')';
else if( x.notation == '+' || x.notation == '-' && y.notation == '+' || y.notation == '-' )
temp.expression = '(' + x.expression + ')' + " x " + '(' + y.expression + ')';
else
temp.expression = x.expression + " x " + y.expression;
return temp;
}
NODE operator / ( const NODE x, const NODE y )
{
NODE temp;
temp.notation = '/';
temp.Numerator = x.Numerator * y.Denominator;
temp.Denominator = y.Numerator * x.Denominator;
if( x.notation == '+' || x.notation == '-' )
temp.expression = '(' + x.expression + ')' + " / " + y.expression;
else if( y.notation )
temp.expression = x.expression + " / " + '(' + y.expression + ')';
else
temp.expression = x.expression + " / " + y.expression;
return temp;
}
void copy( NODE *x, const NODE y )
{
x -> expression = y.expression;
x -> Denominator = y. Denominator;
x -> Numerator = y.Numerator;
x -> notation = y.notation;
}
void Match( int n )
{
if( n == 1 )
{
if( Num[0].Numerator / Num[0].Denominator == 24 && !(Num[0].Numerator % Num[0].Denominator) && FinalStrings.find( Num[0].expression )==FinalStrings.end() )
{
FinalStrings.insert( Num[0].expression );
cout << Num[0].expression << endl;
count ++;
}
return ;
}
for( int i = 0; i < n; i++ )
for( int j = i + 1; j < n; j++ )
{
NODE x, y;
copy( &x, Num[i] );copy( &y, Num[j] );
if( j != n - 1 ) copy( &Num[j], Num[n-1]);
copy( & Num[i], x + y ); Match( n - 1 );
copy( & Num[i], x - y ); Match( n - 1 );
copy( & Num[i], y - x ); Match( n - 1 );
copy( & Num[i], x * y ); Match( n - 1 );
if(y.Numerator)copy( & Num[i], x / y ); Match( n - 1 );
if(x.Numerator)copy( & Num[i], y / x ); Match( n - 1 );
copy(&Num[i] , x);copy(& Num[j] , y);
}
}
int main()
{
strstream part;
while(1)
{
cout << "Please enter the four integers separated by a space: " << endl;
for( int i = 0; i < 4; i++ )
{
cin >> Num[i].Numerator;
Num[i].Denominator = 1;
Num[i].notation = NULL;
part << Num[i].Numerator;
part >> Num[i].expression;
part.clear();
}
count = 0;
Match( 4 );
if( !count )
cout << "No answer !" << endl;
}
}