gpt4 book ai didi

c++ - while 循环在一次迭代后终止

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:34 25 4
gpt4 key购买 nike

我正在尝试用 C++ 编写一个函数来计算后缀符号方程。我的一般策略是扫描一个字符串(以正确的格式,例如“10 20 + 30 -”)。
我通过递增索引变量 i 来做到这一点。在每次递增时,我都会检查字符是数字、运算符还是两者都不是。如果是数字,我使用 getNextNum() 函数获取所有后续数字,将其转换为 float ,然后将其压入堆栈。我还增加了我捕获的数字的长度。如果字符是运算符,我获取栈顶的两个元素,进行运算,然后将结果压回栈中。

问题是,我的 while 循环似乎只执行了一次。该函数仅返回字符串中的第一个数字。我不知道出了什么问题,我将不胜感激!我在 while 循环中插入了 cout 语句,我只是递增到第一个数字之后的索引。

编辑:好的,我添加了 getNextNum() 函数。此外,我用 strLength 的 cout 更新了 evalPostfix(),并且在 while 循环的每次迭代之后更新了 i。运行给定的代码时,我得到这个:

Running…
Please enter an expression in postfix notation: 555 666+
3
555
3
Your expression evaluates to: 555

似乎 strLength 被设置得比它应该的少。为什么会这样?

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
using namespace std;

string getNextNum(string equation, int i);
float evalPostfix(string postfix);
float doOperation(float x, float y, char op);

float doOperation(float x, float y, char op)
{
switch (op) {
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
default:
return 0.0;
}
}


string getNextNum(string equation, int i)
{
string num = "";
const string DELIM = "+-*/%^ ";
while (i<equation.length()) {
// Iterate through equation until you hit a delimiter.
if (DELIM.find(equation[i]) != -1) {
break;
}
num += equation[i];
i++;
}
return num;
}

float evalPostfix(string postfix)
{
const string OPS = "+-*/%^";
const string NUMS = "0123456789";
int strLength = postfix.length();
stack<float> numStack;
int i = 0;
cout << strLength << endl;
while (i<strLength) {
if (NUMS.find(postfix[i]) != -1) {
// If a character is a digit, then you should get the
// value and push it to the stack (could be multiple characters long).
string sNextNum = getNextNum(postfix, i);
float fNextNum = atof(sNextNum.c_str());

numStack.push(fNextNum);
cout << sNextNum << endl;
i += (sNextNum.length() - 1);
}
else if (OPS.find(postfix[i] != -1)) {
// Otherwise, pop the top two elements of the stack, perform the
// operation, then push the result back to the stack.
char op = postfix[i];

float x = numStack.top();
numStack.pop();

float y = numStack.top();
numStack.pop();

float z = doOperation(x, y, op);
numStack.push(z);
}
i++;
cout << i << endl;
};

// Once the entire string has been scanned through, there should be a float
// left in the stack, simply return that.
return numStack.top();
}

int main ()
{
cout << "Please enter an expression in postfix notation: ";
string postfix;
cin >> postfix;

float eval = evalPostfix(postfix);
cout << "Your expression evaluates to: " << eval;
return 0;
}

最佳答案

你有几个问题,其中一个主要问题是打字错误,你放错了 ) 这个:

else if (OPS.find( postfix[i] != -1 ) ) {
^ ^

应该是:

else if (OPS.find( postfix[i] ) != std::string::npos) {
^ ^

因此,您正在将 i 位置的 char-1 进行比较,然后在 find bool 结果。接下来您应该使用 -1 来比较 find 的结果但是std::string::npos

正如乔纳森指出的那样:

cin >> postfix ;

只读到第一个黑色或换行符。使用 getline 将解决该问题:

if (getline(cin, postfix))

关于c++ - while 循环在一次迭代后终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17508607/

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