繁体   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