类堆栈内存问题(已触发错误)

时间:2015-04-22 01:07:49

标签: c++ memory-leaks stack postfix-notation

我使用堆栈类制作了一个Post-Fix(RPN)计算器。在一些帮助下,我设法摆脱了编译器错误,但现在我对内存的东西一无所知。执行程序时,在输入中间发生一条被杀死的错误消息。我也无法摆脱输入。我检查了三巨头,我认为他们没事。

主要

#include <iostream>
#include <cstdlib>
#include <string>
#include "stack.h"
#include "abstractstack.h"
#include "robotcalc.h"
#include <sstream>
using namespace std;

int main()
{
    string userInput;
    Stack<int> operandStack;


    do
    {
        int number;

        cin >> userInput;

        if (istringstream(userInput) >> number)
        {
            operandStack.push(number);
        }
        else if (isOperator)
        {
            performOperation(userInput, operandStack);
        }
    } while (userInput != "@");

}

主要计划实施: &#34;#&#34;操作员打印堆栈

$ operator清除堆栈

@表示输入结束

 #include<string>
 #include<cstdlib>
 #include<iostream>
 #include"robotcalc.h"
 #include "stack.h"

using namespace std;

bool isOperator(const string input)
{
    const int NUM_OPERATORS = 10;

    string operatorArray[] = { "+", "-", "*", "/", "%", "!", "SUM", "R", "#", "R" };

    for (int i = 0; i < NUM_OPERATORS; i++)
    {
        if (input == operatorArray[i])
            return true;
        else
            return false;
    }
}

void performOperation(const string& input, Stack<int> operandStack)
{
    int result;
    int leftOperand, rightOperand;


    if (input == "!")
    {
        result = operandStack.top() * -1;
        operandStack.pop();
        operandStack.push(result);

    }

    if (input == "+")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand + rightOperand;
        operandStack.push(result);
    }

    if (input == "-")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand - rightOperand;
        operandStack.push(result);
    }

    if (input == "*")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand * rightOperand;
        operandStack.push(result);
    }

     if (input == "/")
     {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand / rightOperand;
        operandStack.push(result);
    }

     if (input == "%")
    {
        rightOperand = operandStack.top();
        operandStack.pop();
        leftOperand = operandStack.top();
        operandStack.pop();
        result = leftOperand - rightOperand;
        operandStack.push(result);
    }

    if (input == "#")
    {
        cout << "[";
        while (operandStack.getTop() != NULL)
        {
           cout << operandStack.top() << ",";
           operandStack.pop();
        }
        cout << "]" << endl;
    }

    if (input == "SUM")
    {
        while (operandStack.getTop() != NULL)
        {
            result += operandStack.top();
            operandStack.pop();
        }
        operandStack.push(result);
    }

    if (input == "R")
    {
        operandStack.reverse();
    }

}

Stack类实现:

#include <string>
#include <iostream>
using namespace std;

template class Stack<int>;

template<class T>
const Stack<T>& Stack<T>::operator = (const Stack<T>& rhs)
{
    if (this != &rhs)
    {
        if (Top != NULL)
        {
            clear();
        }
        Top = NULL;
        Node<T>* rhsPtr = rhs.Top;
        Node<T>* copyPtr = Top = new Node<T>;
        copyPtr->Data = rhsPtr->Data;
        while (rhsPtr->next != NULL)
        {
            rhsPtr = rhsPtr->next;
            copyPtr->next = new Node<T>;
            copyPtr = copyPtr->next;
            copyPtr->Data = rhsPtr->Data;
        }
        copyPtr->next = NULL;
    }
    return(*this);
}

template<class T>
Stack<T>::Stack(const Stack<T>& rhs)
{
    Top = NULL;
    *this = rhs;
}

template<class T>
bool Stack<T>::isEmpty() const
{
     return(Top == NULL);
}

template<class T>
const T& Stack<T>::top() const throw(Oops)
{
    if (Top != NULL)
        return(Top->Data);
    else
        throw Oops("Stack is Empty!");

}

template<class T>
void Stack<T>::push(const T& x)
{
    Node<T>* newNode = new Node<T>;
    newNode->Data = x;

    if (isEmpty())
     {
      Top = newNode;
      return;
    }

    newNode->next = Top;
    Top->next = newNode;

 }

 template<class T>
 void Stack<T>::pop()
 {
    Node<T>* temp;
    if (Top != NULL)
    {
        temp = Top;
        Top = Top->next;
        delete temp;
    }
 }

template<class T>
void Stack<T>::clear()
 {
    Node<T>* temp;
    temp = Top;
    while (temp != NULL)
    {
        Top = Top->next;
        delete temp;
        temp = Top;
    }
}

template<class T>
void Stack<T>::reverse()
{
    Node<T>* current;
    Node<T>* previous;
    Node<T>* next;

    previous = NULL;
    current = Top;
    while (current != NULL)
    {
        next = current->next;
        current->next = previous;
        previous = current;
        current = next;
    }

    Top = previous;
}

Stack Class Header:

#ifndef STACK_H
#define STACK_H

#include<string>
#include<iostream>
#include<cstdlib>
#include "abstractstack.h"
using namespace std;



template<typename T>
struct Node
{
    T Data;
    Node<T>* next;
};


template<typename T> 
class Stack : public AbstractStack<T>
{
private:
    Node<T>* Top;

public:

    //Purpose: Destructor
    //Postconditions: The stack is now empty
    ~Stack() {}

    //Purpose: Default Constructor
    //Postconditions: Top is initialized to 'NULL'
    Stack(): Top(NULL) {}


    //Copy Constructor
    Stack(const Stack<T>& rhs);

    //Overloaded = Operator
    //Postconditions: *this is now equal to rhs
    const Stack<T>& operator = (const Stack<T>& rhs);


    //Purpose: Check if a stack is empty
    //Returns: 'true' if the stakc is empty, 'false' otherwise
    bool isEmpty() const;

    //Purpose: Looks at the top of the stack
    //Returns: a const reference to the element currently on top of the stack
    //Exception: if the stack is empty, THROW a 'Oops' object with an error message!!!"
    const T& top() const throw(Oops);

    //Purpose: push an element into the stack
    //Parameters: x is the value to push into the stack
    //Postconditions: x is now the element at the top of the stack
    void push(const T& x);

    //Purpose: pop the stack
    //Postconditions: the element formerly at the top of the stack has been removed
    //Popping an empty stack results in an empty stack
    void pop();

    //Purpose: clears the stack
    //Postconditions: the stack is now empty
    void clear();

    //Reverses the stack
    //Postconditions: stack is now in reverse order
    void reverse();


    //Getter Function for Top member variable
    Node<T>* getTop() const { return(Top); }
};

#include "stack.hpp"
#endif

Stack类是来自纯虚拟调用的派生类&#34; AbstractStack&#34;我没有费心去发帖。

示例输入

  

1 2 3#$

     

4 3 *! #

     

20 3 /#$#

     

62#$

     

2 3 8 * +#

     

4 48 4 2 + /#

     

SUM#$

     

1 2 3 4 5#

     

R#

     

@

示例输出

  

[3,2,1]

     

[-12]

     

[6,-12]

     

[]

     

[62]

     

[26]

     

[8,4,26]

     

[38]

     

[5,4,3,2,1]

     

[1,2,3,4,5]

任何想法或帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

作业

应该捕获异常的内容是什么?我怀疑一个未被捕获的抛出异常就是你所经历的。 尝试添加try / catch来处理它。另外,您是否尝试使用调试器运行? 在浏览代码时我注意到的另一件事是你在operator%的实现中有一个copypaste错误。 祝你好运