|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#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#然后按回车键,它没有运行,反而是换行让我继续输入,这是什么问题,求高手指导一下,不甚感激。
|
|