栈的一些简单问题(二进制和十进制的转化)
#include<stdio.h>#include<stdlib.h>
#include<math.h>
#define STACK_INITSIZE 20
#define STACKCREMENT 10
typedef char Elemtype;
typedef struct
{
Elemtype *base;
Elemtype *top;
int stacksize;
}sqStack;
void initStack(sqStack *s)
{
s->base=(char *)malloc(STACKCREMENT * sizeof(char));
if(!s->base)
{
puts("Error!");
}
s->top=s->base;
s->stacksize=STACK_INITSIZE;
}
void Push(sqStack *s,Elemtype e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(char *)realloc(s->base,(s->stacksize+STACKCREMENT) *sizeof(Elemtype));
if(!s->base)
{
puts("Error!");
}
s->top=s->base+s->stacksize;
s->stacksize=s->stacksize+STACKCREMENT;
}
*(s->top)=e;
s->top++;
}
void Pop (sqStack *s,Elemtype *e)
{
if(s->top==s->base)
{
return;
}
*e=*--(s->top);
}
int StackLen(sqStack s)
{
return (s.top-s.base);
}
int main(void)
{
Elemtype c;
sqStack s;
int len,i;
double sum=0;
printf("Please input a Binary digit\n");
initStack(&s);
scanf("%c",&c);
while(c!='#')
{
Push(&s,c);
scanf("%c",&c);
}
getchar();
len=StackLen(s);
for(i=0;i<len;i++)
{
Pop(&s,&c);
sum=sum+(c-48)*pow(2,i);
}
printf("Decimal is %d\n",sum);
getchar();
return 0;
}
这是一个简单的数据栈的问题,但小弟不才,调了半天总是结果是:Decimal is 0 是不是出栈函数写错了 ,还是别的地方出错了,哎 还是那句话,只缘身在此山中啊!!!