簡體   English   中英

前綴計算器 - 在某些情況下不起作用 - C ++

[英]Prefix Calculator — won't work for some cases — C++

我正在創建一個前綴計算器,用戶輸入前綴表達式,程序對其進行評估。 它適用於某些情況,例如“+43”輸出7應該是,但“/ -421”輸出“2”,當它應輸出“1”,而“/ + 421”輸出“6”而不是“ 3“,這樣的事情。 有任何修復建議嗎? 我稍后會添加例外,所以他們暫時被注釋掉了。

PrefixCalculator.cpp

#pragma once

#include <sstream>

using namespace std;

template<class T>
class PrefixCalculator {
public:
PrefixCalculator(void){
    numOperator = 0;
    numOperand = 0;
};
~PrefixCalculator(void){};

T eval(istringstream&);

int getNumOperator() {
    return numOperator;
};

int getNumOperand() {
    return numOperand;
};

private:
//if you feel you need private helper functions and/or helper data
int numOperator;
int numOperand;
};

template<class T>
T PrefixCalculator<T>::eval(istringstream& input) { 
 //this function needs to throw an exception if there's a problem with the expression or operators
char nextChar = input.peek();

//this while loop skips over the spaces in the expression, if there are any
while(nextChar == ' ') {
    input.get();    //move past this space
    nextChar = input.peek(); //check the next character
}

if(nextChar == '+') {
    input.get();    //moves past the +
    numOperator++;
    return eval(input) + eval(input);   //recursively calculates the first expression, and adds it to the second expression, returning the result
}

/***** more operators here ******/
if(nextChar == '-') {
    input.get();
    numOperator++;
    return eval(input) - eval(input);
}

if(nextChar == '*') {
    input.get();
    numOperator++;
    return eval(input) * eval(input);
}

if(nextChar == '/') {
    input.get();
    numOperator++;
    return eval(input) / eval(input);
} 

/******  BASE CASE HERE *******/
//it's not an operator, and it's not a space, so you must be reading an actual value (like '3' in "+ 3 6".  Use the >> operator of istringstream to pull in a T value!
input>>nextChar;
T digit = nextChar - '0';
numOperand++;
return digit;
//OR...there's bad input, in which case the reading would fail and you should throw an exception

}

driver.cpp

#include <sstream>
#include <string>
#include <iostream>
#include "PrefixCalculator.h"

using namespace std;

int main(int argc, char** argv) {
PrefixCalculator<int> calc;

string expression;
cout << "Give a prefix expression to evaluate, or q to quit." << endl;
getline(cin,expression);

while(expression[0] != 'q') {
    //try {
        int result = calc.eval(istringstream(expression));
        cout << result << endl;
    //}
    //catch { //will not compile, you have to finish this!
    //  
    //}

    cout << "Give a prefix expression to evaluate or q to quit." << endl;
    getline(cin,expression);
}

return 0;
 }

我糊塗了。 根據您提供的輸出,您的程序運行正常,但您的期望不正確。

讓我們評估表達式“/ -421”:
1.操作員檢測到'/',推入堆棧:

+----+
| /  |
+====+

操作員檢測到' - ',推入堆棧:

+---+  
| - |  
+---+  
| / |  
+===+  

檢測到的數字,因為之前的操作員需要2個參

+---+
| 4 |
+---+
| - |
+---+
| / |
+===+

檢測到的數字,兩個運營商中的第二個。
將剩余的操作數彈出堆棧,4。
從堆棧彈出操作評估參數:

result = 4 - 2 --> 1

將結果推到堆棧上。

+---+
| 2 |
+---+
| / |
+===+

檢測到的數字,除法運算符的第2個參數2。
彈出堆棧中的數字,這將成為除法運算的第一個參數。
從堆棧彈出操作並使用參數進行評估:

result = 2 / 1 --> 2

將結果推入堆棧。

+---+
| 2 |
+===+

達到結束表達式,彈出結果並打印:

2

編輯1:

您可以在分析下一個令牌之前打印堆棧(每行一個項目)來確認程序的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM