码迷,mamicode.com
首页 > 其他好文 > 详细

ex6.栈的应用:文字输入模拟(有错误未解决)

时间:2016-03-08 00:07:13      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include<stdio.h>
#include<stdlib.h>
typedef char SElemType;
typedef struct{
    SElemType *base,*top;
    int stacksize;
}SqStack;
int InitStack(SqStack &S)
{
    S.base = (SElemType*)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
    if(!S.base) exit(-2);
    S.base = S.top;
    S.stacksize = STACK_INIT_SIZE;
    return 1;
}//
int GetTop(SqStack S,SElemType &e)
{
    if(S.base == S.top) return -1;
    e = *(S.top-1);
    return 1;
}
int Push(SqStack &S,SElemType e)
{
    if(S.top - S.base >= S.stacksize)
    {
        S.base = (SElemType*)realloc(S.base,sizeof(SElemType) * (STACKINCREMENT + S.stacksize));
        if(!S.base) exit(-2);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return 1;
}
int Pop(SqStack &S,SElemType &e)
{
    if(S.top <= S.base) return -1;
    e = *(--S.top);
    return 1;
}
int ClearStack(SqStack &S)
{
    S.top = S.base ; 
    return 1;
}
void ShowStack(SqStack S)
{
    SElemType e;
    while(S.top > S.base)
    {
        Pop(S,e);
        printf("%c",e);
    }
}
int DestoryStack(SqStack &S)
{
    free(S.base);
    S.base = NULL;
    S.top = NULL;
    S.stacksize = 0;
}
void LineEdit()
{
    printf("欢迎进入文字输入练习系统:\n");
    SqStack S;
    InitStack(S);
    char i = getchar();
    Push(S,i);
    
    // char temp;
    // char ch = getchar();
    // while(ch != EOF)
    // {
        // while(ch != EOF && ch != ‘\n‘)
        // {
            // switch(ch){
                // case ‘#‘:Pop(S,temp);break;
                // case ‘@‘:ClearStack(S);break;
                // default : Push(S,ch);break;
            // }
            // ch = getchar();
        // }
        // ClearStack(S);
        // if(ch != EOF) ch = getchar();
    // } 
    //DestoryStack(S);
}

int main()
{
    LineEdit();
    return 1;
}
    

 

#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#include<stdio.h>#include<stdlib.h>typedef char SElemType;typedef struct{SElemType *base,*top;int stacksize;}SqStack;int InitStack(SqStack &S){S.base = (SElemType*)malloc(sizeof(SElemType) * STACK_INIT_SIZE);if(!S.base) exit(-2);S.base = S.top;S.stacksize = STACK_INIT_SIZE;return 1;}//int GetTop(SqStack S,SElemType &e){if(S.base == S.top) return -1;e = *(S.top-1);return 1;}int Push(SqStack &S,SElemType e){if(S.top - S.base >= S.stacksize){S.base = (SElemType*)realloc(S.base,sizeof(SElemType) * (STACKINCREMENT + S.stacksize));if(!S.base) exit(-2);S.top = S.base + S.stacksize;S.stacksize += STACKINCREMENT;}*S.top++ = e;return 1;}int Pop(SqStack &S,SElemType &e){if(S.top <= S.base) return -1;e = *(--S.top);return 1;}int ClearStack(SqStack &S){S.top = S.base ; return 1;}void ShowStack(SqStack S){SElemType e;while(S.top > S.base){Pop(S,e);printf("%c",e);}}int DestoryStack(SqStack &S){free(S.base);S.base = NULL;S.top = NULL;S.stacksize = 0;}void LineEdit(){printf("欢迎进入文字输入练习系统:\n");SqStack S;InitStack(S);char i = getchar();Push(S,i);    // char temp;// char ch = getchar();// while(ch != EOF)// {// while(ch != EOF && ch != ‘\n‘)// {// switch(ch){// case ‘#‘:Pop(S,temp);break;// case ‘@‘:ClearStack(S);break;// default : Push(S,ch);break;// }// ch = getchar();// }// ClearStack(S);// if(ch != EOF) ch = getchar();// } //DestoryStack(S);}
int main(){LineEdit();return 1;}

ex6.栈的应用:文字输入模拟(有错误未解决)

标签:

原文地址:http://www.cnblogs.com/minemine/p/5252123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!