Стек обратный калькулятор poi sh - PullRequest
0 голосов
/ 06 апреля 2020

По сути, я должен был сделать калькулятор нотации обратного полиса sh внутри стека. Я следую учебному пособию и основываю свой код на нескольких алгоритмах, которые я нашел на net. Однако, когда я запускаю программу, программа не печатает расчеты, и я понятия не имею, что это такое.

#include <iostream>
#include <stack>
#include <sstream> 
#include <string>
using namespace std;

bool check_operator(string op);
bool check_operand(string op);
double operation(const string& input, stack<double>& calc);


int main()
{
    stack<double> calc;
    string input, choice;

    cout << "Welcome to this Post-Fix Notation calculator. Please select your choice"
    << endl << "1. Perform an operation" << endl << "2. Quit" << endl;

    cin >> choice;

    while (choice != "2") {
        cout << "Introduce your operation" << endl;
        cin >> input;
    double value;
    if (istringstream(input) >> value) {

        calc.push(value);

    }
    else if (check_operator(input)){ 

        operation(input, calc);

    }

        //cout << input << endl;

        cout << "Would you like to continue?" << endl << "1. Yes" << endl << "2. No" << endl;
        cin  >> choice;

    }


}


bool check_operator(string op) {
    if (op == "+" || op == "-" || op == "*" || op == "/")
        return true; 
    else 
    return false;//not an operator
}

bool check_operand(string op) {
    if (op >= "0" && op <= "9")
        return true;
    else
        return false;//not an operand
}

double operation(const string& input, stack<double>& calc) {
    double val1, val2, result;
    val1 = calc.top();
    calc.pop();

    val2 = calc.top();
    calc.pop();

    if (input == "+")
    {
        result = val2 + val1;
    }
    else if (input == "-") {
        result = val2 - val1;
    }
    else if (input == "*") {
        result = val2 * val1;
    }
    else if (input == "/" && val1 == 0) {
        cout << "Invalid input";
    }
    else if (input == "/" && val1 != 0) {
        result = val2 / val1;
    }
    else {
        return 0;
    }

    cout << result << endl;
    calc.push(result);
}

Можете ли вы помочь мне понять, что с ней не так?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...