ayicherry 发表于 2015-3-24 11:04:22

一个用栈实现简单进制转换的问题,求教

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define Stack_Init_Size 20
#define StackINCREMENT 10

typedef char ElemType;
typedef struct {
ElemType *base;
ElemType *top;
int stacksize;
}sqStack;

void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(Stack_Init_Size*sizeof(ElemType));
if (!s->base)
exit(0);
s->top = s->base;
s->stacksize = Stack_Init_Size;
}

void Push(sqStack *s, ElemType e)
{
if (s->top - s->base >= s->stacksize)
{
s->base = (ElemType *)realloc(s->base, (s->stacksize + StackINCREMENT)*sizeof(ElemType));
if (!s->base)
   exit(0);
s->top = s->base + s->stacksize;
s->stacksize = s->stacksize + StackINCREMENT;
}
*(s->top) = e;
s->top++;
}

void Pop(sqStack *s, ElemType *e)
{
if (s->base == s->top)
return;
*e = *--(s->top);
}
int StackLen(sqStack s)
{
return(s.top - s.base);
}

int main()
{
ElemType c;
sqStack s;
int len, i, sum = 0;
printf("Please enter a Binary digit:\n");
InitStack(&s);
scanf_s("%c", &c);
while (c != '#')
{
Push(&s, c);
scanf_s("%c", &c);
}
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();
}

编译没问题,但是运行的时候,我输入10011101#然后按回车键,它没有运行,反而是换行让我继续输入,这是什么问题,求高手指导一下,不甚感激。

ayicherry 发表于 2015-3-24 15:21:33

问题解决了,谢谢大家的关注!!
页: [1]
查看完整版本: 一个用栈实现简单进制转换的问题,求教