当前位置: 代码迷 >> C语言 >> [求助]栈的问题
  详细解决方案

[求助]栈的问题

热度:232   发布时间:2006-10-15 16:00:55.0
[求助]栈的问题

# include <stdio.h>
# include <stdlib.h>
# include <malloc.h>
# define Init_size 100
# define New_size 10

typedef struct {
int *base ,* top ;
int size;
} SqStack;

int InitStack (SqStack *S)
{
S->base=(int* )malloc (Init_size *sizeof(int) );
if (!S->base)
return (1);
else {
S->top =S->base;
S->size=Init_size;
return (0);
}

}
int Push (SqStack *S,int ch){
if (S->top-S->base>=S->size) {
S->base=(int *)realloc (S->base, (S->size+New_size)*sizeof (int));
if (!S->base)
{
return (1);
}
S->top=S->base+S->size;
S->size +=New_size;
}
*S->top=ch;
S->top++;
}

int Pop (SqStack *S)
{
if (S->top!=S->base)
return (*(--S->top));
}

void ClearStack (SqStack *S) {
if (S->top!=S->base)
S->top=S->base;

}

main (){

SqStack s;
int num,jz,a,result;
if (InitStack (&s))
{
puts ("fail!!\n");
exit (0);
}
ClearStack (&s);
puts ("please input a operation num\n");

scanf ("%d",&num);
if (num<0)
result=-num;
printf ("please input which JIN Zhi do you want \n");
scanf ("%d",&jz);

while (result)
{ a=result%jz ;
Push (&s,a);
result=result/jz;
}

if (num<0)
printf ("-");
while (s.top!=s.base){
printf ("%d",Pop(&s));
}
printf ("\n");
}

无论我输入什么结果都是一样的
怎么会这样
是算法错了吗


----------------解决方案--------------------------------------------------------

请写明要求助的问题


----------------解决方案--------------------------------------------------------
  相关解决方案