栈的操作——输出超限问题

这个题本来以为挺简单的,结果错了三次。。。

##原本错误的代码

#include<stdio.h>
#include<malloc.h> 
#include<string.h>
#define maxsize 1008
#define error 1
#define ok 0
typedef int Status;
typedef int ElemType;
typedef struct
{
    ElemType *space;
    int top;
}Stack;
Status Initstack(Stack *S)
{
    S->space=(ElemType *)malloc(sizeof(ElemType)*maxsize);
    if(!S->space)
        return error;
    S->top=-1;
    return ok;
}
Status push(Stack *S,ElemType e)
{
    if(S->top+1==maxsize)
        return error;
    S->space[++S->top]=e;
    return ok;
}
Status pop(Stack *S,int *e)
{
    if(S->top==-1)
        return error;
    *e=S->space[S->top--];
    return ok;
}
Status clearstack(Stack *S)
{
    S->top=-1;
}
int main()
{
    char str[1008];
    Stack S;
    Initstack(&S);
    int e,i;
    while(gets(str)){
        int num=1;
        for(i=0;i<strlen(str);i++) {
            switch(str[i]){
                case 'P':
                    push(&S,num);
                    num++;
                    //printf("%d",S.top);
                    break;
                case 'Q':
                    if(pop(&S,&e)==error)
                    {
                        printf("error");
                        break;
                    }
                    else
                        printf("%d ",e);
                    break;
                default:
                    printf("error");
                    break;
            }
        }
        printf("\n");
        clearstack(&S);
    }
}
/*int main()
{
    char ch;
    Stack S;
    Initstack(&S);
    int num,e;
    ch=getchar();
    while(ch!=EOF)
    {
        num=1;
        while(ch!=EOF && ch!='\n')
        {
            switch(ch){
                case 'P':
                    push(&S,num);
                    num++;
                    break;
                case 'Q':
                    if(pop(&S,&e)==error)
                    {
                        printf("error");
                        break;
                    }
                    else
                    printf("%d ",e);
                    break;
            }
            ch=getchar();        
        }
        printf("\n");
        if(e!=EOF)
        {
            clearstack(&S);
            ch=getchar();
        }        
    }
}*/

 

##主函数写了两种都不对

后来在老师的帮助下,发现在输出为error时应该退出,再读取下一段序列,而我的依旧会运行继续输出。故输出超限:多输出了东西

##更正主函数代码

int main()
{
    char str[1008];
    Stack S;
    Initstack(&S);
    int num,e,i;
    while(gets(str)){
        int num=1,tag=1;
        for(i=0;i<strlen(str);i++) {
            switch(str[i]){
                case 'P':
                    push(&S,num);
                    num++;
                    //printf("%d",S.top);
                    break;
                case 'Q':
                    if(pop(&S,&e)==error)
                    {
                        printf("error");
                        tag=0;
                        break;
                    }
                    else
                        printf("%d ",e);
                    break;
                default:
                    printf("error");
                    break;
            }
            if(tag==0)
            break;
        }
        printf("\n");
        clearstack(&S);
    }
}

 

成功AC

posted @ 2018-11-06 19:31  swenw  阅读(401)  评论(0编辑  收藏  举报