Infix expression to suffix expression

Source: Internet
Author: User

I. Basic Concepts

An infix expression is like 1*2 + (2-1). Its operators usually appear between operands. Therefore, it is called an infix expression, which is the expression written in programming.

Replace the infix expression with the extension expression 1*2 + (2-1), and then change it to 12*21-+. The extension expression does not contain parentheses, the operands of the suffix expression are arranged in the same order as those of the mid-fix expression, but the order of operators has changed.

If you want to create a calculator, you first need to input a string of expressions. This expression is generally an infix expression. We can record the expression, but how can we calculate its value? Two steps are required. The first step is to replace it with a suffix expression, and the second step is to calculate the value. Today I will learn how to convert to a suffix expression.

Ii. Problem Description
Compile a program to convert an infix expression to a suffix expression.
Input: only one line is an infix expression. The entered symbols only contain the basic symbols "0123456789 +-*/()" and do not appear in the format of 2 *-3. All digits are single digits, "/" indicates the division operation.
Output: only one line is the converted suffix expression. Numbers, operators, numbers, and operators are separated by a space (see the example ).
Example: In: 8-(3 + 2*6)/5 + 4 out: 8 3 2 6 * + 5/-4 +

Iii. Algorithm Analysis

The order of the operands of the converted suffix expression will not change, but the order of the operators will change. We use the array ans [] to save the converted result.

Traverse the infix expression. When an operand is encountered, it is directly added to the back of ANS. When an operator is encountered, it needs to be processed as follows (Stack is required here ):

1. If it is '(', it is directly pushed into the stack;

2. if it is ')', the operator pops up from the stack to the back of ANS until '(' or the stack is empty {if the infix expression is correct, '('}

3. if it is another operator, if the stack is empty, it is pushed into the stack; if the stack is not empty, and the operator priority at the top of the stack is greater than or equal to the priority of the current operator, then it will pop up and add it to the back of ANS, and continue to compare with the new stack top, until the stack is empty or the stack top priority is smaller than the current operator priority, then, the current operator is added to the stack. If the stack is not empty and the operator priority at the top of the stack is smaller than the current operator priority, the operator is directly added to the stack.

4. After traversal, if there are operators in the stack, pop up and add them to ans in sequence.

Stack_yutao.h

#include <stdio.h>#include <stdlib.h>typedef char elemtype;typedef struct{     elemtype *array;     int size;     int top;}stack;/*function:initial the stack with the sizeinput:stack *s, int sizeoutput:void*/void initStack(stack *s, int size){     s->size = size;     s->top = -1;     s->array = (elemtype *)malloc(sizeof(elemtype) * s->size);}/*function:check whether the stack is fullinput:stack *soutput: 1--the stack is full; 0--the stack is not full*/int is_stack_full(stack *s){     return (s->size - 1) <= s->top;}/*function:check whether the stack is emptyinput:stack *soutput: 1--the stack is empty; 0--the stack is not empty*/int is_stack_empty(stack *s){     return s->top < 0;}/*function:push data to the stack,if the stack is full, it will stopinput:stack *s, int dataoutput:void*/void push(stack *s, elemtype data){     if (is_stack_full(s))     {          printf("The stack is also full !\n");          exit(1);     }else     {          s->top ++;          s->array[s->top] = data;     }}/*function:pop from the stack and no data will be return ,if the stack is empty, it will stopinput:stac *soutput:void*/void pop(stack *s){     if(is_stack_empty(s))     {          printf("The stack is also empty !\n");  system("pause");          exit(1);     }else     {          s->top --;     }}/*function:get the top element of the stack for data, if the stack is empty, it will stopinput:stack *s, int &dataoutput:void*/void get_top(stack *s, elemtype *data){     if(is_stack_empty(s))     {          printf("The stack is also empty !\n");  system("pause");          exit(1);     }else     {          *data = s->array[s->top];     }}/*fucntion:destory the stackinput:stack *soutput:void*/void destory_stack(stack *s){     free(s->array);}

# Include <stdio. h> # include <stdlib. h> # include "stack_yutao.h" int compare (elemtype A, elemtype B); void infix_to_postfix (const char infix_array [], int N, char postfix_array []); void display_array (elemtype array [], int N); void infix_to_postfix (const char infix_array [], int N, char postfix_array []) {int I; int Index = 0; elemtype top_elem; elemtype stack_size = 5; stack char_stack; stack * s = & char_stack; initst Ack (S, stack_size); for (I = 0; I <n; I ++) {If (48 <= infix_array [I] & infix_array [I] <= 57) // if it is a 0-9 number, copy it directly to the Postfix, and continue to the next loop {postfix_array [index ++] = infix_array [I]; continue;} switch (infix_array [I]) {Case '(': If (! Is_stack_full (s) {push (S, infix_array [I]);} break; Case ')': get_top (S, & top_elem); If (! Is_stack_empty & top_elem! = '(') {Postfix_array [index ++] = top_elem; POP (s); get_top (S, & top_elem);} If (top_elem = '(') {Pop (s);} break; Case '+': Case '-': Case '*': Case '/': If (is_stack_empty (s )) {push (S, infix_array [I]);} else {get_top (S, & top_elem); While (compare (top_elem, infix_array [I])> = 0 & top_elem! = '(') {Postfix_array [index ++] = top_elem; POP (s); If (is_stack_empty (s) {break;} else {get_top (S, & top_elem) ;}} push (S, infix_array [I]);} break; default: printf ("there is wrong char in your infix_array! \ N "); break ;}} while (! Is_stack_empty (s) {get_top (S, & top_elem); postfix_array [index ++] = top_elem; POP (s);} postfix_array [Index] = '\ 0 ';} int compare (elemtype A, elemtype B) {if (a = '+' | A = '-') {If (B = '+' | B = '-') {return 0 ;}else {return-1 ;}} if (A = '*' | A = '/') {If (B = '+' | B = '-') {return 1 ;} if (B = '*' | B = '/') {return 0;} If (B = '(') {return-1 ;}} if (A = '(') {return 1 ;}} int main () {elemtype infix_array [] = "8-(3 + 2*6) /5 + 4 "; int n = 13; elemtype postfix_array [13]; infix_to_postfix (infix_array, N, postfix_array); display_array (infix_array, n); display_array (post_array, N ); system ("pause"); Return 0;} void display_array (elemtype array [], int N) {int I; for (I = 0; I <n; I ++) {printf ("% C", array [I]);} printf ("\ n ");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.