C++:将后缀转换为求值的程序

发布于 2024-12-11 21:48:37 字数 623 浏览 0 评论 0原文

如何将数组中的字符转换为整数?

忽略第 5-100 行,这只是我的堆栈。 http://ideone.com/KQytD

向下滚动输出 #2 工作正常,但输出 #3 无法正常工作。当我将值推回堆栈时,当我弹出它时,由于 ASCII,它有 +'43',我似乎无法将其转换为常规整数值,因此我可以轻松地执行这些操作。

第 116 行将输入放入 char postfix 中。注意:输入必须采用后缀表示法,第 117 行在运行完函数后将单个整数值放入 Final 中。

ConvertPostfixToEvaluation 的工作原理如下:我滚动后缀的每个索引,直到读入“=”,然后输出总计/总和。第一个 if 语句将操作数 (0-9) 压入堆栈。第二个 if 语句如果读入一个运算符,则它会尝试执行第 134-158 行中的操作。在 if 语句之后,我将索引值加 1,以便它可以扫描整个数组。

问题出在我尝试对 3 个以上操作数进行加、减、乘或除的开关中。所以我相信第三个仍然有价值(+43,因为 ASCII)。

我的输出(在程序的底部)显示了尴尬之处。

切入正题。第二次将 char 转换为 int 时出现问题。

How can I convert the char in the array into an integer?

Ignore lines 5-100 it is just my stack.
http://ideone.com/KQytD

Scroll down output #2 worked properly but output #3 did not. Some how when I pushed the value back into the stack and when I popped it it had the +'43' because of the ASCII and I cannot seem to get it into a regular integer value so I can do these operations easily.

line 116 puts input into char postfix. NOTE: input must be in postfix notation line 117 puts the single integer value into final after it has run through the function.

convertPostfixToEvaluation works as such: I scroll through each index of postfix until I read in '=' then I output the total/sum. The first if statement pushed the operands (0-9) into a stack. The second if statement if it reads in an operator then it attempts to do the operation as such in lines 134-158. After the if statements I increase the index value by 1 so it can scan the entire array.

The issue lies within the switch where I try adding,subtracting,multiply, or dividing more than 3 operands. so the 3rd one i believe is still has the value (+43 because of the ASCII).

My outputs(on the bottom of my program) show what the awkwardness is.

The cut to the chase issue. Issue converting char to int the second time around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2024-12-18 21:48:37

这段代码很可能有很多错误


查找函数isdigit。这应该消除巨大的 if 语句。

您可能想要使用字符串查找而不是其他复杂的 if 语句:

const std::string my_operators = "+-/*";

if (my_operators.find(postfix[i]) != std::string::npos)
{
    // Enter here if the character is a valid symbol.
}

如果您逐个字符“解析”,则必须构建您的数字:

int number = 0;

// After detecting the character is a number:
number = number * 10 + (postfix[i] - '0');

表达式“postfix[i] - '0 '" 将返回数字字符和零字符之间的距离。 C 和 C++ 语言保证以下关系:

'0' < '1' < '2' < '3' < '4' < '5' < '6' < '7' < '8' < '9'

这些语言还声明这些数字是连续的。

建议:使用 std::string 而不是字符数组。 std::string 包含一些有用的函数,用于搜索、跳过字符和获取子字符串。

There are many things very likely wrong with this code.


Look up the function isdigit. This should eliminate the huge if statement.

You may want to use a string lookup instead of the other complex if statement:

const std::string my_operators = "+-/*";

if (my_operators.find(postfix[i]) != std::string::npos)
{
    // Enter here if the character is a valid symbol.
}

If you "parse" character by character, you will have to build your number:

int number = 0;

// After detecting the character is a number:
number = number * 10 + (postfix[i] - '0');

The expression "postfix[i] - '0'" will return the distance between the number character and the character for zero. The C and C++ languages guarantee the following relationship:

'0' < '1' < '2' < '3' < '4' < '5' < '6' < '7' < '8' < '9'

The languages also state that those numbers are contiguous.

Suggestion: use std::string instead of an array of characters. The std::string contains some helpful functions for searching, skipping characters, and obtaining a substring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文