当前位置:网站首页>利用栈来实现二进制转化为十进制
利用栈来实现二进制转化为十进制
2022-07-07 10:27:00 【舍不得,放不下】
如何实现二进制转化为十进制呢?
举个栗子吧:比如(100101)2(这个2是下标哈)转化为十进制的方法:
1*2^0+0*2^1+1*2^2+0*2^3+0*2^4+1*2^5
知道这个接下来就好办了:100101入栈顺序1->0->0->1->0->1
废话不多数,直接上代码
先用结构体定义一个栈:
typedef struct {
ElemType *base;
ElemType *top;
int stacksize;
} sqstack;初始化栈:
void InitStack(sqstack *s) { //初始化栈
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); //malloc申请空间,
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));//realloc申请空间,将原有数据保存,并添加新的空间,来找新的一份空间储存数据
if (!s->base) {
exit(0);
}
}
*(s->top) = e;//栈顶赋值
s->top++;//栈顶+1
}抛出(出栈)操作:
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, sum = 0;
InitStack(&s);
printf("请输入二进制数,输入#符号表示结束!\n");
scanf("%c", &c);
while (c != '#') {
Push(&s, c);
scanf("%c", &c);
}
getchar();//吸收空格
len = StackLen(s);
printf("栈的当前容量是:%d\n", len);
for (int i = 0; i < len; i++) {
Pop(&s, &c);
sum = sum + ((c - 48) << i);//c-48由ascll码表转化为整形,(c-48)<<i位运算相当于2^i
}
printf("转化的十进制数是:%d\n", sum);
return 0;
}整个代码:
#include<stdio.h>
#include<stdlib.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)); //malloc申请空间,
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));//realloc申请空间,将原有数据保存,并添加新的空间,来找新的一份空间储存数据
if (!s->base) {
exit(0);
}
}
*(s->top) = e;//栈顶赋值
s->top++;//栈顶+1
}
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, sum = 0;
InitStack(&s);
printf("请输入二进制数,输入#符号表示结束!\n");
scanf("%c", &c);
while (c != '#') {
Push(&s, c);
scanf("%c", &c);
}
getchar();//吸收空格
len = StackLen(s);
printf("栈的当前容量是:%d\n", len);
for (int i = 0; i < len; i++) {
Pop(&s, &c);
sum = sum + ((c - 48) << i);//c-48由ascll码表转化为整形,(c-48)<<i位运算相当于2^i
}
printf("转化的十进制数是:%d\n", sum);
return 0;
}边栏推荐
- Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
- 问题:先后键入字符串和字符,结果发生冲突
- Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
- Niuke website
- Hi3516全系统类型烧录教程
- Idea 2021 Chinese garbled code
- 编译 libssl 报错
- NGUI-UILabel
- [data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
- EPP+DIS学习之路(1)——Hello world!
猜你喜欢

Review and arrangement of HCIA

Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition

NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
![111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]](/img/2e/da45198bb6fb73749809ba0c4c1fc5.png)
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]

AirServer自动接收多画面投屏或者跨设备投屏

SQL Lab (46~53) (continuous update later) order by injection

Up meta - Web3.0 world innovative meta universe financial agreement

消息队列消息丢失和消息重复发送的处理策略

跨域问题解决方案

Inverted index of ES underlying principle
随机推荐
Inverted index of ES underlying principle
idea 2021中文乱码
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
SQL lab 1~10 summary (subsequent continuous update)
UP Meta—Web3.0世界创新型元宇宙金融协议
数据库系统原理与应用教程(007)—— 数据库相关概念
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
Introduction and application of smoothstep in unity: optimization of dissolution effect
编译 libssl 报错
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
Hi3516全系统类型烧录教程
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
【深度学习】图像多标签分类任务,百度PaddleClas
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
Rationaldmis2022 array workpiece measurement
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
问题:先后键入字符串和字符,结果发生冲突
zero-shot, one-shot和few-shot