Conversion expression from infix to suffix

Source: Internet
Author: User

Conversion expression from infix to suffix (15 points)
Grade: 15/discount: 0.8
Problem description:
An infix expression is a mathematical expression that we usually write. A suffix expression is also called an inverse polish expression. When compiling a program to check the syntax of the expressions in the program we write, it is often possible to do so through a reverse polish expression. The program we want to design and implement is to convert the arithmetic expression represented by infix into a suffix, for example,
Replace the infix expression
(A 1 (B * C 10 d) * E)/(f 10g)
Convert to suffix
ABC * d Ten E *-FG ten/
Note: to simplify programming, assuming that the variable name is a positive real number and the operator is only +,-, *, And/, You can process the delimiter and assume that the input arithmetic expression is correct.
Requirement: Use the stack data structure. The input infix expression ends #.
Input:
Integer n. There are n infix expressions below
N expressions composed of positive numbers and operators
Output
N suffix expressions. The operands and operators are separated by spaces.

 

#include <stdio.h>#include <stdlib.h>struct shu{    int front;    int rear;}OPND;struct others {    char * base;    char * top;    int size;}OPTR;char bijiao(char x, char y) {    if (x == '+') {        if (y == '+' || y == '-' || y == ')' || y == '#')            return('>');        else            return('<');    }    if (x == '-') {        if (y == '+' || y == '-' || y == ')' || y == '#')            return('>');        else            return('<');    }    if (x == '*') {        if (y == '(')            return('<');        else            return('>');    }    if (x == '/') {        if (y == '(')            return('<');        else            return('>');    }    if (x == '(') {        if (y == ')')            return('=');        if (y == '+' || y == '-' || y == '*' || y == '/' || y == '%' || y == '(')            return('<');    }    if (x == ')') {        if (y == '+' || y == '-' || y == '*' || y == '/' || y == '%' || y == ')' || y == '#')            return('>');    }    if (x == '#') {        if (y == '+' || y == '-' || y == '*' || y == '/' || y == '%' || y == '(')            return('<');        if (y == '#')            return('=');    }}void main() {    int n, i, j, k, s[50], flag;    char sopnd[50][50], str[50],t;    scanf("%d", &n);    getchar();    for (i = 0;i < n;i++) {        flag = 0;        OPND.front = 0;        OPND.rear = 0;        OPTR.size = 50;        OPTR.base = (char *)malloc(OPTR.size * sizeof(char));        OPTR.top = OPTR.base;        *OPTR.top++ = '#';        for (j = 0;j < 50;j++)            s[j] = 0;        gets(str);        for (j = 0;str[j] != '#' || *(OPTR.top - 1) != '#';j++) {            if (str[j] >= '0' && str[j] <= '9') {                for (k = 0;(str[j] <= '9' && str[j] >= '0') || str[j] == '.';) {                    sopnd[OPND.rear][k] = str[j];                    j++;                    k++;                }                sopnd[OPND.rear][k] = '\0';                OPND.rear++;                j--;            }              else {              t= bijiao(*(OPTR.top - 1), str[j]); {                if(t=='<') {                    *OPTR.top++ = str[j];                                    }                if(t=='='){                    OPTR.top--;                                    }                if(t=='>') {                    for (;OPND.front < OPND.rear;OPND.front++) {                        if (flag == 1)                            printf(" %s", sopnd[OPND.front]);                        else                            printf("%s", sopnd[OPND.front]);                        flag = 1;                    }                    printf(" %c", *--OPTR.top);                    j--;                                    }                }            }        }        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.