![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//***************************
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
表达式模板
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
****************************/
#include<stack>
#include<string>
#include<iostream>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int signMap[7][7]=
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{'>','>','<','<','<','>','>'}, // +
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{'>','>','<','<','<','>','>'}, // -
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{'>','>','>','>','<','>','>'}, // *
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{'>','>','>','>','<','>','>'}, // /
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{'<','<','<','<','<','=','#'}, // (
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{'>','>','>','>','#','>','>'}, // )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{'<','<','<','<','<','#','='}, // #
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
char element[7]=
{'+','-','*','/','(',')','#'};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char Precede(char oper1,char oper2)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i,j;
for(i=0;i<7;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(element[i]==oper1) break;
}
for(j=0;j<7;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(element[j]==oper2) break;
}
return signMap[i][j];
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int Operate(int a,char oper,int b)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
switch(oper)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a/b;
break;
default:
cerr<<"Operate Error"<<endl;
return 0;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int Calculate(string &str)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int a,b,t2;
char oper;
stack<int> opnd;
stack<char> optr;
optr.push('#');
str=str+'#';
int index=0;
while(optr.top()!='#'||str[index]!='#')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while(str[index]==' ') index++;
if(!(str[index]=='+'||str[index]=='-'||
str[index]=='*'||str[index]=='('||str[index]==')'||str[index]=='#'))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
opnd.push(str[index++]-'0');
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
char t=Precede(optr.top(),str[index]);
switch(t)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case '<':
optr.push(str[index++]);
break;
case '=':
optr.pop();
index++;
break;
case '>':
oper=optr.top();
optr.pop();
a=opnd.top();
opnd.pop();
b=opnd.top();
opnd.pop();
t2=Operate(b,oper,a);
opnd.push(t2);
break;
default:
cerr<<"Operate Error"<<endl;
return 0;
}
}
}
return opnd.top();
}
posted on 2009-04-17 21:00
姚冰 阅读(260)
评论(0) 编辑 收藏 引用