visual studio сказала, что right_data - это nullptr, но я не могу его найти - PullRequest
0 голосов
/ 05 декабря 2018

Я делаю домашнее задание, а домашнее задание - создание калькулятора с префиксом и стеком, который реализуется связным списком.

Итак, я сделал стек самостоятельно, но код не работает.

VisualСтудия сказала:

right_data is nullptr.

Но никаких признаков линии, поэтому я не могу найти, в чем проблема.

Различная информация:

  • стек, составленный стеком
  • новый тег && delete используется только Constructor, Destructor
  • все входные данные меньше 100
  • , поэтому сделали 100node и используйте его
  • стек используется длясоздание инфикса для префикса
  • последнее программирование было инфиксом для префикса, сделанного стеком STL

Это функции класса стека:

#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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...