用栈队列的方式模拟计算机读取和结束

来源:互联网 发布:网络与新媒体 好就业吗 编辑:程序博客网 时间:2024/06/06 02:38
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct sNode{
    char *data;
    int top;
    int bottom;
    int MAXSIZE;
};
typedef struct sNode *Stack;


Stack createStack(int MAXSIZE){
    Stack s=(Stack)malloc(sizeof(struct sNode));
    s->data=(char *)malloc(MAXSIZE*sizeof(char));
    s->top=-1;
    s->bottom=-1;
    s->MAXSIZE=MAXSIZE;
    return s;
}


bool isFull(Stack s){
    return (s->MAXSIZE-1==s->top);
}


bool isEmpty(Stack s){
    return (s->top==-1);
}


bool push(Stack s,char x){
    if(isFull(s)){
        printf("OVERFLOW!\n");
        return false;
    }else{
    return (s->data[++(s->top)]=x);//可以使用指针的首地址的方式进行*(s->data+s->top)=x;
    }
}


char pop(Stack s){
    if(isEmpty(s)){
        printf("UNDERFLOW!\n");
        return 520;
    }else{
    return  (s->data[(s->top)--]);//返回当前top对应的数值
    }
}


char cc(Stack s){
    if(isEmpty(s)){
        printf("UNDERFLOW!\n");
        return 520;
    }else{
    return  (s->data[++(s->bottom)]);//返回当前top对应的数值
    }
}


void verify(Stack s,int max){
    if(max%2!=0){//当数字为奇数时不满足成对出现的情况直接判断错误,并打印error


            printf("error!!!!!\n");
        return ;
    }
    char c;
    printf("Please input ;");
    while(!isFull(s)){//当表指向最后一位时代表表满了
        scanf(" %c",&c);//防止将/r读入到%c中影响结果
        push(s,c);//将当前的字符存入栈中
    }
    int  cont=0,pos=max/2;
    char a,b;
    while(pos--){
//        printf("%c\n",cc(s));
//         printf("   %c\n",pop(s));
//    {//判断当最后一位和第一位相同,最后第二位和第二位相同,以此类推
//            cont++;//计数器加一
//        }
        a=cc(s);//用a接收否者后面多次判断的话会影响结果
        b=pop(s);
       if(a=='['&&b==']'){
            cont++;
        }
        if(a=='('&&b==')'){
            cont++;
        }
        if(a=='{'&&b=='}'){
            cont++;
        }
    }
    if(cont==(max/2)){
        printf("Complete!!!!!\n");
    }else{
    printf("error!!!!!\n");
    }
}




int main(){
    int max;
    printf("Please input your MAXSIZE?");
    scanf("%d",&max);
    Stack s;
    s=createStack(max);
    verify(s,max);
    return 0;
}
0 0
原创粉丝点击