//表达式求值 #include<iostream> #include<stack> #include<vector> #include<stdio.h> #include<string> using namespace std; bool priority(char a,char b) { int m,n; switch(a) //栈外优先级 { case '(': return true; case '+': case '-': m=0;break; case '*': case '/': m=2; } switch(b) //栈内优先级 { case '#': n=-1;break; case '+': case '-': n=1;break; case '*': case '/': n=3;break; } return m>n; } int main() { freopen("e://test.txt","r",stdin); vector<char> V;//存后缀表达式 string str; int n; while(scanf("%d",&n)!=EOF) { while(n--) { cin>>str;//输入中缀表达式 stack<char> L;//存操作符,利用栈得到符号的优先级,比把符号存入V中 L.push('#');// 让栈低为‘#’ int len=str.size(); for(int i=0;i<len;i++) //扫描中缀表达式 { // if(str[i]=='-')//考虑第一个就是负数的情况,这边没法实现 // { // V.push_back('-'); // continue; // } if(str[i]>='0'&&str[i]<='9') //是数据则直接插入存放后缀表达是的容器V中 V.push_back(str[i]); else { if(str[i]!='(') //如果是‘)’直接插入V V.push_back('#'); bool t=priority(str[i],L.top());//调用优先级函数 if(t)//如果真表示,栈外优先级高则插入V L.push(str[i]); else { if(str[i]==')')//如果是‘)’把栈顶元素插入V,直到遇到‘(’ { while(L.top()!='(') { V.push_back(L.top()); L.pop(); } L.pop();//把左括号出栈 } else//否则,把栈顶元素插入V,然后栈外操作符入栈 { p: V.push_back(L.top()); L.pop(); t=priority(str[i],L.top()); if(t) L.push(str[i]); else goto p; } } } } while(!L.empty())//把已下的操作符插入V { V.push_back(L.top()); L.pop(); } vector<char>::iterator it=V.begin(); // for(;it!=V.end();it++) // cout<<*it; // cout<<endl; // stack<float> M; stack<int> M; //用于计算的栈。 int x,y; it=V.begin(); for(;it!=V.end()-1;)//以下是,计算的实现,思路是扫描后缀表达式V,是数字则推入M { //如果是操作符,则两次把栈顶元素出栈计算,最后栈顶所存的数据就是要求的结果 ///这边有很大的改进空间 int a=0,t=0; if(*it!='+'&&*it!='-'&&*it!='*'&&*it!='/') { do { a=a*10+(*it)-48; ++it; }while(*it!='#'&&*it!='+'&&*it!='-'&&*it!='*'&&*it!='/'); if(a==0) t=1; } if(*it!='+'&&*it!='-'&&*it!='*'&&*it!='/') ++it; if(a!=0||t==1) M.push(a); int flag=0; if(*it=='+'||*it=='-'||*it=='*'||*it=='/') { flag=1; x=M.top(); M.pop(); y=M.top(); M.pop(); switch(*it) { case '+': M.push(y+x);break; case '-': M.push(y-x);break; case '*': M.push(y*x);break; case '/': M.push(y/x);break; } } if(flag==1) { ++it; if(*it=='#') { ++it; if(it==V.end()) break; } } } cout<<M.top()<<endl;//输出结果 M.pop(); V.clear(); } cin>>str; } return 0; } //本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/huasonl88/archive/2009/07/30/4395460.aspx #include<iostream> #include<stack> #include<vector> #include<stdio.h> #include<string> using namespace std; bool priority(char a,char b) { int m,n; switch(a) //栈外优先级 { case '(': return true; case '+': case '-': m=0;break; case '*': case '/': m=2; } switch(b) //栈内优先级 { case '(': n=-1;break; case '#': n=-1;break; case '+': case '-': n=1;break; case '*': case '/': n=3;break; } return m>n; } int main() { // freopen("e://test.txt","r",stdin); vector<char> V;//存后缀表达式 stack<char> L;//存操作符,利用栈得到符号的优先级,比把符号存入V中 stack<int> M; //用于计算的栈。 int x,y; string str; int n; while(scanf("%d",&n)!=EOF) { while(n--) { cin>>str;//输入中缀表达式 L.push('#');// 让栈低为‘#’ int len=str.size(); for(int i=0;i<len;i++) //扫描中缀表达式 { // if(str[i]=='-')//考虑第一个就是负数的情况,这边没法实现 // { // V.push_back('-'); // continue; // } if(str[i]>='0'&&str[i]<='9') //是数据则直接插入存放后缀表达是的容器V中 V.push_back(str[i]); else { if(str[i]!='(') //如果是‘)’直接插入V V.push_back('#'); bool t=priority(str[i],L.top());//调用优先级函数 if(t)//如果真表示,栈外优先级高则插入V L.push(str[i]); else { if(str[i]==')')//如果是‘)’把栈顶元素插入V,直到遇到‘(’ { while(L.top()!='(') { V.push_back(L.top()); L.pop(); } L.pop();//把左括号出栈 } else//否则,把栈顶元素插入V,然后栈外操作符入栈 { V.push_back(L.top()); L.pop(); t=priority(str[i],L.top()); if(t) L.push(str[i]); else { V.push_back(L.top()); L.pop(); L.push(str[i]); } } } } } while(!L.empty())//把已下的操作符插入V { V.push_back(L.top()); L.pop(); } vector<char>::iterator it=V.begin(); // for(;it!=V.end();it++) // cout<<*it; // cout<<endl; // stack<float> M; it=V.begin(); for(;it!=V.end()-1;)//以下是,计算的实现,思路是扫描后缀表达式V,是数字则推入M { //如果是操作符,则两次把栈顶元素出栈计算,最后栈顶所存的数据就是要求的结果 ///这边有很大的改进空间 int a=0,t=0; if(*it!='+'&&*it!='-'&&*it!='*'&&*it!='/') { do { a=a*10+(*it)-48; ++it; }while(*it!='#'&&*it!='+'&&*it!='-'&&*it!='*'&&*it!='/'); if(a==0) t=1; } if(*it!='+'&&*it!='-'&&*it!='*'&&*it!='/') ++it; if(a!=0||t==1) M.push(a); int flag=0; if(*it=='+'||*it=='-'||*it=='*'||*it=='/') { flag=1; x=M.top(); M.pop(); y=M.top(); M.pop(); switch(*it) { case '+': M.push(y+x);break; case '-': M.push(y-x);break; case '*': M.push(y*x);break; case '/': M.push(y/x);break; } } if(flag==1) { ++it; if(*it=='#') { ++it; if(it==V.end()) break; } } } cout<<M.top()<<endl;//输出结果 M.pop(); V.clear(); } cin>>str; } return 0; } #include<iostream> #include<stack> #include<vector> #include<stdio.h> #include<string> using namespace std; bool priority(char a,char b) { int m,n; switch(a) //栈外优先级 { case '(': return true; case '+': case '-': m=0;break; case '*': case '/': m=2; } switch(b) //栈内优先级 { case '(': n=-1;break; case '#': n=-1;break; case '+': case '-': n=1;break; case '*': case '/': n=3;break; } return m>n; } int main() { freopen("e://test.txt","r",stdin); vector<char> V;//存后缀表达式 stack<char> L;//存操作符,利用栈得到符号的优先级,比把符号存入V中 stack<int> M; //用于计算的栈。 int x,y; string str; int n; while(scanf("%d",&n)!=EOF) { while(n--) { cin>>str;//输入中缀表达式 L.push('#');// 让栈低为‘#’ int len=str.size(); for(int i=0;i<len;i++) //扫描中缀表达式 { // if(str[i]=='-')//考虑第一个就是负数的情况,这边没法实现 // { // V.push_back('-'); // continue; // } if(str[i]>='0'&&str[i]<='9') //是数据则直接插入存放后缀表达是的容器V中 V.push_back(str[i]); else { if(str[i]!='(') //如果是‘)’直接插入V V.push_back('#'); bool t=priority(str[i],L.top());//调用优先级函数 if(t)//如果真表示,栈外优先级高则插入V L.push(str[i]); else { if(str[i]==')')//如果是‘)’把栈顶元素插入V,直到遇到‘(’ { while(L.top()!='(') { V.push_back(L.top()); L.pop(); } L.pop();//把左括号出栈 } else//否则,把栈顶元素插入V,然后栈外操作符入栈 { V.push_back(L.top()); L.pop(); t=priority(str[i],L.top()); if(t) L.push(str[i]); else { V.push_back(L.top()); L.pop(); L.push(str[i]); } } } } } while(!L.empty())//把已下的操作符插入V { V.push_back(L.top()); L.pop(); } vector<char>::iterator it=V.begin(); // for(;it!=V.end();it++) // cout<<*it; // cout<<endl; // stack<float> M; it=V.begin(); for(;it!=V.end()-1;)//以下是,计算的实现,思路是扫描后缀表达式V,是数字则推入M { //如果是操作符,则两次把栈顶元素出栈计算,最后栈顶所存的数据就是要求的结果 ///这边有很大的改进空间 int a=0,t=0; if(*it!='+'&&*it!='-'&&*it!='*'&&*it!='/'&&*it!='#') { do { a=a*10+(*it)-48; ++it; }while(*it!='#'&&*it!='+'&&*it!='-'&&*it!='*'&&*it!='/'); if(a==0) t=1; } if(*it!='+'&&*it!='-'&&*it!='*'&&*it!='/') { ++it; if(it==V.end()) { M.push(a); break; } } if(a!=0||t==1) M.push(a); int flag=0; if(*it=='+'||*it=='-'||*it=='*'||*it=='/') { flag=1; x=M.top(); M.pop(); y=M.top(); M.pop(); switch(*it) { case '+': M.push(y+x);break; case '-': M.push(y-x);break; case '*': M.push(y*x);break; case '/': M.push(y/x);break; } } if(flag==1) { ++it; if(*it=='#') { ++it; if(it==V.end()) break; } } } cout<<M.top()<<endl;//输出结果 M.pop(); V.clear(); } } return 0; }