SDUTOJ 2484 arithmetic expression conversion (stack)

Source: Internet
Author: User

SDUTOJ 2484 arithmetic expression conversion (stack)

Arithmetic expression Conversion Time Limit: 1000 MS Memory limit: 65536 K After learning the data structure, James suddenly remembered the problem of converting an unsolved arithmetic expression into a suffix. Today he wants to solve it. With the data structure, James quickly solved this problem, but he suddenly thought about how to find the prefix and mid-fix expressions of arithmetic expressions? James is confused. You can help him solve the problem. Enter an arithmetic expression with the \ '# \' character as the ending sign. (Data must have no space and only one set of inputs) the prefix suffix obtained by the expression conversion is output. Output in three rows. The order is prefix-type suffix. Sample Input
a*b+(c-d/e)*f#
Sample output
+*ab*-c/defa*b+c-d/e*fab*cde/-f*+
The prompt is as long as you understand the prefix, infix, and suffix of the arithmetic expression, you can write it out, but it is very troublesome to simulate the stack with arrays... source

Sample program



#include
 
  #include#include
  
   #include
   
    #include
    
     using namespace std;int bijiao(char ch){    if(ch=='+'||ch=='-')    {        return 1;    }    if(ch=='*'||ch=='/')    {        return 2;    }    if(ch=='(')    {        return 3;    }}void qian(char str[]){    char a[10100];    char b[10010];    char q[11000];    int pp = 0;    int k = 0;    int h = 0;    int len = strlen(str);    for(int i=len-2; i>=0; i--)    {        a[k++] = str[i];    }    a[k] = '#';    //a[k] = '\0';    for(int i=0; a[i]!='#'; i++)    {        if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i] <='Z'))        {            b[h++] = a[i];        }        else        {            if(a[i] == ')')            {                q[++pp] = a[i];            }            else if(a[i] == '(')            {                for(; q[pp]!=')'; pp--)                {                    b[h++] = q[pp];                }                pp--;            }            else            {                if(bijiao(a[i]) < bijiao(q[pp]))                {                    if(q[pp] == ')')                    {                        q[++pp] = a[i];                    }                    else                    {                        b[h++] = q[pp];                        q[pp] = a[i];                    }                }                else                {                    q[++pp] = a[i];                }            }        }    }    while(pp!=0)    {        b[h++] = q[pp];        pp--;    }    for(int i=h-1; i>=0; i--)    {        printf("%c",b[i]);    }    printf("\n");}void zhong(char str[]){    for(int i=0; str[i]!='#'; i++)    {        if(str[i]!='(' && str[i]!=')')        {            printf("%c",str[i]);        }    }    printf("\n");}void hou(char str[]){    char qq[100100];    int p = 0;    for(int i=0; str[i]!='#'; i++)    {        if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))        {            printf("%c",str[i]);        }        else        {            if(p == 0)            {                qq[++p] = str[i];            }            else if(str[i] == '(')            {                qq[++p] = str[i];            }            else if(str[i] == ')')            {                for(; qq[p]!='('; p--)                {                    printf("%c",qq[p]);                }                p--;            }            else            {                if(bijiao(str[i]) <= bijiao(qq[p]))                {                    if(qq[p] == '(')                    {                        qq[++p] = str[i];                    }                    else                    {                        printf("%c",qq[p]);                        qq[p] = str[i];                    }                }                else                {                    qq[++p] = str[i];                }            }        }    }    while(p!=0)    {        printf("%c",qq[p]);        p--;    }    printf("\n");}int main(){    char str[10010];    while(scanf("%s",str)!=EOF)    {        //printf("str = %s\n",str);        qian(str);        zhong(str);        hou(str);    }    return 0;}
    
   
  
 

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.