RPN 问题(运算顺序)
我正在尝试制作一个带有运算顺序的简单计算器。 我在网上查到了RPN(逆波兰表示法)的算法。
编辑:
让我们举个例子: 2 * 5 - 3 + 4
好的,我按照你们说的做了,现在检查一下:
Proc 是数字和操作的字符串数组。 Proc 将是 {2, *, 5, -, 3, +, 4}
这是代码:
int tempt = 0;
Stack <Double> stc = new Stack <Double>();
while (tempt < proc.length)
{
try
{
Double num = Double.parseDouble(proc[tempt]);
stc.push(num);
tempt++;
}
catch (Exception e)
{
char [] stcs = proc[tempt].toCharArray();
switch (stcs[0])
{
case '+':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 + a2);
tempt++;
break;
}
case '-':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 - a2);
tempt++;
break;
}
case 'x':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 * a2);
tempt++;
break;
}
case '÷':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 / a2);
tempt++;
break;
}
}
}
仍然不起作用
我怎样才能让它也起作用? 请帮助我!
I'm trying to make a simple calculator with order of operations.
I had read in the Internet and found the algorithm of RPN (Reverse Polish notation).
EDIT:
Lets take an example:
2 * 5 - 3 + 4
Ok, I did as you both said check it out now:
Proc is string array of both numbers and operations.
Proc will be {2, *, 5, -, 3, +, 4}
This is the code:
int tempt = 0;
Stack <Double> stc = new Stack <Double>();
while (tempt < proc.length)
{
try
{
Double num = Double.parseDouble(proc[tempt]);
stc.push(num);
tempt++;
}
catch (Exception e)
{
char [] stcs = proc[tempt].toCharArray();
switch (stcs[0])
{
case '+':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 + a2);
tempt++;
break;
}
case '-':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 - a2);
tempt++;
break;
}
case 'x':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 * a2);
tempt++;
break;
}
case '÷':
{
double a2 = stc.pop();
double a1 = stc.pop();
stc.push(a1 / a2);
tempt++;
break;
}
}
}
STILL DOESNT WORK
How can I make it work as well?
HELP ME PLS!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的算法错了。 RPN 中的
2 * 5 - 3 + 4
转换为:2 5 * 3 - 4 +
。我不知道为什么你要在两个单独的列表中独立处理数字和符号:用逆波兰表示法:虽然话
虽这么说,你的程序几乎是正确的,除了对于输入你应该采用一系列符号(值和运算符)。现在您从左到右阅读符号。如果是数字,则将其压入堆栈。 If 运算符:弹出前两个值并压入结果。就是这样!
更新:示例
输入:
2 5 * 3 - 4 +
堆栈:
[]
迭代 I:(从输入读取
2
)输入:
5 * 3 - 4 +
堆栈:
[2]
迭代 II:(从输入读取
5
)输入:
* 3 - 4 +< /代码>
堆栈:<代码>[2, 5]
迭代 III:(从输入读取
*
)输入:
3 - 4 +
堆栈:
[2 * 5]
= =[10]
迭代 IV:(从输入读取
3
)输入:
- 4 +
堆栈:
[10, 3]< /code>
迭代 V:(读
-
来自输入)输入:
4 +
堆栈:
[10 - 3]
==[7]
迭代 VI : (从输入中读取
4
)输入:
+
堆栈:
[7, 4]
迭代 VII: (读取
+
> 来自输入)输入:``
堆栈:
[7 + 4]
==[11]
结果:
11
(没有进一步的输入,堆栈上唯一的元素就是结果)更新 2:来吧!
您正在编写一个程序来解释 RPN 但您正在使用 中缀表示法!尝试使用此输入:
您的代码中还有其他几个缺陷(重复、异常驱动的流程控制、无错误处理),但本质上使用此输入它应该可以工作。
You've got the algorithm wrong.
2 * 5 - 3 + 4
in RPN translates to:2 5 * 3 - 4 +
. I don't know why you are treating numbers and symbols independently in two separate lists: in Reverse Polish notation:while
That being said your program is almost correct except that for input you should take a series of symbols (both values and operators). Now you read symbols from left to right. If it is a number, push it onto the stack. If operator: pop top two values and push the result. That's it!
UPDATE: Example
Input:
2 5 * 3 - 4 +
Stack:
[]
Iteration I: (reading
2
from input)Input:
5 * 3 - 4 +
Stack:
[2]
Iteration II: (reading
5
from input)Input:
* 3 - 4 +
Stack:
[2, 5]
Iteration III: (reading
*
from input)Input:
3 - 4 +
Stack:
[2 * 5]
==[10]
Iteration IV: (reading
3
from input)Input:
- 4 +
Stack:
[10, 3]
Iteration V: (reading
-
from input)Input:
4 +
Stack:
[10 - 3]
==[7]
Iteration VI: (reading
4
from input)Input:
+
Stack:
[7, 4]
Iteration VII: (reading
+
from input)Input: ``
Stack:
[7 + 4]
==[11]
Result:
11
(no further input, one and only element on the stack is the result)UPDATE 2: C'mon!
You are writing a program to interpret RPN but you are feeding it with infix notation! Try with this input:
There are several other flaws in your code (duplication, exception driven flow control, no error handling), but essentially with this input it should work.