About infix expression transforming into postfix expression (Expression Data Structure Solution)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/hnujunjie/article/details/92233578

From left to right through each of the numbers and symbols infix expressions, if the digital outputs, i.e., become part of the postfix expression; sign if it is determined that the priority of the symbol stack, the priority is a right parenthesis or does not higher than the top of the stack symbol (priority subtraction, multiplication and division) the top of the stack and the stack are sequentially output and the current symbol into the stack, up to the final output until the postfix expression.

For example:
"9+ (3-1). 3 + 10 × 2 ÷"
after the postfix expression is turned
"931--3 * + 102 / +"
1. Initialize empty stack out of the stack used for the symbol ;
2. The first is the number "9", according to the rules, the digital outputs, followed by "+", into the stack;
3. The third symbol is a left bracket "(", not a number, into the stack;
4. section four symbol number "3", the output
5. fifth symbol is "-", not a number, into the stack, when the stack from the bottom to the top of the stack "+ (-";
6. sixth symbol is the numeral " 1 "output, and the output expression in order to" 931 "
7 is the seventh in line with a closing parenthesis") ", before the need to match the left parenthesis" ( ", followed by the top of the stack until the stack output left parenthesis "(", above which only "-", the output at this time can be expressed as "931--";. (pop-up but not output parentheses)
8. eighth symbol of "×", because the top of the stack symbol "+", according to the rules, "×" a higher priority than "+", so "×" directly into the stack;
9. ninth symbol number "3", output, and the expression is "93 1--3 ";
10. The tenth symbols "+", then the top element "×" higher priority than it, according to the rules, the stack until the output element requires a ratio of "+", "lower" priority is stopped, this when the stack is not less than the priority of the element "+", so all of the stack, i.e., expression is "931--3 * +", and the "+" push;
11. tenth a symbol number 10, the output
12. the twelfth symbol "÷", "+" symbol is higher than the priority of the stack, push;
13. Finally a number "2", the output
14. the symbols for all traversed, the stack the final postfix notation symbols can be sequentially output of
[Note] brackets pop but does not output
the conversion result "931--3 * + 102 / +"
Here are my thoughts written in C on March 2019 ccf answer the second question the use of the stack of code:
#include <stdio .h>
#include <ctype.h>
#include <string.h>
int main ()
{
char Scan [10] [20 is] {= '\ 0'};
char identifier [10] [20 is] = { '\ 0 '};
char OUT [10] [20 is] = {' \ 0 '};
int calstack [10] [20 is] = {0};
int n-, P = 0, tmp = 0, pre = 0;
int I = 0, J = 0, Top = 0;
Scanf ( "% D", & n-);
fflush (stdin);
for (I = 0; I <n-; I ++)
{
the gets (Scan [I]);
fflush (stdin );
}
// turn postfix infix expression
for (I = 0; I <n-; I ++)
{
P = 0;
for (J = 0; J <. 7; J ++)
{
IF (isdigit (Scan [I ] [J])! = 0)
{
out[i][p]=scan[i][j];
p++;
}
else if(scan[i][j]’/’||scan[i][j]'X')
{
Top = strlen (identifier [I]) -. 1;
the while (Top> = -. 1)
{
/ *
in C language array subscript bounds, the compiler does not check it
* /
IF ( identifier [i] [top]‘x’||identifier[i][top]’/’)
{
out[i][p]=identifier[i][top];
identifier[i][top]=’\0’;
p++;
top–;
}
else
{
top++;
identifier[i][top]=scan[i][j];
break;
}
}
}
else if(scan[i][j]’+’||scan[i][j]’-’)
{
top = strlen(identifier[i])-1;
while(top>=-1)
{
if(identifier[i][top]‘x’||identifier[i][top]’/’||identifier[i][top]’+’||identifier[i][top]’-’)
{
out[i][p]=identifier[i][top];
identifier[i][top]=’\0’;
p++;
top–;
}
else
{
top++;
identifier[i][top]=scan[i][j];
break;
}
}
}
}
top = strlen(identifier[i])-1;
while(top>=-1)
{
out[i][p]=identifier[i][top];
identifier[i][top]=’\0’;
p++;
top–;
}
}
//计算后缀表达式的值
for(i=0;i<n;i++)
{
p=0;
pre = 0;
for(pre=0;pre<strlen(out[i]);pre++)
{
if(isdigit(out[i][pre])!=0)
{
calstack[i][p]=out[i][pre]-‘0’;
p++;
}
else if(out[i][pre]‘x’||out[i][pre]’/’||out[i][pre]’+’||out[i][pre]’-’)
{
if(out[i][pre]‘x’)
{
tmp=calstack[i][p-2]*calstack[i][p-1];
calstack[i][p-2]=tmp;
p–;
}
else if(out[i][pre]
’-’)
{
tmp=calstack[i][p-2]-calstack[i][p-1];
calstack[i][p-2]=tmp;
p–;
}
else if(out[i][pre]’+’)
{
tmp=calstack[i][p-2]+calstack[i][p-1];
calstack[i][p-2]=tmp;
p–;
}
else if(out[i][pre]
’/’)
{
tmp=calstack[i][p-2]/calstack[i][p-1];
calstack[i][p-2]=tmp;
p–;
}
}
}
for(i=0;i<n;i++)
{
if(calstack[i][0]==24)
{
printf(“yes”);
}
else
{
printf(“no”);
}
}
}
return 0;
}

Guess you like

Origin blog.csdn.net/hnujunjie/article/details/92233578