类堆栈内存问题(致命错误)
Class Stack Memory Problems (Killed error)
我使用堆栈类制作了一个修复后(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来处理它。此外,您是否尝试使用调试器运行?我在浏览代码时注意到的另一件事是,在运算符%的实现中存在复制粘贴错误。祝好运
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- 在Raspbian上编译pybind11测试用例时出现致命错误
- 致命错误:特征/密集:VSC 中没有此类文件或目录
- v8::HandleScope::CreateHandle() # 中的致命错误无法在没有 HandleScope 的情
- macOS 致命错误:sys/_types/_int8_t.h:没有此类文件或目录 #include < sys/_types/_int8_t.h>
- /usr/include/c++/7/cstdlib:75:15:致命错误:stdlib.h:没有这样的文件或目录 #i
- 代码::块C++使用 MacOS 编译 Mojave : 致命错误: sys/cdefs.h: 没有这样的文件或目录
- 致命错误:找不到'boost/uuid/uuid.hpp'文件
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- 链接:致命错误LNK1104:无法打开文件"python37_d.lib"
- 造成致命错误:boost/fusion/iterator/equal_to.hpp 没有这样的文件或目录
- 多次调用本机方法时出现致命错误
- 在 Windows 10 下使用 GTest 的 CMake - 致命错误 LNK1104:无法打开文件"gtest.lib",但存在调试"gtestd.lib"
- NMAKE:致命错误 U1077:cl.exe 和 nmake.exe 返回代码'0x2'
- 【MacOSX 10.15.1】使用 odb 生成数据库,致命错误:wchar.h:没有这样的文件或目录 #include < wchar.h>
- 初学者问题 - 致命错误LNK1561:必须定义入口点
- 类堆栈内存问题(致命错误)
- 致命错误:iostream:没有这样的文件或目录-你能帮助解决这个问题吗?