主题:计算表达式,请大家指正
#include "Test.hpp"
//
typedef struct Node
{
bool IsSymbol;
bool Useful;
char Symbol;
int SymbolLevel;
float Left;
float Right;
float Sum;
}NODE,*PNODE;
//
int IsSymbol(char *);
float GetNum(char *,char *);
bool SortFunc(NODE,NODE);
float Calculate(float,float,char);
//
int main(int argc,char *argv[])//程序入口处
{
NODE TempNode;
char StrBuffer[64]={0};
char TempBuffer[64]={0};
char *p=StrBuffer;
int SymbolLevel=64;
float Result=0;
vector<NODE> Symbolvector;
vector<NODE>::iterator Ite;
//
cin>>StrBuffer;
for(int i=0;i<strlen(StrBuffer);i++)
{
if(IsSymbol(StrBuffer+i)==1)
{
TempNode.IsSymbol=false;
TempNode.Useful=true;
TempNode.Symbol=0;
TempNode.SymbolLevel=0;
TempNode.Left=0;
TempNode.Right=0;
TempNode.Sum=GetNum(p,StrBuffer+i);
Symbolvector.push_back(TempNode);
if(*(StrBuffer+i)==';')
{
break;
}
TempNode.IsSymbol=true;
TempNode.Useful=true;
TempNode.Symbol=*(StrBuffer+i);
if(TempNode.Symbol=='*'||TempNode.Symbol=='/')
{
TempNode.SymbolLevel=SymbolLevel+32;
}
else if(TempNode.Symbol=='+'||TempNode.Symbol=='-')
{
TempNode.SymbolLevel=SymbolLevel;
}
TempNode.Left=0;
TempNode.Right=0;
TempNode.Sum=0;
Symbolvector.push_back(TempNode);
p=StrBuffer+i+1;
SymbolLevel-=1;
}
else if(IsSymbol(StrBuffer+i)==2)
{
SymbolLevel+=64;
p=StrBuffer+i+1;
}
else if(IsSymbol(StrBuffer+i)==3)
{
SymbolLevel-=64;
*(StrBuffer+i)=' ';
}
}
for(Ite=Symbolvector.begin();Ite!=Symbolvector.end();Ite++)
{
if(Ite->IsSymbol==true)
{
Ite->Left=(Ite-1)->Sum;
Ite->Right=(Ite+1)->Sum;
}
}
for(Ite=Symbolvector.begin();Ite!=Symbolvector.end();Ite++)
{
if(Ite->IsSymbol==true)
{
cout<<Ite->Left<<"("<<Ite->Symbol<<Ite->SymbolLevel<<")"<<Ite->Right<<" ";
}
}
cout<<endl;
Ite=Symbolvector.begin();
while(Ite!=Symbolvector.end())
{
if(Ite->IsSymbol==false)
{
Symbolvector.erase(Ite);
}
else
{
Ite+=1;
}
}
cout<<"There are "<<Symbolvector.size()<<" Nodes."<<endl;
/**************************处理Vector***************************/
while(!Symbolvector.empty())
{
Ite=max_element(Symbolvector.begin(),Symbolvector.end(),SortFunc);
Ite->Sum=Calculate(Ite->Left,Ite->Right,Ite->Symbol);
if(Ite>Symbolvector.begin()&&Ite<Symbolvector.end())
{
(Ite-1)->Right=Ite->Sum;
(Ite+1)->Left=Ite->Sum;
}
else if(Ite==Symbolvector.begin())
{
(Ite+1)->Left=Ite->Sum;
}
else if(Ite==Symbolvector.end())
{
(Ite-1)->Right=Ite->Sum;
}
Result=Ite->Sum;
for(vector<NODE>::iterator i=Symbolvector.begin();i!=Symbolvector.end();i++)
{
cout<<i->Left<<","<<i->Symbol<<","<<i->Right<<endl;
}
Symbolvector.erase(Ite);
}
cout<<Result<<endl;
Symbolvector.clear();
return 0;
}
//
int IsSymbol(char *p)
{
if(*p=='+'||*p=='-'||*p=='*'||*p=='/'||*p==';')
{
return 1;
}
else if(*p=='(')
{
return 2;
}
else if(*p==')')
{
return 3;
}
return 0;
}
//
float GetNum(char *Ps,char *Pe)
{
char TempBuffer[64]={0};
memset(TempBuffer,0,64);
memcpy(TempBuffer,Ps,Pe-Ps);
vector<char> CharVector(TempBuffer,TempBuffer+64);
replace(CharVector.begin(),CharVector.end(),'(',' ');
replace(CharVector.begin(),CharVector.end(),')',' ');
return atof(TempBuffer);
}
//
bool SortFunc(NODE N1,NODE N2)
{
if(N1.SymbolLevel<N2.SymbolLevel)
{
return true;
}
else
{
return false;
}
}
//
float Calculate(float i,float j,char k)
{
if(k=='+')
{
return i+j;
}
else if(k=='-')
{
return i-j;
}
else if(k=='*')
{
return i*j;
}
else if(k=='/')
{
return i/j;
}
return 0.0;
}
//
//
typedef struct Node
{
bool IsSymbol;
bool Useful;
char Symbol;
int SymbolLevel;
float Left;
float Right;
float Sum;
}NODE,*PNODE;
//
int IsSymbol(char *);
float GetNum(char *,char *);
bool SortFunc(NODE,NODE);
float Calculate(float,float,char);
//
int main(int argc,char *argv[])//程序入口处
{
NODE TempNode;
char StrBuffer[64]={0};
char TempBuffer[64]={0};
char *p=StrBuffer;
int SymbolLevel=64;
float Result=0;
vector<NODE> Symbolvector;
vector<NODE>::iterator Ite;
//
cin>>StrBuffer;
for(int i=0;i<strlen(StrBuffer);i++)
{
if(IsSymbol(StrBuffer+i)==1)
{
TempNode.IsSymbol=false;
TempNode.Useful=true;
TempNode.Symbol=0;
TempNode.SymbolLevel=0;
TempNode.Left=0;
TempNode.Right=0;
TempNode.Sum=GetNum(p,StrBuffer+i);
Symbolvector.push_back(TempNode);
if(*(StrBuffer+i)==';')
{
break;
}
TempNode.IsSymbol=true;
TempNode.Useful=true;
TempNode.Symbol=*(StrBuffer+i);
if(TempNode.Symbol=='*'||TempNode.Symbol=='/')
{
TempNode.SymbolLevel=SymbolLevel+32;
}
else if(TempNode.Symbol=='+'||TempNode.Symbol=='-')
{
TempNode.SymbolLevel=SymbolLevel;
}
TempNode.Left=0;
TempNode.Right=0;
TempNode.Sum=0;
Symbolvector.push_back(TempNode);
p=StrBuffer+i+1;
SymbolLevel-=1;
}
else if(IsSymbol(StrBuffer+i)==2)
{
SymbolLevel+=64;
p=StrBuffer+i+1;
}
else if(IsSymbol(StrBuffer+i)==3)
{
SymbolLevel-=64;
*(StrBuffer+i)=' ';
}
}
for(Ite=Symbolvector.begin();Ite!=Symbolvector.end();Ite++)
{
if(Ite->IsSymbol==true)
{
Ite->Left=(Ite-1)->Sum;
Ite->Right=(Ite+1)->Sum;
}
}
for(Ite=Symbolvector.begin();Ite!=Symbolvector.end();Ite++)
{
if(Ite->IsSymbol==true)
{
cout<<Ite->Left<<"("<<Ite->Symbol<<Ite->SymbolLevel<<")"<<Ite->Right<<" ";
}
}
cout<<endl;
Ite=Symbolvector.begin();
while(Ite!=Symbolvector.end())
{
if(Ite->IsSymbol==false)
{
Symbolvector.erase(Ite);
}
else
{
Ite+=1;
}
}
cout<<"There are "<<Symbolvector.size()<<" Nodes."<<endl;
/**************************处理Vector***************************/
while(!Symbolvector.empty())
{
Ite=max_element(Symbolvector.begin(),Symbolvector.end(),SortFunc);
Ite->Sum=Calculate(Ite->Left,Ite->Right,Ite->Symbol);
if(Ite>Symbolvector.begin()&&Ite<Symbolvector.end())
{
(Ite-1)->Right=Ite->Sum;
(Ite+1)->Left=Ite->Sum;
}
else if(Ite==Symbolvector.begin())
{
(Ite+1)->Left=Ite->Sum;
}
else if(Ite==Symbolvector.end())
{
(Ite-1)->Right=Ite->Sum;
}
Result=Ite->Sum;
for(vector<NODE>::iterator i=Symbolvector.begin();i!=Symbolvector.end();i++)
{
cout<<i->Left<<","<<i->Symbol<<","<<i->Right<<endl;
}
Symbolvector.erase(Ite);
}
cout<<Result<<endl;
Symbolvector.clear();
return 0;
}
//
int IsSymbol(char *p)
{
if(*p=='+'||*p=='-'||*p=='*'||*p=='/'||*p==';')
{
return 1;
}
else if(*p=='(')
{
return 2;
}
else if(*p==')')
{
return 3;
}
return 0;
}
//
float GetNum(char *Ps,char *Pe)
{
char TempBuffer[64]={0};
memset(TempBuffer,0,64);
memcpy(TempBuffer,Ps,Pe-Ps);
vector<char> CharVector(TempBuffer,TempBuffer+64);
replace(CharVector.begin(),CharVector.end(),'(',' ');
replace(CharVector.begin(),CharVector.end(),')',' ');
return atof(TempBuffer);
}
//
bool SortFunc(NODE N1,NODE N2)
{
if(N1.SymbolLevel<N2.SymbolLevel)
{
return true;
}
else
{
return false;
}
}
//
float Calculate(float i,float j,char k)
{
if(k=='+')
{
return i+j;
}
else if(k=='-')
{
return i-j;
}
else if(k=='*')
{
return i*j;
}
else if(k=='/')
{
return i/j;
}
return 0.0;
}
//