#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <string>
#include <iostream>
#include <list>
#include <vector>
#include <QTextStream>//cin/cout 必须用的。
//#include "algorithm"
#include <algorithm>
using namespace std;//vector是属于std命名域的,因此需要通过命名限定
class Expression
{
public :
bool execCmp(string expression);
int execCalculator(string expression);
list<string> strsplit(string expression,string token);//表达式,分隔符
string strval;
int intval;
Expression()
{
strval="";
intval=0;
};
~Expression()
{
};
};
list<string>Expression::strsplit(string expression,string seps1)//表达式,分隔符
{
list<string> listtoken;
char str1[expression.length()];
sprintf(str1,"%s",expression.c_str());
//char seps[] = " /t/n";
char seps[seps1.length()];
sprintf(seps,"%s",seps1.c_str());
char *token;
string strtoken;
QTextStream cout(stdout, QIODevice::WriteOnly);
// printf( "%s/n/nTokens:/n", str1 );
/* Establish string and get the first token: */
token = strtok( str1, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
strtoken=token;
//cout<<"aaa"<<strtoken.c_str()<<endl;
listtoken.push_back(strtoken);
//printf( " %s/n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
listtoken.push_back("#");
return listtoken;
}
/*
auther:symbol
*/
int Expression::execCalculator(string expression)//使用标准C++ string
{
//第一步:表达式分解,分类存储,例表达式:"Title1 > '3'",分解Title1,>,'3'。
QTextStream cout(stdout, QIODevice::WriteOnly);
cout<<expression.c_str()<<endl;
vector<string> Stack1;//变量及数字栈 //vector末端看为栈顶
vector<string> Stack2;//运算符栈 //vector末端看为栈顶
vector<string> Stack3;//运算符优先级栈 //
try
{
while(expression.find(" ")>0)//替换多余的空格
{
int ll_pos=expression.find(" ");
//printf( " %s/n", expression.c_str() );
expression=expression.replace(ll_pos,2," ");
}
}catch(...){}
Stack3.push_back(")");//优先级由高到底//0
Stack3.push_back("*");//1
Stack3.push_back("/");//1
Stack3.push_back("+");//3
Stack3.push_back("-");//3
Stack3.push_back("(");//5
Stack3.push_back("#");//结束符优先级低//6
list<string> Expressionlist;
Expressionlist=strsplit(expression," ");
cout<<"表达式已分解为TOKEN"<<endl;
for(list <string>::iterator it = Expressionlist.begin (); it != Expressionlist.end (); it ++)//解析后的字符串
{
cout<<"Token="<<it->c_str()<<endl;
if(strcmp(it->c_str(),"(")==0 || strcmp(it->c_str(),")")==0 || strcmp(it->c_str(),"+")==0 ||strcmp(it->c_str(),"-")==0 ||strcmp(it->c_str(),"*")==0||strcmp(it->c_str(),"/")==0||strcmp(it->c_str(),"#")==0)
{
//先和运算符栈栈顶进行优先级比较
//cout<<"RUN HERE 1"<<endl;
if(Stack2.empty())
{
Stack2.push_back(it->c_str());
continue;
}
cout<<"Stack2="<<Stack2.at(Stack2.size()-1).c_str()<<endl;//vector Stack2.end()最后指针指向空
// vector<string>::iterator f1 = find( Stack3.begin( ), Stack3.end( ), Stack2.at(Stack2.size()-1).c_str() ); //查找Stack2.end()栈顶优先级
// vector<string>::iterator f2 = find( Stack3.begin( ), Stack3.end( ), it->c_str() ); //查找it当前运算符优先级
int li_yxj_f1=6;//初始优先级//栈顶运算符
int li_yxj_f2=6;//初始优先级//当前运算符
switch(Stack2.at(Stack2.size()-1).c_str()[0])//运算符号都是单字符.switch只判断一次,必须加break;
{
case ')':
li_yxj_f1=0;
break;
case '*':
li_yxj_f1=1;
break;
case '/':
li_yxj_f1=1;
break;
case '+':
li_yxj_f1=3;
break;
case '-':
li_yxj_f1=3;
break;
case '(':
li_yxj_f1=5;
break;
case '#':
li_yxj_f1=6;
break;
}
switch(it->c_str()[0])//运算符号都是单字符
{
case ')':
li_yxj_f2=0;
break;
case '*':
li_yxj_f2=1;
break;
case '/':
li_yxj_f2=1;
break;
case '+':
li_yxj_f2=3;
break;
case '-':
li_yxj_f2=3;
break;
case '(':
li_yxj_f2=5;
break;
case '#':
li_yxj_f2=6;
break;
}
cout<<"Stack2.at(Stack2.size()-1).c_str()[0]="<<Stack2.at(Stack2.size()-1).c_str()[0]<<" it->c_str()[0]="<<it->c_str()[0]<<endl;
cout<<"li_yxj_f2="<<li_yxj_f2<<" li_yxj_f1="<<li_yxj_f1<<endl;
if(strcmp(it->c_str(),"(")==0)//扫描到左括号时无条件压入符号栈
{
Stack2.push_back(it->c_str());//
cout<<"PUSH Stack2"<<it->c_str()<<endl;
continue;
}
//if(f2<f1)//数字越小优先级越大,f1找不到的话Stack2就是空栈,f1指向迭代器的尾部
if(li_yxj_f2<li_yxj_f1)//数字越小优先级越大,f1找不到的话Stack2就是空栈,f1指向迭代器的尾部
{
if(strcmp(it->c_str(),")")!=0)
{
Stack2.push_back(it->c_str());//当前优先级高,运算符压栈
cout<<"PUSH Stack2"<<it->c_str()<<endl;
}
else
{
string ls_pop1="";
string ls_pop2="";
while(strcmp(Stack2.at(Stack2.size()-1).c_str(),"(")!=0)//弹栈直到遇到'('
{
double result=0;
ls_pop1=Stack1.at(Stack1.size()-2);
ls_pop2=Stack1.at(Stack1.size()-1);
cout<<"ls_pop1="<<ls_pop1.c_str()<<endl;
cout<<"ls_pop2="<<ls_pop2.c_str()<<endl;
cout<<"Stack2.at(Stack2.size()-1).c_str()[0]="<<Stack2.at(Stack2.size()-2).c_str()[0]<<endl;
switch(Stack2.at(Stack2.size()-1).c_str()[0])//括号内做运算
{
case '*':
result=atof(ls_pop1.c_str())*atof(ls_pop2.c_str());
break;
case '/':
result=atof(ls_pop1.c_str())/atof(ls_pop2.c_str());
break;
case '+':
result=atof(ls_pop1.c_str())+atof(ls_pop2.c_str());
cout<<"run here temp()="<<result<<endl;
break;
case '-':
result=atof(ls_pop1.c_str())-atof(ls_pop2.c_str());
break;
}
cout<<"temp()="<<result<<endl;
Stack2.pop_back();//弹出一个运算符
Stack1.pop_back();//弹出一个数字
Stack1.pop_back();//再弹出一个数字
//Stack2.push_back(it->c_str());//弹完上一个运算符做计算,压入当前运算符。
char str[10] = {0};
sprintf(str, "%lf", result);
string ls_result="";
ls_result=str;
Stack1.push_back(ls_result);//运算结果压栈
int i=0;
i++;
if(i>10000)//限定循环数,避免由于表达式错误而死循环
{
break;
}
}//while 结束
cout<<"弹出一个左括号运算符("<<endl;
if(Stack2.size()>0)//栈空不必再弹了
{
Stack2.pop_back();//弹出一个左括号运算符
}
}
}
else//栈顶优先级高,运算符弹栈做运算
{
string ls_pop1="";
string ls_pop2="";
// cout<<"POP Stack2"<<endl;
cout<<"POP Stack2"<<Stack2.at(Stack2.size()-1).c_str()<<endl;
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"*")==0)
{
double result=0;
ls_pop1=Stack1.at(Stack1.size()-2);
ls_pop2=Stack1.at(Stack1.size()-1);
cout<<"temp*="<<endl;
result=atof(ls_pop1.c_str())*atof(ls_pop2.c_str());
cout<<"temp*="<<result<<endl;
Stack2.pop_back();//弹出一个运算符
Stack1.pop_back();//弹出一个数字
Stack1.pop_back();//再弹出一个数字
//Stack2.push_back(it->c_str());//弹完上一个运算符做计算,压入当前运算符。
char str[10] = {0};
sprintf(str, "%lf", result);
string ls_result="";
ls_result=str;
Stack1.push_back(ls_result);//运算结果压栈
it --;//下一次循环重新比较当前运算符(注意当前运算符未变)与新栈顶运算符
continue;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"/")==0)
{
double result=0;
ls_pop1=Stack1.at(Stack1.size()-2);
ls_pop2=Stack1.at(Stack1.size()-1);
cout<<"temp/="<<endl;
result=atof(ls_pop1.c_str())/atof(ls_pop2.c_str());
cout<<"temp/="<<result<<endl;
Stack2.pop_back();//弹出一个运算符
Stack1.pop_back();//弹出一个数字
Stack1.pop_back();//再弹出一个数字
//Stack2.push_back(it->c_str());//弹完上一个运算符做计算,压入当前运算符。
char str[10] = {0};
sprintf(str, "%lf", result);
string ls_result="";
ls_result=str;
Stack1.push_back(ls_result);//运算结果压栈
it --;//下一次循环重新比较当前运算符(注意当前运算符未变)与新栈顶运算符
continue;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"+")==0)
{
double result=0;
ls_pop1=Stack1.at(Stack1.size()-2);
ls_pop2=Stack1.at(Stack1.size()-1);
cout<<"temp+="<<ls_pop1.c_str()<<"+"<<ls_pop2.c_str()<<endl;
result=atof(ls_pop1.c_str())+atof(ls_pop2.c_str());
cout<<"temp+="<<result<<endl;
Stack2.pop_back();//弹出一个运算符
Stack1.pop_back();//弹出一个数字
Stack1.pop_back();//再弹出一个数字
//Stack2.push_back(it->c_str());//弹完上一个运算符做计算,压入当前运算符。
char str[10] = {0};
sprintf(str, "%lf", result);
string ls_result="";
ls_result=str;
Stack1.push_back(ls_result);//运算结果压栈
it --;//下一次循环重新比较当前运算符(注意当前运算符未变)与新栈顶运算符
continue;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"-")==0)
{
double result=0;
ls_pop1=Stack1.at(Stack1.size()-2);
ls_pop2=Stack1.at(Stack1.size()-1);
cout<<"temp-="<<endl;
result=atof(ls_pop1.c_str())-atof(ls_pop2.c_str());
cout<<"temp-="<<result<<endl;
Stack2.pop_back();//弹出一个运算符
Stack1.pop_back();//弹出一个数字
Stack1.pop_back();//再弹出一个数字
//Stack2.push_back(it->c_str());//弹完上一个运算符做计算,压入当前运算符。
char str[10] = {0};
sprintf(str, "%lf", result);
string ls_result="";
ls_result=str;
Stack1.push_back(ls_result);//运算结果压栈
it --;//下一次循环重新比较当前运算符(注意当前运算符未变)与新栈顶运算符
continue;
}
}
}
else
{
Stack1.push_back(it->c_str());//变量及数字栈
}
}
string ls_expression_result="";
for(vector <string>::iterator it = Stack1.begin (); it != Stack1.end (); it ++)
{
cout<<it->c_str();
}
cout<<endl;
for(vector <string>::iterator it = Stack2.begin (); it != Stack2.end (); it ++)
{
cout<<it->c_str();
}
cout<<endl;
ls_expression_result=Stack1.at(Stack1.size()-1).c_str();
return atof(ls_expression_result.c_str());
}
/*
auther:symbol
返回比较字符串表达式所运算的结果
校验:必须包含token{>,<,==,<=,>=,(,),||,&&,<>,!=,and,or}中的一种或几种
例: ( 字段1 > 3 ) && ( 字段2 < 6 )
优先级:关系运算符>逻辑运算符
算法分析:
第一种:类似算符优先
第二种:先按&& , || and or 把表达式拆分//按逻辑运算符拆分
然后把拆分后的表达式进行运算
最后合并运算
*/
bool Expression::execCmp(string expression)//使用标准C++ string
{
//第一步:表达式分解,分类存储,例表达式:"Title1 > '3'",分解Title1,>,'3'。
QTextStream cout(stdout, QIODevice::WriteOnly);
vector<string> Stack1;//变量及数字栈 //vector末端看为栈顶
vector<string> Stack2;//运算符栈 //vector末端看为栈顶
vector<string> Stack3;//运算符优先级栈 //
try
{
while(expression.find(" ")>0)//替换多余的空格
{
int ll_pos=expression.find(" ");
//printf( " %s/n", expression.c_str() );
expression=expression.replace(ll_pos,2," ");
}
}catch(...){}
Stack3.push_back(")");//优先级由高到底//0
Stack3.push_back(">");//1
Stack3.push_back("<");//1
Stack3.push_back("==");//1
Stack3.push_back(">=");//1
Stack3.push_back("<=");//1
Stack3.push_back("<>");//1
Stack3.push_back("!=");//1
Stack3.push_back("&&");//2
Stack3.push_back("and");//2
Stack3.push_back("||");//2
Stack3.push_back("or");//2
Stack3.push_back("(");//3
Stack3.push_back("#");//结束符优先级低//6
list<string> Expressionlist;
Expressionlist=strsplit(expression," ");
//cout<<"表达式已分解为TOKEN"<<endl;
for(list <string>::iterator it = Expressionlist.begin (); it != Expressionlist.end (); it ++)//解析后的字符串
{
// cout<<"Token="<<it->c_str()<<endl;
if(strcmp(it->c_str(),"(")==0 || strcmp(it->c_str(),")")==0 || strcmp(it->c_str(),">")==0 ||strcmp(it->c_str(),"<")==0
||strcmp(it->c_str(),">=")==0||strcmp(it->c_str(),"<=")==0||strcmp(it->c_str(),"!=")==0||strcmp(it->c_str(),"<>")==0
||strcmp(it->c_str(),"==")==0||strcmp(it->c_str(),"and")==0||strcmp(it->c_str(),"or")==0||strcmp(it->c_str(),"&&")==0
||strcmp(it->c_str(),"||")==0||strcmp(it->c_str(),"#")==0)
{
//先和运算符栈栈顶进行优先级比较
//cout<<"RUN HERE 1"<<endl;
if(Stack2.empty())
{
Stack2.push_back(it->c_str());
continue;
}
// cout<<"Stack2="<<Stack2.at(Stack2.size()-1).c_str()<<endl;//vector Stack2.end()最后指针指向空
int li_yxj_f1=6;//初始优先级//栈顶运算符
int li_yxj_f2=6;//初始优先级//当前运算符
//栈顶运算符优先级
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),")")==0)
{
li_yxj_f1=0;
}
else if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),">")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),"<=")==0 ||strcmp(Stack2.at(Stack2.size()-1).c_str(),">=")==0||strcmp(Stack2.at(Stack2.size()-1).c_str(),"<>")==0||strcmp(Stack2.at(Stack2.size()-1).c_str(),"!=")==0||strcmp(Stack2.at(Stack2.size()-1).c_str(),"==")==0)
{
li_yxj_f1=1;
//cout<<"test_li_yxj_f1="<<li_yxj_f1<<endl;
}
else if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"&&")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),"and")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),"||")==0 ||strcmp(Stack2.at(Stack2.size()-1).c_str(),"or")==0)
{
li_yxj_f1=2;
}
else if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"(")==0)
{
li_yxj_f1=3;
}
else
{
li_yxj_f1=6;
// cout<<"test_else_li_yxj_f1="<<li_yxj_f1<<endl;
}
//当前运算符优先级
if(strcmp(it->c_str(),")")==0)
{
li_yxj_f2=0;
}
else if(strcmp(it->c_str(),"<")==0 || strcmp(it->c_str(),">")==0 || strcmp(it->c_str(),"<=")==0 ||strcmp(it->c_str(),">=")==0||strcmp(it->c_str(),"<>")==0||strcmp(it->c_str(),"!=")==0||strcmp(it->c_str(),"==")==0)
{
li_yxj_f2=1;
}
else if(strcmp(it->c_str(),"&&")==0 || strcmp(it->c_str(),"and")==0 || strcmp(it->c_str(),"||")==0 ||strcmp(it->c_str(),"or")==0)
{
li_yxj_f2=2;
}
else if(strcmp(it->c_str(),"(")==0)
{
li_yxj_f2=3;
}
else
{
li_yxj_f2=6;
}
// cout<<"Stack2.at(Stack2.size()-1).c_str()="<<Stack2.at(Stack2.size()-1).c_str()<<" it->c_str()="<<it->c_str()<<endl;
// cout<<"li_yxj_f2="<<li_yxj_f2<<" li_yxj_f1="<<li_yxj_f1<<endl;
if(strcmp(it->c_str(),"(")==0)//扫描到左括号时无条件压入符号栈
{
Stack2.push_back(it->c_str());//
// cout<<"PUSH Stack2"<<it->c_str()<<endl;
continue;
}
//if(f2<f1)//数字越小优先级越大,f1找不到的话Stack2就是空栈,f1指向迭代器的尾部
if(li_yxj_f2<li_yxj_f1)//数字越小优先级越大,f1找不到的话Stack2就是空栈,f1指向迭代器的尾部
{
if(strcmp(it->c_str(),")")!=0)
{
Stack2.push_back(it->c_str());//当前优先级高,运算符压栈
// cout<<"PUSH Stack2"<<it->c_str()<<endl;
}
else
{
string ls_pop1="";
string ls_pop2="";
while(strcmp(Stack2.at(Stack2.size()-1).c_str(),"(")!=0)//弹栈直到遇到'('
{
int result=0;
int lb_result=false;
ls_pop1=Stack1.at(Stack1.size()-2);
ls_pop2=Stack1.at(Stack1.size()-1);
// cout<<"ls_pop1="<<ls_pop1.c_str()<<endl;
// cout<<"ls_pop2="<<ls_pop2.c_str()<<endl;
// cout<<"Stack2.at(Stack2.size()-1).c_str()="<<Stack2.at(Stack2.size()-1).c_str()<<endl;
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),">")==0 ||
strcmp(Stack2.at(Stack2.size()-1).c_str(),"<=")==0 ||strcmp(Stack2.at(Stack2.size()-1).c_str(),">=")==0||
strcmp(Stack2.at(Stack2.size()-1).c_str(),"<>")==0||strcmp(Stack2.at(Stack2.size()-1).c_str(),"!=")==0||
strcmp(Stack2.at(Stack2.size()-1).c_str(),"==")==0)
{
result=strcmp(ls_pop1.c_str(),ls_pop2.c_str());//返回<0的值=0的值>0的值具体和编译器有关
if(result<0)
{
result=-1;
}
else if(result>0)
{
result=1;
}
else
{
result=0;
}
//lb_result true=1;false=0;
lb_result=false;//默认flase;
// cout<<"(result)="<<result<<endl;
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<")==0 && result<0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<=")==0 && result<=0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),">")==0 && result>0)
{
lb_result=true;
// cout<<"if_lb_result="<<lb_result<<endl;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),">=")==0 && result>=0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"==")==0 && result==0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"!=")==0 && result!=0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<>")==0 && result!=0)
{
lb_result=true;
}
}
else
{
if(strcmp(it->c_str(),"&&")==0 || strcmp(it->c_str(),"and")==0)
{
result=atoi(ls_pop1.c_str()) && atoi(ls_pop2.c_str());
}
else if(strcmp(it->c_str(),"||")==0 || strcmp(it->c_str(),"or")==0)
{
result=atoi(ls_pop1.c_str()) || atoi(ls_pop2.c_str());
}
}
// cout<<"temp()="<<lb_result<<endl;
Stack2.pop_back();//弹出一个运算符
Stack1.pop_back();//弹出一个数字
Stack1.pop_back();//再弹出一个数字
char str[10] = {0};
sprintf(str, "%i", lb_result);
string ls_result="";
ls_result=str;
Stack1.push_back(ls_result);//运算结果压栈
int i=0;
i++;
if(i>10000)//限定循环数,避免由于表达式错误而死循环
{
break;
}
}//while 结束
//cout<<"弹出一个左括号运算符("<<endl;
if(Stack2.size()>0)//栈空不必再弹了
{
Stack2.pop_back();//弹出一个左括号运算符
}
}
}
else//栈顶优先级高,运算符弹栈做运算
{
string ls_pop1="";
string ls_pop2="";
ls_pop1=Stack1.at(Stack1.size()-2);
ls_pop2=Stack1.at(Stack1.size()-1);
// cout<<"POP Stack2"<<endl;
// cout<<"POP Stack2"<<Stack2.at(Stack2.size()-1).c_str()<<endl;
int result=0;
bool lb_result=false;
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),">")==0 ||
strcmp(Stack2.at(Stack2.size()-1).c_str(),"<=")==0 ||strcmp(Stack2.at(Stack2.size()-1).c_str(),">=")==0||
strcmp(Stack2.at(Stack2.size()-1).c_str(),"<>")==0||strcmp(Stack2.at(Stack2.size()-1).c_str(),"!=")==0||
strcmp(Stack2.at(Stack2.size()-1).c_str(),"==")==0)
{
result=strcmp(ls_pop1.c_str(),ls_pop2.c_str());//返回<0的值=0的值>0的值具体和编译器有关
// cout<<"int result="<<result<<endl;
if(result<0)
{
result=-1;
}
else if(result>0)
{
result=1;
}
else
{
result=0;
}
//lb_result true=1;false=0;
lb_result=false;//默认flase;
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<")==0 && result<0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<=")==0 && result<=0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),">")==0 && result>0)
{
lb_result=true;
// cout<<"if_lb_result="<<lb_result<<endl;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),">=")==0 && result>=0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"==")==0 && result==0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"!=")==0 && result!=0)
{
lb_result=true;
}
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"<>")==0 && result!=0)
{
lb_result=true;
}
}
else
{
if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"&&")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),"and")==0)
{
lb_result=atoi(ls_pop1.c_str()) && atoi(ls_pop2.c_str());
}
else if(strcmp(Stack2.at(Stack2.size()-1).c_str(),"||")==0 || strcmp(Stack2.at(Stack2.size()-1).c_str(),"or")==0)
{
lb_result=atoi(ls_pop1.c_str()) || atoi(ls_pop2.c_str());
}
}
//cout<<"lb_result="<<lb_result<<endl;
Stack2.pop_back();//弹出一个运算符
Stack1.pop_back();//弹出一个数字
Stack1.pop_back();//再弹出一个数字
//Stack2.push_back(it->c_str());//弹完上一个运算符做计算,压入当前运算符。
char str[10] = {0};
sprintf(str, "%i", lb_result);
string ls_result="";
ls_result=str;
Stack1.push_back(ls_result);//运算结果压栈
it --;//下一次循环重新比较当前运算符(注意当前运算符未变)与新栈顶运算符
//continue;
}
}
else
{
Stack1.push_back(it->c_str());//变量及数字栈
}
}
string ls_expression_result="";
for(vector <string>::iterator it = Stack1.begin (); it != Stack1.end (); it ++)
{
// cout<<it->c_str();
}
cout<<endl;
for(vector <string>::iterator it = Stack2.begin (); it != Stack2.end (); it ++)
{
// cout<<it->c_str();
}
// cout<<endl;
ls_expression_result=Stack1.at(Stack1.size()-1).c_str();
return atoi(ls_expression_result.c_str());
}
#endif // EXPRESSION_H