想了解一下为什么不同编译器会有不同结果
  • 板块学术版
  • 楼主DaiwaIssho
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/3/16 20:53
  • 上次更新2023/11/5 01:59:01
查看原帖
想了解一下为什么不同编译器会有不同结果
446288
DaiwaIssho楼主2021/3/16 20:53
#include <iostream>
#include <cstring>
using namespace std;

struct Node
{
    int num;
    Node* next;
};

class Stack
{
    Node* head;
public:
    Stack()
    {
        head = new Node;
        head->next = NULL;
    }

    int pop()
    {
        Node* p = head;
        Node* q = head->next;
        int temp = q->num;
        p->next = p->next->next;
        delete q;
        return temp;
    }

    void push(int a)
    {
        Node* p = head;
        Node* q = new Node;
        q->num = a;
        q->next = p->next;
        p->next = q;
    }

    ~Stack()
    {
        Node* p = head;
        Node* q = head->next;
        while (q)
        {
            delete p;
            p = q;
            q = q->next;
        }
        delete p;
    }
};

int main()
{
    char str[1005];
    cin >> str;
    int len = strlen(str);
    Stack a;
    int tempNum = 0;
    char temp;
    bool is = true;
    for (int i = 0; i < len - 1; i++)
    {
        temp = str[i];
        if (temp == '.')
        {
            continue;
        }
        else if (str[i] >= '0' && str[i] <= '9')
        {
            tempNum = tempNum * 10 + str[i] - '0';
            if (str[i + 1] == '.')
            {
                a.push(tempNum);
                tempNum = 0;
            }
        }
        else
        {
            switch (temp)
            {
            case '+':a.push(a.pop() + a.pop());
                break;
            case '-':a.push(-(a.pop() - a.pop()));//<<---你仔细看这里
                break;
            case '*':a.push(a.pop() * a.pop());
                break;
            default:
                break;
            }
        }
    }
    cout << a.pop() << endl;
    return 0;
}

测试数据:3.5.2.-*7.+@ 正确输出:16 为什么用vs code和dev C++的输出都是-2,而用visual studio就是16呢?我感觉不同的地方就是减法时候的两个pop函数,但是找不到原因(猜测和运行栈有关?)。希望有大佬能解答

2021/3/16 20:53
加载中...