栈:中缀表达式转后缀表达式求值(不大于10)

#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.csdn.net/chenpeggy/article/details/7478145
http://wenku.baidu.com/link?url=H4_VPu-kadYwzYnpaAcF0ZFQSxE4MjuXNaZoSYE04nXW7hzDdzv0wvM242QuGPlQbZtzQx-0gxz6iN2ZoCTgvNfZiqcytRpMJh1-Qt9XV-i
http://blog.csdn.net/zj0910/article/details/40933423(无法处理负数,操作数可大于10)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值