#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXSIZE 100
#define EXSIZE 20
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
} sqStack;
void InitStack(sqStack *s)
{
s->base=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
if(!s->base) exit(0);
s->top=s->base;
s->stackSize=MAXSIZE;
}
void Pop(sqStack *s,ElemType *e)
{
if(s->top==s->base)
return;
*e=*--(s->top);
}
void Push(sqStack *s,ElemType e)
{
if(s->top-s->base>=s->stackSize)
{
s->base=(ElemType *)realloc(s->base,(s->stackSize+EXSIZE)*sizeof(ElemType));
if(!s->base) exit(0);
s->top=s->base+s->stackSize;
s->stackSize=s->stackSize+EXSIZE;
}
*(s->top)=e;
s->top++;
}
int StackLen(sqStack s){
return (s.top-s.base);
}
int main()
{
ElemType c;
sqStack s;
int len, i, sum = 0;
InitStack(&s);
printf("输入二进制数!\n");
scanf("%c", &c);
while( c != '\n' )
{
Push(&s, c);
scanf("%c", &c);
}
len = StackLen(s);
for( i=0; i < len; i++ )
{
Pop(&s, &c);
sum = sum + (c-48) * pow(2, i);
}
printf("十进制数是: %d\n", sum);
return 0;
}