Niuke simply simulates a strange calculator

Strange calculator topic link
topic description
Niuniu has a calculator at home.
Niuniu accidentally broke his calculator. The multiplication (×) and division (÷) buttons all fail.
Therefore, Niuniu decided to use it to calculate expressions that only contain addition (+) and subtraction (-).
Recently, Niuniu suddenly discovered that the power (^) button of this calculator has become an exclusive OR key! Originally 5^2=25, now 5^2=7.
So he wants to know how much the calculator will return after entering an expression.
The calculator considers priority XOR (^)>plus (+), minus (-).
Input description
Input a string of expressions to ensure that the string contains only numbers, +, -, ^, and each number <= 1000000
Output description
Output answer.
Input sample

3+5^2-9

Sample output

1

s i z e size s i z e represents the length of the expression. It is guaranteed that the expression is legal and the number of symbols + 1 = the number of numbers. For all data1 ≤ size ≤ 1 0 6 1≤size≤10^61size106

First convert the string, first if the symbol is + ++ Or−- Then it is stored in the stack, and then the corresponding number character is converted to a number. If the symbol is encountered during the search process, a number will be popped, and then the next number will be calculated, and the two numbers will be XORed and stored in the stack at once Then pop the elements in the character stack and the number stack into a new container, and finally add or subtract in order to find the result.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
stack<ll>num,ans;
stack<char>fu,res;
int main()
{
    
    
    string s;
    cin>>s;
    int len = s.size();
    for(int i = 0; i < len; i++)
    {
    
    
        ll t = 0; int ok = 0;
        if(s[i] == '+' || s[i] == '-') fu.push(s[i]);
        else{
    
    
            while(s[i] >= '0' && s[i] <= '9' && i < len)
            {
    
    
                t = t*10 + s[i] - '0';
                i++;
                ok  = 1;
            }
            if(ok) num.push(t);
            if(ok && s[i] != '^') i--;
            if(s[i] == '^')
            {
    
    
                ll x = num.top(); num.pop();
                t = 0;
                while(s[i+1] >= '0' && s[i+1] <= '9' && i+1 < len)
                {
    
    
                    t = t*10 + s[i+1] - '0';
                    i++;
                }
                ll temp = x^t;
                num.push(temp);
            }
        }
    }
    while(!fu.empty())
    {
    
    
        char tx = fu.top(); fu.pop();
        res.push(tx);
    }
    while(!num.empty())
    {
    
    
        ll tx = num.top(); num.pop();
        ans.push(tx);
    }
    while(res.size()>0)
    {
    
    
        char st = res.top(); res.pop();
        ll x = ans.top(); ans.pop();  //9
        ll y = ans.top(); ans.pop();   //7
        if(st == '+') ans.push(x+y);
        if(st == '-') ans.push(x-y);
    }
    cout<<ans.top()<<'\n';
    return 0;
}

Guess you like

Origin blog.csdn.net/Edviv/article/details/111304740