# 居然找到自己很早很早24点计算的程序 赶紧纪念一下

#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <assert.h>
class StorePoint
{
public:

StorePoint() : mMolecule(0),mDenominator(1) {}
StorePoint( int i ) { mMolecule = i ; }
StorePoint( int i, int j ) ;
StorePoint( StorePoint& ) {};
StorePoint& operator + ( StorePoint& param );
StorePoint& operator - ( StorePoint& param );
StorePoint& operator * ( StorePoint& param );
StorePoint& operator / ( StorePoint& param );
StorePoint& operator = ( StorePoint& param );
void Init() ;
int GetRes() { return mMolecule % 24 == 0 ? mMolecule/mDenominator : mMolecule % 24; };
int SetData( int i, int j = 1 ) ;

private:
int mMolecule;
int mDenominator;
};

StorePoint::StorePoint( int i, int j )
{
mMolecule = i;
mDenominator = j;

}

void StorePoint::Init()
{
mMolecule = 0;
mDenominator = 1;
}

StorePoint& StorePoint::operator+( StorePoint& param )
{
mMolecule = param.mMolecule*mDenominator + mMolecule*param.mDenominator ;
mDenominator *= param.mDenominator;
return *this;
};

StorePoint& StorePoint::operator-( StorePoint& param )
{
mMolecule = mMolecule*param.mDenominator - param.mMolecule*mDenominator;
mDenominator *= param.mDenominator;
return *this;
};

StorePoint& StorePoint::operator*( StorePoint& param )
{
mMolecule *= param.mMolecule;
mDenominator *= param.mDenominator;
return *this;
};

StorePoint& StorePoint::operator/( StorePoint& param )
{
if( param.mMolecule == 0 )
{
mMolecule = 0;
mDenominator  = 1;
}
else
{
mMolecule *= param.mDenominator;
mDenominator *= param.mMolecule;
}
return *this;
}

StorePoint& StorePoint::operator=( StorePoint& param )
{
mMolecule += param.mMolecule;
mDenominator *= param.mDenominator;
return *this;
}

int StorePoint::SetData( int i, int j )
{
assert( j!= 0 );
mMolecule = i;
mDenominator = j;

return 1;
}
StorePoint& CaculaorObject( StorePoint& First, StorePoint& Sencond, char CaluFlag )
{
switch(CaluFlag)
{
case '+':
return ( First + Sencond );
break;
case '-':
return( First - Sencond );
break;
case '*':
return( First * Sencond );
break;
case '/':
return( First / Sencond );
break;
default:
break;
}
}

void GetLastRes( StorePoint& First, StorePoint& Second, StorePoint& Third, StorePoint& Forth )
{
StorePoint Result, Temp1, Temp2,Temp;
StorePoint l_first, l_second, l_third, l_forth;
char Sign[4] = { '+', '-', '*', '/' };
int i = 0, j = 0, k = 0;

for( i = 0; i < 4; i++ )
{
for( j = 0; j < 4; j++ )
{
for( k = 0; k < 4; k++ )
{
Temp1.Init();
Temp2.Init();
Result.Init();
memcpy( &l_first, &First, sizeof(StorePoint) );
memcpy( &l_second, &Second, sizeof(StorePoint) );
memcpy( &l_third, &Third, sizeof(StorePoint) );
memcpy( &l_forth, &Forth, sizeof(StorePoint) );
Temp2 = CaculaorObject( l_third,  l_forth, Sign[k]  );
Temp1 = CaculaorObject( l_second, Temp2, Sign[j] );
Result = CaculaorObject( l_first, Temp1, Sign[i] );
if( Result.GetRes() == 24 )
cout<<First.GetRes()<<Sign[i]<<"("<<Second.GetRes()<<Sign[j]<<"("<<Third.GetRes()<<Sign[k]<<Forth.GetRes()<<"))"<<endl;

Temp1.Init();
Temp2.Init();
Result.Init();
memcpy( &l_first, &First, sizeof(StorePoint) );
memcpy( &l_second, &Second, sizeof(StorePoint) );
memcpy( &l_third, &Third, sizeof(StorePoint) );
memcpy( &l_forth, &Forth, sizeof(StorePoint) );

Result.Init();
Temp2 = CaculaorObject( l_third,  l_forth, Sign[k]  );
Temp1 = CaculaorObject( Temp2, l_second, Sign[j] );
Result = CaculaorObject( l_first, Temp1, Sign[i] );
if( Result.GetRes() == 24 )
cout<<First.GetRes()<<Sign[i]<<"("<<"("<<Third.GetRes()<<Sign[k]<<Forth.GetRes()<<")"<<Sign[j]<<Second.GetRes()<<")"<<endl;

Temp1.Init();
Temp2.Init();
Result.Init();
memcpy( &l_first, &First, sizeof(StorePoint) );
memcpy( &l_second, &Second, sizeof(StorePoint) );
memcpy( &l_third, &Third, sizeof(StorePoint) );
memcpy( &l_forth, &Forth, sizeof(StorePoint) );

Result.Init();
Temp2 = CaculaorObject( l_third,  l_forth, Sign[k]  );
Temp1 = CaculaorObject( Temp2, l_second, Sign[j] );
Result = CaculaorObject(  Temp1, l_first, Sign[i] );
if( Result.GetRes() == 24 )
cout<<"("<<"("<<Third.GetRes()<<Sign[k]<<Forth.GetRes()<<")"<<Sign[j]<<Second.GetRes()<<")" <<Sign[i]<<First.GetRes()<<endl;

Temp1.Init();
Temp2.Init();
Result.Init();
memcpy( &l_first, &First, sizeof(StorePoint) );
memcpy( &l_second, &Second, sizeof(StorePoint) );
memcpy( &l_third, &Third, sizeof(StorePoint) );
memcpy( &l_forth, &Forth, sizeof(StorePoint) );
Temp2 = CaculaorObject( l_third,  l_forth, Sign[k]  );
Temp1 = CaculaorObject( l_second, Temp2, Sign[j] );
Result = CaculaorObject(  Temp1, l_first, Sign[i] );
if( Result.GetRes() == 24 )
cout<<"("<<Second.GetRes()<<Sign[j]<<"("<<Third.GetRes()<<Sign[k]<<Forth.GetRes()<<"))"<<First.GetRes()<<Sign[i]<<endl;

}
}
}
}

void Operate4Data( StorePoint* Data )
{
int i = 0, j = 0, k = 0, t = 0;

for( i = 0; i < 4; i++ )
{
for( j = 0; j < 4; j++ )
{
if( j != i )
{
for( k = 0; k < 4; k++ )
{
if( (k != i) && (k != j) )
{
for( t = 0; t < 4; t++ )
{
if( (t != i) && (t != j) && (t != k) )
GetLastRes( Data[i], Data[j], Data[k], Data[t] );
else
continue;
}

}
else
continue;
}
}
else
continue;
}
}
}

void main()
{
StorePoint Data[4];
Data[0].SetData(3);
Data[1].SetData(3);
Data[2].SetData(8);
Data[3].SetData(8);

Operate4Data( Data );
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120