中缀到postfix转换,逻辑错误

时间:2017-07-30 17:18:47

标签: c

我的老师给了我一个使用堆栈将中缀表达式转换为postfix的赋值。我不太擅长编程,而且我很久没有使用过这种语言了。任何人都可以看看这个程序,我自己写这个没有任何参考。请检查逻辑是否正确以及是否存在语法错误。我问这个是因为我想用我的逻辑编写它,使用下面写的逻辑。

        #include<stdio.h>
        #include<conio.h>
        #include<stdlib.h>
        #define size 20
        int z = 0;`enter code here`
        char b[20];char postarr[20];
        struct st
        {
            char a[size];
            int top;
        }s;
        //function to store the postfix expression
        void arrpush(char c)
        {
            while(c != '\0')
            {
                postarr[z] = c;
                z++;
            }
        }
        //function to pop operator from stack based on the priority assigned to each operator
        void pop()
        {
            if(s.top == -1)
            printf("Stack Is Empty");
            else
            {   if(s.a[s.top] == '1')
                arrpush('-');
                else if(s.a[s.top] == '2')
                arrpush('+');
                else if(s.a[s.top] == '3')
                arrpush('%');
                else if(s.a[s.top] == '4')
                arrpush('/');
                else if(s.a[s.top] == '5')
                arrpush('*');
                else
                arrpush('(');
                s.top--;
            }
        }
        //function to push operators on stack
        void push(int n)
        {
            if(s.top > size-1)
            printf("Stack Overflow!");
            else if(s.top >= -1)
            {
                s.top++;
                if(n == 0)
                {
                    s.a[s.top] == 0;
                }
                if(n == 1 && s.a[--s.top]<=n)
                s.a[s.top] == 1;
                else
                {
                    while(s.a[s.top]>n)
                    {
                    pop();
                    s.top--;
                    }
                    s.a[s.top]== 1;
                }
                if(n == 2 && s.a[--s.top]<=n)
                s.a[s.top] == 2;
                else
                {
                    while(s.a[s.top]>n)
                    {
                    pop();
                    s.top--;
                    }
                    s.a[s.top]== 2;
                }
                if((n == 3 )&& (s.a[--s.top]<=n))
                s.a[s.top] == 3;
                else
                {
                    while(s.a[s.top]>n)
                    {
                    pop();
                    s.top--;
                    }
                    s.a[s.top]== 3;
                }
                if(n == 4 && s.a[--s.top]<=n)
                s.a[s.top] == 4;
                else
                {
                    while(s.a[s.top]>n)
                    {
                    pop();
                    s.top--;
                    }
                    s.a[s.top]== 4;
                }

                if(n == 6)
                {
                    while(s.a[s.top]!=0)
                {   
                    pop();
                    s.top--;
                }
                }
                else if(n == 5)
                s.a[s.top] == 5;
            }
        }
        int main()
        {
            int i,j,k;
            s.top = -1;
            printf("Enter the infix expression : ");
            scanf("%s",b);
            while(b[i]!= '\0')
            {
                if(b[i] == '(')
                push(0);
                else if(b[i] == '-')
                push(1);
                else if(b[i] == '+')
                push(2);
                else if(b[i] == '%')
                push(3);
                else if(b[i] == '/')
                push(4);
                else if(b[i] == '*')
                push(5);
                else if(b[i] == ')')
                push(6);
                else
                arrpush(b[i]);
            }
//This does not display the postfix expression after conversion
            printf("\nThe postfix expression is :\n");
            for(i=0;;i++)
            {
                printf("%c",postarr[i]);
            }
        }
       // sorry for bad code I'm just starting all over again :\

0 个答案:

没有答案
相关问题