AlexCao:) 2021-09-30 17:27 采纳率: 60%
浏览 39
已结题

用栈实现多项式计算时提示SegmentationFault

#ifndef IMPROVEDSTACK_H
#define IMPROVEDSTACK_H

template<typename T>
class Stack
{
    public:
        Stack();
        Stack(const Stack&);
        ~Stack();
        bool empty() const;
        T peek() const;
        void push(T);
        T pop();
        int getSize() const;
    
    private:
        T* elements;
        int size;
        int capacity;
        void ensureCapacity();
};

template<typename T>
Stack<T>::Stack():size(0),capacity(16)
{
    elements = new T[capacity];
}

template<typename T>
Stack<T>::Stack(const Stack& stack)
{
    elements = new T[stack.capacity];
    capacity = stack.capacity;
    size = stack.size;
    for (int i = 0; i < size;i++)
    {
        elements[i] = stack.elements[i];
    }

}

template<typename T>
Stack<T>::~Stack()
{
    delete[] elements;
}

template<typename T>
bool Stack<T>::empty() const
{
    return size == 0;
}

template<typename T>
T Stack<T>::peek() const
{
    return elements[size - 1];
}

template<typename T>
void Stack<T>::push(T value)
{
    ensureCapacity();
    elements[size++] = value;
}

template<typename T>
T Stack<T>::pop()
{
    return elements[--size];
}

template<typename T>
int Stack<T>:: getSize() const
{
    return size;
}

template<typename T>
void Stack<T>:: ensureCapacity()
{
    if(size>=capacity)
    {
        T *old = elements;
        elements = new T[2 * size];
        capacity = 2 * size;
        for (int i = 0; i < size;i++)
        {
            elements[i] = old[i];
        }
        delete[] old;
    }
}

#endif



#include<iostream>
#include<vector>
#include<string>
#include<cctype>
#include "ImprovedStack.h"

using namespace std;
void processOperator(Stack<char>& operatorStack,Stack<int>& operandStack)
{
    int a = operandStack.pop();
    int b = operandStack.pop();
    char c = operatorStack.pop();
    if(c=='+')
        operandStack.push(b + a) ;
    if(c=='-')
        operandStack.push(b - a);
    if(c=='*')
       operandStack.push(b * a);
    if(c=='/')
        operandStack.push(b / a);
}
int main()
{
    string expression;
    cout << "Enter an expression, motherfucker";
    getline(cin, expression);
    Stack<char> operatorStack;
    Stack<int> operandStack;
    for (unsigned i = 0; i < expression.length();i++)
    {
        if(isdigit(expression[i]))
        {
            operandStack.push(expression[i]-'0');
        }
        else
        {
            if(expression[i]=='+'||expression[i]=='-')
            {
                while(!operandStack.empty()&&(operatorStack.peek()!='(')&&(operatorStack.peek()!=')'))
                {
                    processOperator(operatorStack,operandStack);
                }
                operatorStack.push(expression[i]);
            }
            if(expression[i]=='*'||expression[i]=='/')
            {
                while(!operandStack.empty()&&(!operatorStack.empty())&&(operatorStack.peek()=='/'||operatorStack.peek()=='*'))
                {
                    processOperator(operatorStack,operandStack);
                }
                operatorStack.push(expression[i]);

            }
            if(expression[i]=='(')
            {
                operatorStack.push(expression[i]);
            }
            if(expression[i]==')')
            {
                while(operatorStack.peek()!='(')
                {
                    processOperator(operatorStack, operandStack);
                }
                operatorStack.pop();
            }
        }
        
    }
    while(!operatorStack.empty())
        {
             processOperator(operatorStack, operandStack);
        }
        cout << expression << "=" << operandStack.pop() << endl;
        
    
}


img

  • 写回答

3条回答 默认 最新

  • zeng2859 2021-09-30 19:42
    关注

    加 -std=c++11

    评论

报告相同问题?

问题事件

  • 系统已结题 10月8日
  • 创建了问题 9月30日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境