类堆栈内存问题(致命错误)

Class Stack Memory Problems (Killed error)

本文关键字:致命错误 问题 堆栈 内存      更新时间:2023-10-16

我使用堆栈类制作了一个修复后(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 != "@");
}

主要项目实施:"#"操作员打印堆栈

$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();
}
}

堆栈类实现:

#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;
}

堆栈类标题:

#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类是从纯虚拟调用"AbstractStack"派生而来的类,我没有费心发布它。

样本输入

1 2 3#$

4 3*!#

20 3/#$#

62#$

2 3 8*+#

4 48 4 2+/#

金额#$

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]

如有任何想法或帮助,我们将不胜感激。

作业

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

相关文章: