2019 West Electric Test-3 expression evaluation (adapted from CFF201903-2 24 o'clock)

First of all, let me talk about the changes. In fact, only the input and output have been changed, and the rest has not changed. From the input n groups to a fixed group, and then the output aspect, the original problem is to judge whether the required value satisfies 24, judge YES or NO, and change it to a direct output value.
Directly put the original question:
Insert picture description here
insert a piece of knowledge here, I did not know when I did it, about the difference between scanf, gets, and getchar .

#include<bits/stdc++.h>

using namespace std;

int n;
char str[10];

stack<int>num;
stack<char>sign;
int main()
{
    
    
    for(int i = 0; i < 7; i++)
    cin>>str[i];
    //gets(str);
    while(!num.empty()) num.pop();
    while(!sign.empty()) sign.pop();
    int i = 0;
    while(i < strlen(str))
    {
    
    
        if(str[i] > '0' && str[i] <= '9')
            num.push(str[i] - '0');
        else
        {
    
    
            if(str[i] == '+')
                sign.push('+');
            else if(str[i] == '-')
            {
    
    
                num.push((str[i + 1] - '0') * (-1));
                sign.push('+');
                i++;
            }
            else if(str[i] == 'x')
            {
    
    
                int lhs = num.top();
                num.pop();
                num.push(lhs * (str[i + 1] - '0'));
                i++;
            }
            else if(str[i] == '/')
            {
    
    
                int lhs = num.top();
                num.pop();
                num.push(lhs / (str[i + 1] - '0'));
                i++;
            }
        }
        i++;
    }
    while(!sign.empty())
    {
    
    
        int rhs = num.top();
        num.pop();
        int lhs = num.top();
        num.pop();
        sign.pop();
        num.push(lhs + rhs);
    }
    int ans = num.top();
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/KO812605128/article/details/114462440