(25分)PAT 7-20 中缀表达式转后缀表达式(20分 嵌套括号 运行时错误)

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef char ElemType;
typedef char OperatorType;
typedef int Status;
typedef struct
{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
    (*S).base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
    if(!(*S).base)  exit(OVERFLOW);
    (*S).top=(*S).base;
    (*S).stacksize=STACK_INIT_SIZE;
    return OK;
}
Status StackEmpty(SqStack S)
{
    return S.top==S.base?TRUE:FALSE;
}
int StackLength(SqStack S)
{
    return S.top-S.base;
}
Status Gettop(SqStack S,ElemType *e)
{
    if(S.top==S.base) return ERROR;
    *e=*(S.top-1); return OK;
}
OperatorType GetTop(SqStack S)
{
    ElemType e;
    Gettop(S,&e);
    return e;
}
Status Push(SqStack *S,ElemType e)
{
    if(((*S).top-(*S).base)>=(*S).stacksize)
    {
        (*S).base=(ElemType *)realloc((*S).base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(ElemType));
        if(!(*S).base) exit(OVERFLOW);
        (*S).stacksize+=STACKINCREMENT;
        (*S).top=(*S).base+(*S).stacksize;
    }
    *((*S).top++)=e;
    return OK;
}
Status Pop(SqStack *S,ElemType *e)
{
    if((*S).base==(*S).top) return ERROR;
    *e=*(--(*S).top);
    return OK;
}

void PostfixExpression(char c[])
{
    SqStack S;
    InitStack(&S);
    int i=0;  //下标计数变量
    int n=strlen(c);
    int j=0; //输出个数计数变量
    //最后因为空格的原因 我就把那些不输出的也做出相应的操作
    // ( )刚开始我在左右括号都在到达时j+1 后来发现如果是(((8))) 那么8后面还是会有空格
    //所以最后在(出现时j+=2 并且作为正负号的符号的+-也有j++
    ElemType e;  //用于接受Pop变量

    while(c[i]!='\0')
    {
        if(c[i]>='0'&&c[i]<='9')
        {

            if((i<=n-2)&&c[i+1]>='0'&&c[i+1]<='9')
            {
                printf("%d",c[i]-'0');
                i++;
                j++;
            }
            else if((i<=n-2)&&c[i+1]=='.')
            {
                printf("%d",c[i]-'0');
                i++;
                j++;
            }
            else if(j>=n-1)
            {
                printf("%d",c[i]-'0');
                  i++;
                  j++;
            }
            else
            {
                printf("%d ",c[i]-'0');
                  i++;
                  j++;
            }


        }
        else
        {

            if(c[i]=='(')
        {
            Push(&S,c[i]);
            i++;
            j+=2;
        }

        else if(c[i]=='+'||c[i]=='-')
        {
            if(i==0||(i>=1&&i<=n-1&&c[i-1]=='('))
            {
                if(c[i]=='+')
                {
                    j++;
                }
                else
                {
                    printf("%c",c[i]);
                    j++;
                }
                i++;
            }

             else if(StackEmpty(S))  //这个地方不能缺少else   不然的话 如果第一个是+ 那么输出+后 栈为空 下面的数字会被压入栈中
            {
                Push(&S,c[i]);
                i++;
            }

            else
            {
                Pop(&S,&e);
                while(e!='(')
                {

                    if(j<n-1)
                    {
                        printf("%c ",e);
                          j++;
                    }
                    else
                    {
                        printf("%c",e);
                          j++;
                    }

                    if(StackEmpty(S)) break;
                    else Pop(&S,&e);

                }

                if(e=='(') Push(&S,e);

                Push(&S,c[i]);
                i++;
            }

        }

        else if(c[i]==')')
        {

            Pop(&S,&e);

            while(e!='(')
            {
                if(j<n-1)
                {
                    printf("%c ",e);
                      j++;
                }
                else
                {
                    printf("%c",e);
                      j++;
                }

                Pop(&S,&e);

                if(StackEmpty(S)) break;

            }

            i++;

        }

        else if(c[i]=='*'||c[i]=='/')
        {
            Push(&S,c[i]);
            i++;
        }

        else if(c[i]=='.')
        {
            printf("%c",c[i]);
            j++;
            i++;
        }

        }
    }

    while(!StackEmpty(S)&&GetTop(S)!='(')
        {
            Pop(&S,&e);

            if(StackLength(S)==0) //第一点这里不能使用j<n-1 因为有的符号不会输出来 比如( )以及表示符号的+ - //第二点 这里应该是StackLength(S)而不是StackLength,虽然我也不知道后者为什么能够编译成功
            {
                printf("%c",e);
                j++;
            }
            else
            {
                printf("%c ",e);
                j++;
            }
        }

}
int main()
{
    char c[21];
    gets(c);
    int n=strlen(c);
    if(n>0&&n<=20)
    {
        PostfixExpression(c);
    }
   // printf("#");  //用来判断最后是否有空格
    return 0;
}


测试结果12+3*(7-4)+8/4
2 3 7 4 - * + 8 4 / +
Process returned 0 (0x0)   execution time : 17.773 s
Press any key to continue.

测试结果2((2+3)*4+(8+2))/5
2 3 + 4 * 8 2 + + 5 /
Process returned 0 (0x0)   execution time : 30.193 s
Press any key to continue.

测试结果31314+25.5*12
1314 25.5 12 * +
Process returned 0 (0x0)   execution time : 18.026 s
Press any key to continue.

测试结果4-2*(+3)
-2 3 *
Process returned 0 (0x0)   execution time : 7.604 s
Press any key to continue.

测试结果5:
123
123
Process returned 0 (0x0)   execution time : 3.063 s
Press any key to continue.



发布了34 篇原创文章 · 获赞 38 · 访问量 2649

猜你喜欢

转载自blog.csdn.net/qq_43779149/article/details/104203080