uva 327

15 篇文章 0 订阅

表达式求值,这道题的检测点很多, 给大家贴几个:

a + b
b - z
a+b--+c++
c+f--+--a
f-- + c-- + d-++e
++x
++y
--x
--y
x++-y
x+--y
x--+y
x++-++y
x++---y
x--+++y
x--+--y
x+++--y
x---++y

z+d+c++-++b-a
++b-++c+--e-b+f++

这道题我首先找到有自增或自减计算的变量,判断是否需要提前计算,之后将中缀表达式转换为后缀表达式,最后计算出表达式结果,代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <memory>
#include <stack>

using namespace std;
const int maxn = 30;
int num[maxn];
bool afterp[maxn];
bool appear[maxn];
const char * sadd = "++";
const char * smin = "--";
stack<int> var;
stack<char> oper;
char ch;

int main()
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    string cexp, bstr;
    while(getline(cin, cexp))
    {
        bstr = cexp;
        for(int i = 0; i < cexp.size(); i++)
        {
            if(' ' == cexp[i])
            {
                cexp.erase(i, 1);
                i--;
            }
        }

        memset(num, 0, sizeof(num));
        memset(afterp, 0, sizeof(afterp));
        memset(appear, false, sizeof(appear));
        int position = position = cexp.find(sadd);
        while(string::npos != position)
        {
            if(position - 1 >= 0 && isalpha(cexp[position - 1]) && !num[cexp[position - 1] - 'a' + 1]) //after
            {
                int index = cexp[position - 1] - 'a' + 1;
                appear[index] = true;
                afterp[index] = true;
                num[index] = index + 1;
                cexp.erase(position, 2);
                position = cexp.find(sadd, position);
            }
            else if(isalpha(cexp[position + 2]) && !num[cexp[position + 2] - 'a' + 1])//before
            {
                int index = cexp[position + 2] - 'a' + 1;
                appear[index] = true;
                num[index] = index + 1;
                cexp.erase(position, 2);
                position = cexp.find(sadd, position);
            }
            else
                position = cexp.find(sadd, position + 1);
        }//while

        position = cexp.find(smin);
        while(string::npos != position)
        {
            if(position - 1 >= 0 && isalpha(cexp[position - 1]) && !num[cexp[position - 1] - 'a' + 1]) //after
            {
                int index = cexp[position - 1] - 'a' + 1;
                appear[index] = true;
                afterp[index] = true;
                num[index] = index - 1;
                cexp.erase(position, 2);
                position = cexp.find(smin, position);
            }
            else if(isalpha(cexp[position + 2]) && !num[cexp[position + 2] - 'a' + 1])//before
            {
                int index = cexp[position + 2] - 'a' + 1;
                appear[index] = true;
                num[index] = index - 1;
                cexp.erase(position, 2);
                position = cexp.find(smin, position);
            }
            else
                position = cexp.find(smin, position + 1);
        }

        while(!var.empty())
            var.pop();
        while(!oper.empty())
            oper.pop();

        string afterexp = "";
        for(int i = 0; i < cexp.length(); i++)
        {
            if(isalpha(cexp[i]))
                afterexp += cexp[i];
            else
            {
                while(!oper.empty())
                {
                    ch = oper.top();
                    oper.pop();
                    afterexp += ch;
                }//while
                oper.push(cexp[i]);
            }
        }
        while(!oper.empty())
        {
            ch = oper.top();
            afterexp += ch;
            oper.pop();
        }

        for(int i = 0; i < afterexp.size(); i++)
        {
            if(isalpha(afterexp[i]))
            {
                int index = afterexp[i] - 'a' + 1;
                if(afterp[index])
                    var.push(index);
                else if(!appear[index])
                {
                    appear[index] = true;
                    num[index] = index;
                    var.push(index);
                }
                else
                    var.push(num[index]);
                appear[index] = true;
            }
            else
            {
                int b = var.top();
                var.pop();
                int a = var.top();
                var.pop();

                if('+' == afterexp[i])
                    var.push(a + b);
                else
                    var.push(a - b);
            }
        }

        cout<<"Expression: "<<bstr<<endl;
        cout<<"    "<<"value = "<<var.top()<<endl;
        for(int i = 1; i < maxn; i++)
        {
            if(appear[i])
            {
                cout<<"    "<<char('a' + i - 1)<<" = "<<num[i]<<endl;
            }

        }
    }//while(getline)
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值