栈——表达式求值

老师代码

//202031061018 刘知鑫
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>

using namespace std;

stack<int> num;
stack<char> op;

void eval()
{
    auto b = num.top();
    num.pop();
    auto a = num.top();
    num.pop();
    auto c = op.top();
    op.pop();
    int x;
    if (c == '+') x = a + b;
    else if (c == '-') x = a - b;
    else if (c == '*') x = a * b;
    else x = a / b;
    num.push(x);
}

int main()
{
    unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
    string str;
    cin >> str;
    for (int i = 0; i < str.size(); i ++ )
    {
        auto c = str[i];
        if (isdigit(c))
        {
            int x = 0, j = i;
            while (j < str.size() && isdigit(str[j]))
                x = x * 10 + str[j ++ ] - '0';
            i = j - 1;
            num.push(x);
        }
        else if (c == '(') op.push(c);
        else if (c == ')')
        {
            while (op.top() != '(') eval();
            op.pop();
        }
        else
        {
            while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval();
            op.push(c);
        }
    }
    while (op.size()) eval();
    cout << num.top() << endl;
    return 0;
}

我的代码

#include<stdio.h>    
#include<string.h>    
#include<math.h>    
#define MAX 10000000    
  
int fig[100];  
char sym[100];  
int temp1 = -1, temp2 = -1;  
char op[9] = {'+','-','*','/','(',')','#','^','%'};  
char checklist[9][9] =   
{  
    {'>','>','<','<','<','>','>','<','<'},  
    {'>','>','<','<','<','>','>','<','<'},  
    {'>','>','>','>','<','>','>','<','>'},  
    {'>','>','>','>','<','>','>','<','>'},  
    {'<','<','<','<','<','=',' ','<','<'},  
    {'>','>','>','>',' ','>','>','>','>'},  
    {'<','<','<','<','<',' ','=','<','<'},  
    {'>','>','>','>','<','>','>','<','>'},  
    {'>','>','>','>','<','>','>','<','>'}  
};  

int number(char* q)  
{  
    return (int)(*q-'0');  
}  
  
void push_figure(int q)  
{  
    fig[++temp1]=q;  
}  
  
int pop_figure()   
{  
    return fig[temp1--];  
}  
  
char pop_symbol()   
{  
    return sym[temp2--];  
}  
  
void push_symbol(char ch)   
{  
    sym[++temp2]=ch;  
}  
  
int operate(int x, int y, char symbol)   
{  
    switch (symbol)   
    {  
    case '+': return x+y;  
    case '-': return x-y;  
    case '*': return x*y;  
    case '/':   
            if(y) return x / y;  
            else   
            {  
            printf("Divide 0.\n");  
            return MAX;  
            }  
    case '%': return (int)fmod(x,y);  
    case '^':   
            if (y >= 0) return (int)pow(x,y);  
            else   
            {  
            printf("error.\n");  
            return MAX;  
            }     
    default: printf("error.\n");  
          
        return MAX;  
    }  
}  
  
char compare(char x,char y)   
{  
    int a, b;  
    for(int i = 0; i <= 8; i++)   
    {  
        if(op[i] == x)   
        {  
            a = i;  
            break;  
        }  
    }  
      
    for(int i = 0; i <= 8; i++)   
    {  
        if(op[i] == y)   
        {  
            b = i;  
            break;  
        }  
    }  
    return checklist[a][b];  
}  
int main()   
{  
    int n,flag = 0;    
    char expression[100], * p;  
    scanf("%d", &n);  
      
    while(n--)   
    {     
        flag = 2;  
        scanf("%s", expression);  
        strcat(expression,"#");   
          
        p = expression;  
        push_symbol('#');  
  
    k:while(*p != '#' || sym[temp2] != '#')   
    {  
        if(*p >= '0' && *p <= '9')   
        {  
            if(flag == 0)   
            {  
                push_figure(pop_figure() * 10 + number(p++));  
                flag = 0;  
            }  
              
            else  
                push_figure(number(p++));  
            flag = 0;  
        }  
          
        else   
        {  
            if(flag == 1)   
            {  
                if(*p == '+' || *p == '-' || *p == '*' || *p == '/' || *p == '%' || *p == '^')   
                {  
                    printf("error.\n");  
                    goto j;  
                }  
            }  
            
            if(*p == '(') flag = 1;  
            else flag = 2;  
  
            if (temp2 == -1)   
            {  
                printf("error.\n");  
                goto j;  
            }  
            else   
            {  
                char ch = pop_symbol(), ans;  
                ans = compare(ch, *p);  
                if(ans == ' ')  
                {  
                    printf("error.\n");  
                    goto j;  
                }  
                  
                else if(ans == '<')   
                {  
                    push_symbol(ch);  
                    push_symbol(*p++);  
                    goto k;  
                }  
                  
                else if(ans == '=')   
                {  
                    p++;  
                    goto k;  
                }  
              
                else   
                {  
                    int integer_x, integer_y;  
                    integer_y = pop_figure();  
                    integer_x = pop_figure();  
                      
                    int judge = operate(integer_x, integer_y, ch);  
                    if (judge == MAX) goto j;  
                  
                    else  
                        push_figure(judge);  
                    continue;  
                }  
                p++;  
            }  
        }  
    }  
    if(temp1 == 0 && temp2 == 0) printf("%d\n", fig[temp1]);  
      
    else   
    {  
        printf("error.\n");  
    }  
  
    j:  memset(expression,'\0',100);  
  
    temp1 = -1;  
    temp2 = -1;  
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值