Our brains are easy to understand infix expressions, but infix expressions are not computationally good in computers, and all we want to do is convert infix expressions to suffix expressions, because postfix expressions are easy to calculate. Why write a program like this? The reason is that I started to write a computer that was able to calculate the value of an input expression. At first, the program should be very simple, and then began to write, and began to write after the discovery is not so simple. Then I knew the suffix expression before I knew it would be possible to calculate the value of an expression.
Anyway
The first is the method of converting infix expressions to Postfix expressions:
1. Operand encountered: Direct output (added to suffix expression)
2, when the stack is empty, encountered the operator, directly into the stack
3. The opening parenthesis is encountered: put it into the stack
4, encountered the right parenthesis: Perform a stack operation, and will be out of the stack of elements output, until the pop-up stack is the left parenthesis, the left parenthesis does not output
5. Encounter other operators: subtraction: POPs all top-level elements with precedence greater than or equal to the operator, and then stacks the operator into the stack
6, the end of the stack of elements sequentially out of the stack, output
It seems simple, but it's really a headache to write. So I drew a lot of flowchart. I think the drawing flowchart is very good, can deepen understanding.
This time I decided to use C language.
The first step is to do some preparatory work.
First, the implementation of the data structure of the stack, has the following methods:
1. Initialize the stack and return the stack pointer
2. Pressing in an element
3. Eject an element
The stack top element that I implemented is empty, it is used only to point to the first element of the stack, and if the stack is empty it points to null.
Specific code:
//implementation of the data structure of the stack typedef struct node{char data; struct Node *next;} Node;node *ini_node () {//Initialize stack, if successful return to the top of the stack pointer, failed to return 0 node *p; p = (node *) malloc (sizeof (node)); if (p = = NULL) return 0; else {p->next = NULL; return p; }}int Push_node (node *top,char e) {//stack, top for stack top pointer, e for pressed data//return 0 for memory allocation failure//return 1 for Compression stack success, return 2 for incoming character illegal if (E < 0 && e > 255) return 2; Node *p; p = (node *) malloc (sizeof (node)); if (p = = NULL) return 0; P->next = NULL; P->data = e; P->next = top->next; Top->next = p; return 1;} Char pop_node (node *top)//Popup An element {if (top = = NULL) return 0; Node *p; Char e; p = top->next; E = p->data; Top->next = p->next; Free (p); return e;}
Second, write a function that returns the precedence of the operator
Here, the operator is only +-*/, we specify +-the priority is 1, */The priority is 2, to facilitate comparison.
Specific code:
int Prior (char a) { if (a = = ' + ' | | a = = '-') return 1; if (A = = ' * ' | | a = = '/') return 2;}
You can then write a function that converts infix expressions to postfix expressions.
Instead of using the switch statement here, I use the IF ... else statement directly, because the branch here is less suitable for using a switch statement. This function is not a lot of tests, there may be serious bugs, I will improve it later.
Code:
int shift (char *a,node *n) {int i = 0; while (1) {if (a[i] = = ())//Encounter Terminator {if (N->next = = null)//At this point, if the top of the stack is empty, return to re Turn 1; else//If the stack is not empty, all elements in the stack are out of stack {while (n->next! = NULL) { printf ("%c", Pop_node (n)); } return 1; }} else if (a[i]>=0x30 && a[i]<=0x39)//If the number is output {printf ("%c", A[i]); i++; Continue } else if (N->next = = NULL)//If it is not a number, it can only be an operator, judging if the stack has no elements at this time//if the stack has no elements, the operator is pressed into the stack {pus H_node (N,a[i]); i++; Continue The else if (a[i] = = ' (')//encounters an opening parenthesis and presses directly into the stack {push_node (n,a[i]); i++; Continue } else if (a[i] = = ') ')//encounters a closing parenthesis, ejects and outputs the elements in the stack until an opening parenthesis is encountered, and finally the opening parenthesis POPs//opening parenthesis does not output {while (n ≫next->data! = ' (') printf ("%c", Pop_node (n)); Pop_node (n); i++; Continue } else//Neither the opening parenthesis nor the closing parenthesis nor the stack is empty//then the comparison operator and the precedence of the top element of the stack {while (1) { if (N->next = = NULL | | Prior (A[I)) > Prior (n->next->data) | | n->next->data = = ' (')// If the top of the stack is empty or the priority is greater than the top element of the stack or the top element of the stack is an opening parenthesis//Then the element is pressed into the stack {push_node (n,a[i]); Break } else {printf ("%c", Pop_node (n)); Eject an element and output}} i++; Continue }} return 1;}
int shift (char *a,node *n) {int i = 0; while (1) {if (a[i] = = ())//Encounter Terminator {if (N->next = = null)//At this point, if the top of the stack is empty, return to re Turn 1; else//If the stack is not empty, all elements in the stack are out of stack {while (n->next! = NULL) { printf ("%c", Pop_node (n)); } return 1; }} else if (a[i]>=0x30 && a[i]<=0x39)//If the number is output {printf ("%c", A[i]); i++; Continue } else if (N->next = = NULL)//If it is not a number, it can only be an operator, judging if the stack has no elements at this time//if the stack has no elements, the operator is pressed into the stack {pus H_node (N,a[i]); i++; Continue The else if (a[i] = = ' (')//encounters an opening parenthesis and presses directly into the stack {push_node (n,a[i]); i++; Continue } else if (a[i] = = ') ')//encounters a closing parenthesis, ejects and outputs the elements in the stack until an opening parenthesis is encountered, and finally the opening parenthesis POPs//opening parenthesis does not output {while (n ≫next->data! = ' (') printf ("%c", Pop_node (n)); Pop_node (n); i++; Continue } else//Neither the opening parenthesis nor the closing parenthesis nor the stack is empty//then the comparison operator and the precedence of the top element of the stack {while (1) { if (N->next = = NULL | | Prior (A[I)) > Prior (n->next->data) | | n->next->data = = ' (')// If the top of the stack is empty or the priority is greater than the top element of the stack or the top element of the stack is an opening parenthesis//Then the element is pressed into the stack {push_node (n,a[i]); Break } else {printf ("%c", Pop_node (n)); Eject an element and output}} i++; Continue }} return 1;}
Converting infix expressions to suffix expressions