不确定为什么会出现分段错误。我应该做一个RPN计算器,它在把我的Stack.h和.cpp变成模板类之前就已经工作了(在把它改成模板类之前,它只能接受int数据类型(。请帮忙,对不起所有的代码>lt;
RPNCalc.cpp文件
#include <iostream>
#include "RPNCalc.h"
#include "GenStack.h"
using namespace std;
RPNCalc::RPNCalc(){ //constructor
}
RPNCalc::~RPNCalc(){ //default
}
int RPNCalc::calculator(string a, GenStack<int>* s1){
int total;
int num1;
int num2;
string filler = ""; //meant to convert char into a string
int outcomeNum; //number that resulted from num1 and num2 operation
for(int i = 0; i < a.size(); i++){
if (a.at(i) != '+' && a.at(i) != '-' && a.at(i) != '*' && a.at(i) != '/' && a.at(i) != '%'){
filler = filler + a.at(i);
num1 = stoi(filler);
s1 -> push(num1);
filler = "";
}
else if(a.at(i) == '+'){
num1 = s1 -> pop();
num2 = s1 -> pop();
outcomeNum = num2 + num1;
s1 -> push(outcomeNum);
}
else if(a.at(i) == '-'){
num1 = s1 -> pop();
num2 = s1 -> pop();
outcomeNum = num2 - num1;
s1 -> push(outcomeNum);
}
else if(a.at(i) == '*'){
num1 = s1 ->pop();
num2 = s1 -> pop();
outcomeNum = num2 * num1;
s1 -> push(outcomeNum);
}
else if(a.at(i) == '/'){
num1 = s1 -> pop();
num2 = s1 -> pop();
outcomeNum = num2 / num1;
s1 -> push(outcomeNum);
}
else if(a.at(i) == '%'){
num1 = s1 -> pop();
num2 = s1 -> pop();
outcomeNum = num2 % num1;
s1 -> push(outcomeNum);
}
else{
cout << "The value is not a letter nor operation" << endl;
}
}
total = s1 -> peak();
return total;
}
RPNCalc.h
#include <iostream>
#include "GenStack.h"
using namespace std;
class RPNCalc{
public:
RPNCalc(); //default constructor
~RPNCalc(); //default destructor
int calculator(string a, GenStack<int>* s1); //takes a string and calculates based off RPN
calculating
};
这是我的GenStack.h
#include <iostream>
#pragma once
using namespace std;
template <typename T>
struct node{
public:
T data;
node<T>* next;
};
template <typename T>
class GenStack{
private:
node<T>* top;
int maxnum;
public:
GenStack(){
maxnum = 5;
top = NULL;
}
~GenStack(){
node<T>* current = top;
while (current){
node<T>* next = current -> next;
delete current;
current = next;
}
top = NULL;
}
void push(const T& val){
if(top == nullptr){
top = new node<T>;
top -> next = nullptr;
top -> data = val;
}
else{
node<T>* temp = new node<T>;
temp -> data = val;
temp -> next = top;
top = temp;
}
}
T pop(){
if(top == nullptr){
cout << "stack is empty" << endl;
return 0;
}
else{
node<T>* old = top;
top = top -> next;
return old -> data;
delete(old);
}
}
T peak(){
if (top){
return top -> data;
}
else
cout << "Stack is empty" << endl;
}
void print(){
node<T>* temp = top;
while(temp != nullptr){
cout << temp -> data << ", ";
temp = temp -> next;
}
}
};
这是我的主.cpp
#include <iostream>
#include "RPNCalc.h"
#include "GenStack.h"
using namespace std;
int main(int argc, char** argv)
{
GenStack<int>* s1;
RPNCalc* calc = new RPNCalc();
string example = "23+";
int total = calc -> calculator(example, s1);
cout << total << endl;
delete s1;
delete calc;
}
由于忘记在main
中构造GenStack
,导致出现错误。
GenStack<int>* s1;
应该是
GenStack<int> *s1 = new GenStack<int> ();
如果您激活了适当的警告,编译器应该告知‘s1’ may be used uninitialized
。