检错 C语言

来源:百度知道 编辑:UC知道 时间:2024/07/07 10:06:44
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
typedef struct Stack{
int *top;
int *base;
}Stack;
Stack *initstack()
{
Stack *S;
S=NULL;
S->top=(int*)malloc(12*sizeof(int));
S->base=S->top;
return S;
}
void push(Stack *S,int a)
{
if(S->top-S->base>0)
*(S->top++)=a;
}
int pop(Stack *S)
{
if(S->top-S->base>0)
return *(--S->top);
else
return 0;
}

void main()
{
Stack *S;
int a,b,n;
S=initstack();
printf("请输入要转换的数");
scanf("%d",&a);
printf("请输入转化进制");
scanf("%d",&b);
while(a){
push(S,a%b);
a=a/b;
}
while(n){
n=pop(S);
printf("%d\t",n);
}
}

这样如何?
#include "stdio.h"
#include "stdlib.h"
typedef struct Stack{
int *top;
int *base;
int size;
}Stack;
Stack *initstack(int sz)
{
Stack *S = (Stack *)malloc(sizeof(Stack));
S->size = sz;
S->top=(int*)malloc(sz*sizeof(int));
S->base=S->top;
return S;
}
void push(Stack *S,int a)
{
if(S->top < S->base + S->size)
*S->top++ = a;
}
int pop(Stack *S)
{
if(S->top>S->base)
return *(--S->top);
else
return 0;
}

void main()
{
Stack *S;
int a,b,n;
S=initstack(12);
printf("请输入要转换的数");
scanf("%d",&a);
printf("请输入转化进制");
scanf("%d",&b);
while(a){
push(S,a%b);
a=a/b;
}
while(n){
n=pop(S);
printf("%d\t"