|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define INIT_STACK_SIZE 10
#define INCREMENT_STACK_SIZE 10
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;
void Initstack(sqStack *s)
{
s->base=(ElemType *)malloc(INIT_STACK_SIZE*sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top=s->base;
s->stackSize=INIT_STACK_SIZE;
}
void Push(sqStack *s,ElemType e)
{
if(s->top-s->base>=s->stackSize)
{
s->base=(ElemType *)realloc(s->base,(INIT_STACK_SIZE+s->stackSize)*sizeof(ElemType));
}
if(!s->base)
{
exit(0);
}
*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()
{
sqStack s;
int i,len,sum=0;
ElemType c;
Initstack(&s);
printf("请输入二进制字符串按#结束\n");
scanf("%c",&c);
while(c!='#')
{
Push(&s,c);
scanf("%c",&c);
}
getchar();
len=stackLen(s);
printf("当前栈的容量为:%d\n",len);
for(i=0;i<len;i++)
{
Pop(&s,&c);
sum=sum+(c-48)*pow(2,i);
}
printf("转化为十进制数为%d",sum);
return 0;
} |
-
|