Math 后缀表达式?

Math 后缀表达式?,math,expression,Math,Expression,后缀表达式6 3 2 4+-*的值是多少 Here we get an operator '+' so , op2 = pop(); // op2=4 op1 = pop(); // op1=2 result = op1 + op2 ; // result =6 push(result); So new stack will be like { 6 } { 3 } { 6 } Stack 选项包括: Here we get an operator '+' so ,

后缀表达式6 3 2 4+-*的值是多少

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
选项包括:

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
A.介于-15和-100之间

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
B.介于-5和-15之间

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
C.介于5和-5之间的某个值

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
D.大约在5到15岁之间

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
大约在15到100之间

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
根据我的计算,我得到的答案是18,也就是E,但答案是A。
我遗漏了什么吗?

您遗漏了-运算符的操作数顺序。在中缀表示法中,其计算结果为6*3-4+2=-18。

让我们使用堆栈来计算表达式6*3-4+-4*
Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
然后,从左到右一个接一个地遍历,如果我们得到operend,那么我们需要将它推入堆栈,如果我们得到operator,那么我们需要弹出;两次,将第一个弹出的元素分配给变量op2,第二个弹出的元素分配给变量op1,然后执行遍历中得到的操作,如op1*op2,然后push;这个过程将继续到表达式的最后一个元素。因此,让我们实现表达式的stack方法

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
1现在在表达式中从左向右遍历,直到得到一个运算符,在得到操作数时,将其推入堆栈中, 在遍历到'4'时,堆栈将如下 6324+-*

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
Here again  we get an operator '-' so ,

op2 = pop(); // op2=6
op1 = pop(); // op1=3
result = op1 - op2 ; // result =-3
push(result);

So, new stack will be like 

{ - 3 }
{  6  }
Stack
2 6 3 2 4+-*

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
36324+-*

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
Here again  we get an operator '-' so ,

op2 = pop(); // op2=6
op1 = pop(); // op1=3
result = op1 - op2 ; // result =-3
push(result);

So, new stack will be like 

{ - 3 }
{  6  }
Stack
4 6 3 2 4+-*

Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack
Here again  we get an operator '*' so ,

op2 = pop(); // op2=-3
op1 = pop(); // op1=6
result = op1 - op2 ; // result =-18
push(result);

So, new stack will be like 

{ -18 }
Stack

5所以,最后的答案是,-18

这个问题似乎是离题的,因为它与编程无关。我投票以离题的方式结束这个问题,因为它是一个数学问题,而不是编程问题。我投票结束这个问题,因为它与编程无关。可能更适合你
Here we get an operator '+' so ,

 op2 = pop(); // op2=4
 op1 = pop(); // op1=2
 result = op1 + op2 ; // result =6
 push(result);

So new stack will be like 

{  6  }
{  3  }
{  6  }
Stack