注册 登录
编程论坛 C语言论坛

这个代码那个地方出问题了?

lsl9527 发布于 2021-03-30 16:37, 1491 次点击
#include<stdio.h>
#include<malloc.h>
#define MAXLEN 50                        //设定最大长度
#define SIZE 100
typedef struct
{
    char name[10];
    int age;
}DATA;

typedef struct stack
{
    DATA data[SIZE+1];            //数据元素
    int top;                            //栈顶
}StackType;

StackType *STInit()
{
    StackType *p;
    if(p=(StackType *)malloc(sizeof(StackType)))        //申请内存
    {
        p->top=0;                        //设置栈顶为0
        return p;                        //返回指向栈的指针
    }
    return NULL;
}

int STIsEmpty(StackType *s)                //判断栈是否为空
{
    int t;
    t=(s->top==0);
    return t;
}

int STIsFull(StackType *s)                //判断栈是否已满
{
    int t;
    t=(s->top==MAXLEN);
    return t;
}

void STClear(StackType *s)                //清空栈
{
    s->top=0;
}

void STFree(StackType *s)                //释放栈所占空间
{
    if(s)
    {
        free(s);
    }
}

int PushST(StackType *s,DATA data)            //入栈
{
    if((s->top+1)>MAXLEN)
    {
        printf("栈溢出!\n");
        return 0;
     }
     s->data
      [++s->top]=data;                //将元素入栈
     return 1;
     }

DATA PopST(StackType *s)                    //出栈
{
    if(s->top==0)
    {
        printf("栈为空!\n");
        exit(1);
    }
    return(s->data[s->top--]);
}

DATA PeekST(StackType *s)                    //读栈顶数据
{
    if(s->top==0)
    {
        printf("栈为空!\n");
        exit(1);
    }
    return (s->data[s->top]);
}

void conversion(int a,int b)
 {
     int i;
     StackType s;
     init(&s);
     while(a);
     {
         Push(&s,a%b);
         a=a/b;
     }
     while(!IsEmpty(&s))
     {
         i=Pop(&s);
         printf("%d",i);
     }
 }

int main(int argc,char *argv[])
 {
     int a,b;
     printf("please input the data you want to conversion\n");
     scanf("%d",&a);
     printf("please input the scale you want to convert to\n");
     scanf("%d",&b);
     conversion(a,b);
     return 0;
 }
     
3 回复
#2
apull2021-03-30 16:55
函数名和调用时要一致
#3
lsl95272021-03-30 17:38
回复 2楼 apull
能否具体说明
#4
apull2021-03-30 21:10
上面是StackType *STInit(){},下面是init(&s);,其他如Push,IsEmpty等都一样。
1