求大神帮忙看看这个C语言程序哪里错了 本人新手 看不出来 谢谢
#include"stdio.h"#include"string.h"#include"malloc.h"#include"stdlib.h"#defineINIT100...
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#include "stdlib.h"
#define INIT 100
#define DECREAT 10
#define LEN sizeof(char)
#define NULL 0
typedef struct
{
char *base;
char *top;
int size;
}Stack;
void init(Stack *p)
{
p->base = (char *)malloc(INIT *LEN);
if(!p->base)
exit(1);
p->top = p->base;
p->size = INIT;
}
char getTop(Stack *p,char a)
{
if(p->base == p->top)
return NULL;
a = *(p->top - 1);
return a;
}
void Push(Stack *p,char a)
{
if(p->top - p->base >= p->size)
{
p->base = (char *)realloc(p->base,LEN * (p->size + DECREAT));
if(p->base == 0)
printf("failed!");
p->top = p->base + p->size;
p->size += DECREAT;
}
*p->top++ = a;
}
char Pop(Stack *p,char a)
{
if(p->top == p->base)
printf("null");
a = *--p->top;
}
main()
{
Stack *p;
char x,a,b;
int i = 1;
init(p);
while((x=getchar()) != '$')
Push(p,x);
b = getTop(p,a);
printf("%c",b);
b = Pop(p,a);
printf("%c",b);
} 展开
#include "string.h"
#include "malloc.h"
#include "stdlib.h"
#define INIT 100
#define DECREAT 10
#define LEN sizeof(char)
#define NULL 0
typedef struct
{
char *base;
char *top;
int size;
}Stack;
void init(Stack *p)
{
p->base = (char *)malloc(INIT *LEN);
if(!p->base)
exit(1);
p->top = p->base;
p->size = INIT;
}
char getTop(Stack *p,char a)
{
if(p->base == p->top)
return NULL;
a = *(p->top - 1);
return a;
}
void Push(Stack *p,char a)
{
if(p->top - p->base >= p->size)
{
p->base = (char *)realloc(p->base,LEN * (p->size + DECREAT));
if(p->base == 0)
printf("failed!");
p->top = p->base + p->size;
p->size += DECREAT;
}
*p->top++ = a;
}
char Pop(Stack *p,char a)
{
if(p->top == p->base)
printf("null");
a = *--p->top;
}
main()
{
Stack *p;
char x,a,b;
int i = 1;
init(p);
while((x=getchar()) != '$')
Push(p,x);
b = getTop(p,a);
printf("%c",b);
b = Pop(p,a);
printf("%c",b);
} 展开
1个回答
推荐于2016-06-19
展开全部
#define NULL 0 这个系统给了,不用定义
下面加一句
char getTop(Stack *p,char a)
{
if(p->base == p->top)
return NULL;
a = *p->top - 1;
return a;
}
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#include "stdlib.h"
#define INIT 100
#define DECREAT 10
#define LEN sizeof(char)
typedef struct
{
char *base;
char *top;
int size;
}Stack;
void init(Stack *p)
{
p->base = (char *)malloc(INIT *LEN);
if(!p->base)
exit(1);
p->top = p->base;
p->size = INIT;
}
char getTop(Stack *p,char a)
{
if(p->base == p->top)
return (char)NULL; //这个NULL是整型,不能转成char...
a = *p->top - 1;
return a;
}
void Push(Stack *p,char a)
{
if(p->top - p->base >= p->size)
{
p->base = (char *)realloc(p->base,LEN * (p->size + DECREAT));
if(p->base == 0)
printf("failed!");
p->top = p->base + p->size;
p->size += DECREAT;
}
*p->top++ = a;
}
char Pop(Stack *p,char a)
{
if(p->top == p->base)
printf("null");
a = *--p->top;
return a;
}
main()
{
Stack *p;
char x,a,b; //这里的a与上面的p没有赋值
int i = 1;
init(p);
while((x=getchar()) != '$')
Push(p,x);
b = getTop(p,a);
printf("%c",b);
b = Pop(p,a);
printf("%c",b);
}
追问
大神能否给发下个直接修改好的?
追答
什么功能?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询