我正在做作业,并且作业正在制作带有前缀和堆栈的计算器,该计算器由链表实现。
所以我自己制作了堆栈,但是代码不起作用。
Visual Studio说:
right_data is nullptr.
但是没有排队的迹象,所以我找不到问题所在。
各种信息:
这是堆栈类函数:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstddef>
using namespace std;
class Node {
friend class Linkedlist;
public:
string data;
Node *prev;
Node *next;
Node();
Node(string data);
};
Node::Node(){
this->data = "";
this->prev = NULL;
this->next = NULL;
};
Node::Node(string data){
this->data = data;
};
class stack {
private:
Node *top_node;
int length;
public:
stack(){
this->top_node = new Node();
this->length = 0;
for(int x = 0; x<100; x++){
Node* temp = new Node();
temp->next = this->top_node;
this->top_node -> prev = temp;
this->top_node = temp;
}
}
~stack(){
while(this->top_node->next != NULL){
Node* temp = this->top_node->next;
delete top_node;
top_node = temp;
}
}
void push(string data){
this->top_node->data = data;
this->top_node = this->top_node->next;
this->length += 1;
}
int size(){
return this->length;
}
string top(){
return this->top_node->data;
}
bool empty(){
if(this->top_node->data.size() == 0)
return true;
else
return false;
}
void pop(){
this->top_node = this->top_node->prev;
this->length -= 1;
}
};
stack inputed, op, calclist;
int priority(const char* c) {
if (*c == '*' || *c == '/') return 2;
else if (*c == '+' || *c == '-') return 1;
else cout << "error" << endl;
}
bool is_digit(string str) {
return atoi(str.c_str()) != 0 || str.compare("0") == 0;
}
//infix to prefix
//using stack and queue to make prefix
void infix_to_prefix() {
while (!inputed.empty()) {
string t1 = inputed.top();
inputed.pop();
if (is_digit(t1)) calclist.push(t1);//digit go straightly to calclist
else {
if (op.empty()) {
op.push(t1);
}
else {
while (!op.empty()) {
if (priority(t1.c_str()) < priority(op.top().c_str())) {
calclist.push(op.top());
op.pop();
}
else {
op.push(t1);
break;
}
}
if (op.empty()) op.push(t1);
}
}
}
while (!op.empty()) {
calclist.push(op.top()); //all of last op going to calclist
op.pop();
}
}
string do_calc(string num1, string oper, string num2) {
int number1 = atoi(num1.c_str());
int number2 = atoi(num2.c_str());
int result;
if (oper[0] == '+') result = number1 + number2;
if (oper[0] == '-') result = number1 - number2;
if (oper[0] == '*') result = number1 * number2;
if (oper[0] == '/') result = number1 / number2;
char buffer[256];
sprintf(buffer, "%d", result);
string sss = string(buffer);
cout << "calc result : " << sss << endl;
return sss;
}
int result;
//using queue calculating
int calcing_list() {
stack temp;
bool is_consecutive = false, keep_going = false;
while (!calclist.empty()) {
if (is_digit(calclist.top())) {
if (is_consecutive) keep_going = true;
op.push(calclist.top());
calclist.pop();
is_consecutive = true;
}
else {
if (is_consecutive) {
is_consecutive = false;
keep_going = false;
}
op.push(calclist.top()); //operator push
calclist.pop();
}
cout << "op top is " << op.top() << endl;
if (keep_going) { //num consecutive, do calc
string operand2 = op.top(); op.pop();
string operand1 = op.top(); op.pop();
if (!is_digit(op.top())) { //operator
cout << "calc " << operand1 << " " << op.top() << " " << operand2 << endl;
operand1 = do_calc(operand1, op.top(), operand2);
op.pop(); //operator out
}
else { //serial int
stack temp;
while (is_digit(op.top())) {
temp.push(operand2);
operand2 = operand1;
operand1 = op.top(); op.pop();
}
cout << "calc " << operand1 << " " << op.top() << " " << operand2 << endl;
operand1 = do_calc(operand1, op.top(), operand2);
op.pop(); //operator out
while (!temp.empty()) {
operand2 = temp.top(); temp.pop();
cout << "calc " << operand1 << " " << op.top() << " " << operand2 << endl;
operand1 = do_calc(operand1, op.top(), operand2);
op.pop(); //operator out
}
}
op.push(operand1); //result in
}
}
while (op.size() != 1) {
string operand2 = op.top(); op.pop();
string operand1 = op.top(); op.pop();
if (!is_digit(op.top())) { //operator
cout << "calc " << operand1 << " " << op.top() << " " << operand2 << endl;
operand1 = do_calc(operand1, op.top(), operand2);
op.pop(); //operator out
}
else { //serial int
stack temp;
while (is_digit(op.top())) {
temp.push(operand2);
operand2 = operand1;
operand1 = op.top(); op.pop();
}
cout << "calc " << operand1 << " " << op.top() << " " << operand2 << endl;
operand1 = do_calc(operand1, op.top(), operand2);
op.pop(); //operator out
while (!temp.empty()) {
operand2 = temp.top(); temp.pop();
cout << "calc " << operand1 << " " << op.top() << " " << operand2 << endl;
operand1 = do_calc(operand1, op.top(), operand2);
op.pop(); //operator out
}
}
op.push(operand1); //result in
}
return atoi(op.top().c_str());
}
//make result
int main() {
ifstream fin("1.inp");
if (fin.is_open()) {
string str;
while (!fin.eof()) {//using stack make reverse
fin >> str;
inputed.push(str);
}
}
else cout << "file input error" << endl;
infix_to_prefix();
//while(!calclist.empty()){cout<<calclist.top()<<" ";calclist.pop();}
int rr = calcing_list();
ofstream fout("stack.out");
fout << rr;
return 0;
}