利用栈的数据结构,将二进制数转换为十进制数。
由于栈具有后进先出的特性,因此可以用栈很方便地实现二进制转换为十进制。
#include "stdio.h"
#include "math.h"
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef char ElemType;
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}sqStack;
initStack(sqStack *s)
{
//内存中开辟一段连续空间作为栈空间,首地址赋给s->base
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!s->base) exit(0); //分配空间次数
s->top = s->base; //最开始,栈顶就是栈底
s->stacksize = STACK_INIT_SIZE; //最大容量为STACK_INIT_SIZE
}
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++;
}
Pop(sqStack *s, ElemType *e){
if(s->top == s->base) return;
*e = *--(s->top);
}
int StackLen(sqStack s){
return (s.top - s.base);
}
main()
{
ElemType c;
sqStack s;
int len, i, sum=0;
printf("Please input a Binary digit\n");
initStack(&s); //创建一个栈,用来存放二进制字符串
//输入0/1字符表示的二进制数,以#结束
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);
getche(); //该函数一般在linux下使用
}