如何将数字入栈改为字符串入栈?

如何将数字入栈改为字符串入栈?


#include
#define TRUE 1
#define FALSE 0
#define Stack_Size 20

typedef int SElemType;
typedef struct{
    SElemType elem[Stack_Size];
    int top;
}SeqStack;

void InitStack(SeqStack *S)//初始化顺序栈 
{
    S->top=-1;
}

int IsEmpty(SeqStack *S)//判断栈是否为空 
{
    if(S->top==-1) printf("栈为空!");
    else printf("栈非空!"); 
}

int IsFull(SeqStack *S)//判断栈是否为满 
{
    if(S->top==Stack_Size-1) printf("栈已满!");
    else printf("栈未满!");
}

int Push(SeqStack *S,SElemType x)//入栈 
{
    if(S->top==Stack_Size-1)
    {
        printf("栈已满!");
        return(FALSE);
    }
    S->top++;
    S->elem[S->top]=x;
    return(TRUE);
}

int Pop(SeqStack *S,int x)//出栈 
{
    if(S->top==-1)
    {
        printf("栈为空!");
        return(FALSE);
    }
    else
    {
        x=S->elem[S->top];
        S->top--;
        return(TRUE);
    }
}

int GetTop(SeqStack S)
{
    if(S.top==-1)
        return(FALSE);
    int x = S.elem[S.top];
    return x;
}

int PrintStack(SeqStack S)
{
    int i;
    if(S.top==-1)
    {
        printf("栈为空");
        return(FALSE);
    }
    for( i =0;i<=S.top;i++)
    {
        printf("          |%d|\n",S.elem[S.top-i]);
    }
    return(TRUE);
}

int main()
{
    SeqStack S;
    InitStack(&S);
    int chose;
    int push,pop;
    printf("\n---------------------------------------------\n");
    printf("请选择功能:\n1:入栈   2:出栈  3:判空 4:判满  5:读取栈顶元素 6:遍历 \n");
    scanf("%d",&chose);
    while(chose==0 || chose ==1 ||chose==2 ||chose==3 ||chose==4 ||chose==5 ||chose==6 ||chose==7 )
    {
        switch(chose)
        {
            case 0:
                scanf("%d",&chose);continue;
            case 1:
                printf("请输入入栈数:");
                scanf("%d",&push);
                Push(&S,push);
                printf("入栈后为:\n");
                PrintStack(S);
                chose = 0;
                break;
            case 2:
                printf("将栈顶元素 %d 出栈\n",GetTop(S));
                Pop(&S,pop);
                printf("出栈后为:\n");
                PrintStack(S);
                chose = 0;
                break;
            case 3:
                IsEmpty(&S);
                chose = 0;
                break;
            case 4:
                IsFull(&S);
                chose = 0;
                break;
            case 5:
                printf("栈顶元素为%d",GetTop(S));
                chose = 0;
                break;
            case 6:
                PrintStack(S);
                chose = 0;
                break;
        }
        printf("\n---------------------------------------------\n");
        printf("请选择功能:\n1:入栈   2:出栈  3:判空 4:判满  5:读取栈顶元素 6:遍历  \n");
    }
    return 0;
}

修改如下,改动处见注释处,供参考:

#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define Stack_Size 20

typedef char SElemType; //typedef int SElemType; 修改
typedef struct{
    SElemType elem[Stack_Size];
    int top;
}SeqStack;

void InitStack(SeqStack *S)//初始化顺序栈
{
    S->top=-1;
}

int IsEmpty(SeqStack *S)//判断栈是否为空
{
    if(S->top==-1) printf("栈为空!");
    else printf("栈非空!"); 
}

int IsFull(SeqStack *S)//判断栈是否为满 
{
    if(S->top==Stack_Size-1) printf("栈已满!");
    else printf("栈未满!");
}

int Push(SeqStack *S,SElemType x)//入栈
{
    if(S->top==Stack_Size-1)
    {
        printf("栈已满!");
        return(FALSE);
    }
    S->top++;
    S->elem[S->top]=x;
    return(TRUE);
}
 
int Pop(SeqStack *S,SElemType* x)//出栈 int Pop(SeqStack *S,int x) 修改
{
    if(S->top==-1)
    {
        printf("栈为空!");
        return(FALSE);
    }
    else
    {
        *x=S->elem[S->top];
        S->top--;
        return(TRUE);
    }
}
 
SElemType GetTop(SeqStack S) //int GetTop(SeqStack S) 修改
{
    if(S.top==-1)
        return(FALSE);
    SElemType x = S.elem[S.top]; // 修改 int
    return x;
}
 
int PrintStack(SeqStack S)
{
    int i;
    if(S.top==-1)
    {
        printf("栈为空");
        return(FALSE);
    }
    for( i =0;i<=S.top;i++)
    {
        printf("          |%c|\n",S.elem[S.top-i]); //修改
        //printf("          |%d|\n",S.elem[S.top-i]);
    }
    return(TRUE);
}

int main()
{
    SeqStack S;
    InitStack(&S);
    int chose;
    SElemType push[20],pop,*p;  //int push,pop;  修改
    printf("\n---------------------------------------------\n");
    printf("请选择功能:\n1:入栈   2:出栈  3:判空 4:判满  5:读取栈顶元素 6:遍历 \n");
    scanf("%d",&chose);
    while(chose==0 || chose ==1 ||chose==2 ||chose==3 ||chose==4 ||chose==5 ||chose==6 ||chose==7 )
    {
        switch(chose)
        {
            case 0:
                scanf("%d",&chose);continue;
            case 1:
                printf("请输入入栈字串:");
                scanf("%s",push);  //scanf("%d",&push); 修改
                p = push;          //修改
                while (*p)  Push(&S,*p++); //修改
                printf("入栈后为:\n");
                PrintStack(S);
                chose = 0;
                break;
            case 2:
                printf("将栈顶元素 %c 出栈\n",GetTop(S));
                //printf("将栈顶元素 %d 出栈\n",GetTop(S)); 修改
                Pop(&S,&pop);          //修改
                printf("出栈后为:\n");
                PrintStack(S);
                chose = 0;
                break;
            case 3:
                IsEmpty(&S);
                chose = 0;
                break;
            case 4:
                IsFull(&S);
                chose = 0;
                break;
            case 5:
                printf("栈顶元素为%c",GetTop(S));
                //printf("栈顶元素为%d",GetTop(S)); 修改
                chose = 0;
                break;
            case 6:
                PrintStack(S);
                chose = 0;
                break;
        }
        printf("\n---------------------------------------------\n");
        printf("请选择功能:\n1:入栈   2:出栈  3:判空 4:判满  5:读取栈顶元素 6:遍历  \n");
    }
    return 0;
}