# include
Int F (char symbol)
{
The switch (symbol)
{
Case: '+'
Case '-' : return 2;
Case: '*'
Case '/' : return 4;
Case '^' :
Case '$' : return 5;
Case '(' : return 0;
Case '#' : return - 1;
Default: return 8;
}
}
Int G (char symbol)
{
The switch (symbol)
{
Case: '+'
Case '-' : return 1;
Case: '*'
Case '/' : return 3;
Case '^' :
Case '$' : return 6;
Case '(' : return 9;
Case ') : return 0;
Default: return 7;
}
}
Void infix_postfix (char infix [], char postfix [])
{
Int top;
int j;
int i;
Char s [30];
Char symbol;
Top=1;
S [+ + top]='#';
j=0;
for(i=0; i
Symbol=infix [I];
While (F (s (top)) & gt; G (symbol))
{
Postfix [top -] [j]=s;
J++;
}
If (F (s (top))!=G (symbol))
S [+ + top]=symbol;
The else
Top -;
}
While (s [top]!='#')
{
Postfix [top -] [j++]=s;
}
Postfix [j]='\ 0';
}
Int main ()
{
Char infix [20].
Char postfix [20].
Printf (" Enter a vaild infix expression \ n ");
The scanf (" % s ", infix);
Infix_postfix (infix, postfix);
Printf (" the postfix expression is \ n ");
Printf (" % s \ n ", postfix);
}
CodePudding user response:
This code involving infix and postfix expression, need the knowledge of the data structure of binary tree, if you have not learned data structure, can pass the program first, then to understand,CodePudding user response:
Can you explain in detail the function of the program stackCodePudding user response:
Postfix expression 5 + 2, for example, successively into the stack 2 5, met a +, the 2 5 popup stack calculation, again into the stack,CodePudding user response:
Can specific to explain the steps the programCodePudding user response:
for(i=0; iSymbol=infix [I];
While (F (s (top)) & gt; G (symbol))
{
Postfix [top -] [j]=s;
J++;
}
If (F (s (top))!=G (symbol))
S [+ + top]=symbol;
The else
Top -;
}
While (s [top]!='#')
{
Postfix [top -] [j++]=s;
}
Postfix [j]='\ 0';
This part is not very understanding
CodePudding user response:
Expressions to calculate the code online (or book) is very much, can be different writing, principles are the same, you can get a written good comments, understand the look back after this, also just about,