#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;
#include <stdio.h>
bool IsOperator(char ch)
{
char ops[] = "+-*/";
for (int i = 0; i < sizeof(ops) / sizeof(char); i++)
{
if (ch == ops[i])
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
// 比较两个操作符的优先级
int Precedence(char op1, char op2)
{
if (op1 == '(')
{
return -1;
}
if (op1 == '+' || op1 == '-')
{
if (op2 == '*' || op2 == '/')
{
return -1;
}
else
{
return 0;
}
}
if (op1 == '*' || op1 == '/')
{
if (op2 == '+' || op2 == '-')
{
return 1;
}
else
{
return 0;
}
}
}
//////////////////////////////////////////////////////////////////////////
// 中缀表达式转换成后缀表达式
void inFix2PostFix(char* inFix, char* postFix)
{
int j = 0, len;
char c;
stack<char> st;
len = strlen(inFix);
for (int i = 0; i < len; i++)
{
c = inFix[i];
if (c == '(')
st.push(c);
else if (c == ')')
{
while (st.top() != '(')
{
postFix[j++] = st.top();
st.pop();
}
st.pop();
}
else
{
if (!IsOperator(c))
// st.push(c);
postFix[j++] = c;
else
{
while (!st.empty() && Precedence(st.top(), c) >= 0)
{
postFix[j++] = st.top();
st.pop();
}
st.push(c);
}
}
}
while (!st.empty())
{
postFix[j++] = st.top();
st.pop();
}
postFix[j] = '\0';
}
//////////////////////////////////////////////////////////////////////////
// 后缀表达式求值程序1
int evalRPN(char *tokens)
{
int result = 0;
int i;
stack<int> opd; //存储操作数
int size = strlen(tokens);
for(i=0; i<size; i++)
{
if(tokens[i]=='*')
{
int rOpd = opd.top(); //右操作数
opd.pop();
int lOpd = opd.top(); //左操作数
opd.pop();
result = lOpd*rOpd;
opd.push(result);
}
else if(tokens[i]=='/')
{
int rOpd = opd.top();
opd.pop();
int lOpd = opd.top();
opd.pop();
result = lOpd/rOpd;
opd.push(result);
}
else if(tokens[i]=='+')
{
int rOpd = opd.top();
opd.pop();
int lOpd = opd.top();
opd.pop();
result = lOpd+rOpd;
opd.push(result);
}
else if(tokens[i]=='-')
{
int rOpd = opd.top();
opd.pop();
int lOpd = opd.top();
opd.pop();
result = lOpd-rOpd;
opd.push(result);
}
else
{
opd.push(tokens[i]-'0');
}
}
return opd.top();
}
//////////////////////////////////////////////////////////////////////////
// 后缀表达式求值程序2
int postFixEval(char* postFix)
{
stack<int> st;
int len = strlen(postFix);
char c;
for (int i = 0; i < len; i++)
{
c = postFix[i];
if (IsOperator(c) == false)
{
st.push(c - '0');
}
else
{
char op1, op2;
int val;
op1 = st.top();
st.pop();
op2 = st.top();
st.pop();
switch (c)
{
case '+':
val = op1 + op2;
break;
case '-':
val = op2 - op1;
break;
case '*':
val = op1 * op2;
break;
case '/':
val = op2 / op1;
break;
}
st.push(val);
}
}
return st.top();
}
int main()
{
char inFix[100];
char postFix[100];
double val;
while (1)
{
printf("enter an expression:");
gets(inFix);
if (strlen(inFix) == 0)
continue;
printf("\n%s = ", inFix);
inFix2PostFix(inFix, postFix);
printf("%s = ", postFix);
//val = postFixEval(postFix);
val = evalRPN(postFix);
printf("%.3f\n", val);
}
return 0;
}
http://www.cnblogs.com/dolphin0520/p/3708602.html(利用vector[string],处理大于10的数及负数)
http://www.cnblogs.com/mygmh/archive/2012/10/06/2713362.html(转为后缀表达式时候的判断分析。)
http://blog.youkuaiyun.com/chenpeggy/article/details/7478145
http://wenku.baidu.com/link?url=H4_VPu-kadYwzYnpaAcF0ZFQSxE4MjuXNaZoSYE04nXW7hzDdzv0wvM242QuGPlQbZtzQx-0gxz6iN2ZoCTgvNfZiqcytRpMJh1-Qt9XV-i
http://blog.youkuaiyun.com/zj0910/article/details/40933423(无法处理负数,操作数可大于10)