当前位置:网站首页>利用栈来实现二进制转化为十进制
利用栈来实现二进制转化为十进制
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;
}边栏推荐
- 编译 libssl 报错
- RHSA first day operation
- Completion report of communication software development and Application
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
- Let digital manage inventory
- Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
- The road to success in R & D efficiency of 1000 person Internet companies
- Sonar:cognitive complexity
- <No. 9> 1805. 字符串中不同整数的数目 (简单)
- Several methods of checking JS to judge empty objects
猜你喜欢

idea 2021中文乱码

SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)

对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
![111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]](/img/2e/da45198bb6fb73749809ba0c4c1fc5.png)
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]

Solutions to cross domain problems

Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB

"Series after reading" my God! It's so simple to understand throttling and anti shake~

Matlab implementation of Huffman coding and decoding with GUI interface

Time bomb inside the software: 0-day log4shell is just the tip of the iceberg

Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
随机推荐
Let digital manage inventory
30. Feed shot named entity recognition with self describing networks reading notes
Solutions to cross domain problems
Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
MATLAB實現Huffman編碼譯碼含GUI界面
什么是局域网域名?如何解析?
What are the technical differences in source code anti disclosure
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
Apache installation problem: configure: error: APR not found Please read the documentation
消息队列消息丢失和消息重复发送的处理策略
File upload vulnerability - upload labs (1~2)
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
Flet tutorial 17 basic introduction to card components (tutorial includes source code)
The function of adding @ before the path in C #
Attack and defense world ----- summary of web knowledge points
Niuke website
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code