【OJ练习】并行计算器

#include
#include
#include
#include
using namespace std;


typedef enum NumType
{
Add,
Multi,
REV
}enNumType;


typedef struct NumNode
{
enNumType OperatorPyte;
vector Num;
}strNumNode;


void CopyNumToQueue( char *InputChar, vector &NumQueue );
void SortNumQueue( vector &NumQueue );


int main()
{
char InputChar[100] = "1+7+3+2*3+4*7*5+9*2+4*3*2*6+4";
//char InputChar[100] = "1+3+2*3+4";
//char InputChar[100] = "1";
//char InputChar[100] = "2+3";
//char InputChar[100] = "2*3";


int IntputNum = sizeof(InputChar);
cout<

IntputNum = strlen(InputChar);
cout<

vector NumQueue;


/*将表达式数字按加法和乘法分别存入不同节点*/
CopyNumToQueue( InputChar, NumQueue);


while( ( NumQueue.size() > 1 ) || ( NumQueue[0].Num.size() > 1 ) )
{
/*节点内部数字排序,外部字典序排序*/
SortNumQueue( NumQueue );
}



return 0;
}


void CopyNumToQueue( char *InputChar, vector &NumQueue )
{
enNumType PreOperator = Add;
enNumType NextOperator;


int TempNum;
int MultiNum = 0;

strNumNode TempNumNode;
TempNumNode.OperatorPyte = Add;
NumQueue.push_back( TempNumNode );


for( int i = 0; i < strlen(InputChar); i += 2 )
{
TempNum = atoi( &InputChar[i] );


if( i == ( strlen(InputChar) - 1 ) )
{
if( PreOperator == Add )
{
NumQueue[0].Num.push_back(TempNum);
}
else
{
NumQueue[MultiNum].Num.push_back(TempNum);
}
}
else
{
/*判断下一个运算符*/
( InputChar[i+1] == '+' ) ? ( NextOperator = Add ) : ( NextOperator = Multi );


/* a+b+c 保存b */
if( ( PreOperator == Add ) && ( NextOperator == Add ) )
{
NumQueue[0].Num.push_back(TempNum);
}
/* a+b*c 保存b */
else if( ( PreOperator == Add ) && ( NextOperator == Multi ) )
{
TempNumNode.OperatorPyte = Multi;
NumQueue.push_back( TempNumNode );
MultiNum++;
NumQueue[MultiNum].Num.push_back(TempNum);
}
/* a*b*c 和 a*b+c 保存b */
else
{
NumQueue[MultiNum].Num.push_back(TempNum);
}


PreOperator = NextOperator;
}
}
}


bool SortFun(const strNumNode &Node1, const strNumNode &Node2 ) 
{
if( Node1.Num.size() < Node2.Num.size() )
{
return true;
}
else if( Node1.Num.size() == Node2.Num.size() )
{
for( int i = 0; i < Node1.Num.size(); i++ )
{
if( Node1.Num[i] < Node2.Num[i] )
{
return true;
}
else if( Node1.Num[i] > Node2.Num[i] )
{
return false;
}
else
{
}
}
return true;
}
else
{
return false;
}
}


void SortNumQueue( vector &NumQueue )
{
/*对所有节点内部的数字从小到大排序*/
for( int i = 0; i < NumQueue.size(); i++ )
{
sort( NumQueue[i].Num.begin(), NumQueue[i].Num.end());
}


/*对乘法节点进行字典序排序*/
sort( ( NumQueue.begin() + 1 ), NumQueue.end(), SortFun );
}

你可能感兴趣的:(OJ,练习)