[YBT-1356] Calc 题解报告

· · 个人记录

原题传送门!

死亡模拟.jpg

中缀转后缀然后计算qwq

switch + case = 317行qaq

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
#define rg register
#define LL long long
template <typename qwq> inline void read(qwq & x)
{
    x = 0;
    rg int f = 1;
    rg char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}
template <typename qaq> inline void print(qaq x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
//==================================================================
char c[233333];
LL tot,x;
stack<LL> num;
stack<char> op;
//==================================================================
int main()
{
    //My bro is Sun e Phone,he says the calculation!
    scanf("%s",c + 1);
    rg int len = strlen(c + 1);
    for (rg int i = 1;i <= len;++i)
    {
        if (c[i] >= '0' && c[i] <= '9')
        {
            x = (x << 1) + (x << 3) + (c[i] ^ 48);
            if (c[i + 1] < '0' || c[i + 1] > '9')
            {
                num.push(x);
                x = 0;
            }
        }
        else
        {
            switch (c[i])
            {
                //==================================================================
                case '+':
                {
                    rg bool temp = true;
                    while(!op.empty() && op.top() != '(' && temp && num.size() > 1)
                    {
                        rg int x = num.top();num.pop();
                        rg int y = num.top();num.pop();
                        rg char tmp = op.top();
                        switch (tmp)
                        {
                            case '^': 
                                y = pow(y,x);num.push(y);
                                op.pop();
                                break;
                            case '*': 
                                y *= x;num.push(y);
                                op.pop();
                                break; 
                            case '/': 
                                y /= x;num.push(y);
                                op.pop();
                                break; 
                            case '+':
                                y += x;num.push(y);
                                op.pop();
                                break; 
                            case '-':
                                y -= x;num.push(y);
                                op.pop();
                                break; 
                            default: 
                                num.push(y),num.push(x);
                                temp = false;
                                break;
                        }
                    }
                    op.push('+');
                    break;
                }
                //==================================================================
                case '-':
                {
                    rg bool temp = true;
                    while(!op.empty() && op.top() != '(' && temp && num.size() > 1)
                    {
                        rg int x = num.top();num.pop();
                        rg int y = num.top();num.pop();
                        rg char tmp = op.top();
                        switch (tmp)
                        {
                            case '^': 
                                y = pow(y,x);num.push(y);
                                op.pop();
                                break;
                            case '*': 
                                y *= x;num.push(y);
                                op.pop();
                                break; 
                            case '/': 
                                y /= x;num.push(y);
                                op.pop();
                                break; 
                            case '+':
                                y += x;num.push(y);
                                op.pop();
                                break; 
                            case '-':
                                y -= x;num.push(y);
                                op.pop();
                                break; 
                            default: 
                                num.push(y),num.push(x);
                                temp = false;
                                break;
                        }
                    }
                    op.push('-');
                    break;
                }
                //==================================================================
                case '*':
                {
                    rg bool temp = true;
                    while(!op.empty() && op.top() != '(' && temp && num.size() > 1)
                    {
                        rg int x = num.top();num.pop();
                        rg int y = num.top();num.pop();
                        rg char tmp = op.top();
                        switch (tmp)
                        {
                            case '^': 
                                y = pow(y,x);num.push(y);
                                op.pop();
                                break;
                            case '*': 
                                y *= x;num.push(y);
                                op.pop();
                                break; 
                            case '/': 
                                y /= x;num.push(y);
                                op.pop();
                                break; 
                            default: 
                                num.push(y),num.push(x);
                                temp = false;
                                break;
                        }
                    }
                    op.push('*');
                    break;
                }
                //==================================================================
                case '/':
                {
                    rg bool temp = true;
                    while(!op.empty() && op.top() != '(' && temp && num.size() > 1)
                    {
                        rg int x = num.top();num.pop();
                        rg int y = num.top();num.pop();
                        rg char tmp = op.top();
                        switch (tmp)
                        {
                            case '^': 
                                y = pow(y,x);num.push(y);
                                op.pop();
                                break;
                            case '*': 
                                y *= x;num.push(y);
                                op.pop();
                                break; 
                            case '/': 
                                y /= x;num.push(y);
                                op.pop();
                                break; 
                            default: 
                                num.push(y),num.push(x);
                                temp = false;
                                break;
                        }
                    }
                    op.push('/');
                    break;
                }
                //==================================================================
                case '^':
                {
                    rg bool temp = true;
                    while(!op.empty() && op.top() != '(' && temp && num.size() > 1)
                    {
                        rg int x = num.top();num.pop();
                        rg int y = num.top();num.pop();
                        rg char tmp = op.top();
                        switch (tmp)
                        {
                            case '^': 
                                y = pow(y,x);num.push(y);
                                op.pop();
                                break;
                            default: 
                                num.push(y),num.push(x);
                                temp = false;
                                break;
                        }
                    }
                    op.push('^');
                    break;
                }
                //==================================================================
                case '(': 
                {
                    op.push('(');
                    break;
                } 
                //==================================================================
                default:
                {
                    rg bool temp = true;
                    while(!op.empty() && temp && num.size() > 1)
                    {
                        rg int x = num.top();num.pop();
                        rg int y = num.top();num.pop();
                        rg char tmp = op.top();
                        switch (tmp)
                        {
                            case '^': 
                                y = pow(y,x);num.push(y);
                                op.pop();
                                break;
                            case '*': 
                                y *= x;num.push(y);
                                op.pop();
                                break; 
                            case '/': 
                                y /= x;num.push(y);
                                op.pop();
                                break; 
                            case '+':
                                y += x;num.push(y);
                                op.pop();
                                break; 
                            case '-':
                                y -= x;num.push(y);
                                op.pop();
                                break; 
                            default: 
                                num.push(y),num.push(x);
                                temp = false;
                                break;
                        }
                    }
                    op.pop();
                    break;
                }
            }
        }
    }
//==================================================================
    rg bool temp = true;
    while(!op.empty() && op.top() != '(' && temp && num.size() > 1)
    {
        rg int x = num.top();num.pop();
        rg int y = num.top();num.pop();
        rg char tmp = op.top();
        switch (tmp)
        {
            case '^': 
                y = pow(y,x);num.push(y);
                op.pop();
                break;
            case '*': 
                y *= x;num.push(y);
                op.pop();
                break; 
            case '/': 
                y /= x;num.push(y);
                op.pop();
                break; 
            case '+':
                y += x;num.push(y);
                op.pop();
                break; 
            case '-':
                y -= x;num.push(y);
                op.pop();
                break; 
            default: 
                num.push(y),num.push(x);
                temp = false;
                break;
        }
    }
    print(num.top());
}