1 Star 0 Fork 0

付峻霖 / 数据结构

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
二进制转十进制 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
付峻霖 提交于 2021-04-11 19:39 . add 二进制转十进制.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
typedef int 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*)malloc((s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if (!s->base)
{//检查
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack* s, ElemType *e)//删除数据
{
if (s->top == s->base)//没数据了
{
return;
}
*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!='#')
{
Push(&s, c);
scanf("%c", &c);
}
getchar();//清理键盘缓冲区,把'\n'从缓冲区去掉
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\n", sum);
return 0;
}
C++
1
https://gitee.com/fujunlin/data-structure.git
git@gitee.com:fujunlin/data-structure.git
fujunlin
data-structure
数据结构
master

搜索帮助