0pts, 全RE, 已过样例, 求助qwq
查看原帖
0pts, 全RE, 已过样例, 求助qwq
749392
kuailedetongnian楼主2023/1/13 14:05

code

#include <iostream>
#include <math.h>

enum {
    val_et,
    op_et
};

struct Node {
    bool type;
    int val;
    char op;

    bool vis;

    Node() = default;

    Node(int _val) {
        vis = false;
        type = val_et;
        val = _val;
    }

    Node(char _op) {
        vis = false;
        type = op_et;
        op = _op;
    }

    void print() {
        if(type == val_et)
            printf("%d", val);
        else
            putchar(op);
        return;
    }
};

class stack {
public:
    Node data[1145];
    Node* head;
    Node* tail;
public:
    stack()
    {
        head = tail = data;
    }

    ~stack()
    {
        head = tail = nullptr;
    }
public:
    Node pops() {
        return *(--tail);
    }

    void pop() {
        tail--;
        return;
    }

    Node& top() {
        return *(tail - 1);
    }

    void push(Node x) {
        *(tail++) = x;
    }

    int size() {
        return (int)(tail - head);
    }

    bool empty() {
        return head == tail;
    }

    void print(int offset = 0) {
        for(Node* it = head + offset; it != tail; it++)
            it->print(), putchar(' ');
        return;
    }
};

int match(char op) {
    switch(op)
    {
        case '(': case ')':
            return 0;
        case '+': case '-':
            return 1;
        case '*': case '/':
            return 2;
        case '^':
            return 3;
    }
    return 4;
}

stack src, des;
void Transformation() {
    char c;
    while((c = getchar()) != EOF)
    {
        if(isdigit(c))
            des.push(Node((int)(c - '0')));
        else if(c == '(')
            src.push(Node(c));
        else if(c == ')') {
            while(src.top().op != '(')
                des.push(src.pops());
            src.pop();
        }
        else if(c == '^')
            src.push(c);
        else {
            while(!src.empty() && match(c) <= match(src.top().op))
                des.push(src.pops());
            src.push(Node(c));
        }
    }
    while(!src.empty())
        des.push(src.pops());
    return;
}

stack p;

void Execute() {
    des.print();
    putchar('\n');
    int size = des.size();

    int offset = 0;
    for(int i = 0; i < size; i++)
    {
        Node now = des.data[i];
        if(now.type == op_et) {
            char op = now.op;
            int b = p.pops().val;
            int a = p.pops().val;
            if(op == '+')
                p.push(a + b);
            else if(op == '-')
                p.push(a - b);
            else if(op == '*')
                p.push(a * b);
            else if(op == '/')
                p.push(a / b);
            else if(op == '^')
                p.push((int)pow(a, b));

            if(des.data[offset].type == op_et)
                offset += 1;
            else
                offset += 2;
            p.print();
            des.print(offset);
            putchar('\n');
        } else {
            p.push(now);
            offset++;
        }
    }
    return;
}

int main() {
    Transformation();
    Execute();
    return 0;
}
2023/1/13 14:05
加载中...