gpt4 book ai didi

c++ - 前缀计算器——在某些情况下不起作用——C++

转载 作者:行者123 更新时间:2023-11-28 02:49:19 25 4
gpt4 key购买 nike

我正致力于创建一个前缀计算器,用户可以在其中输入前缀表达式,然后程序对其进行计算。它适用于某些情况,例如“+43”应该输出 7,但是“/-421”应该输出“1”时输出“2”,而“/+421”输出“6”而不是“3”,诸如此类。有什么解决这个问题的建议吗?我稍后会添加异常(exception)情况,因此暂时将它们注释掉。

前缀计算器.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

}

驱动.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的第二个参数。
从堆栈中弹出数字,这成为除法运算的第一个参数。
从堆栈中弹出操作并使用参数进行评估:

result = 2 / 1 --> 2

将结果压入堆栈。

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

到达表达式结尾,弹出结果并打印:

2

编辑 1:

在分析下一个标记之前,您可以通过打印堆栈(每行一项)来确认程序的操作。

关于c++ - 前缀计算器——在某些情况下不起作用——C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23414681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com