当前位置:网站首页>利用棧來實現二進制轉化為十進制
利用棧來實現二進制轉化為十進制
2022-07-07 12:26: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;
}边栏推荐
- 112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
- SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
- Upgrade from a tool to a solution, and the new site with praise points to new value
- 从工具升级为解决方案,有赞的新站位指向新价值
- <No. 8> 1816. Truncate sentences (simple)
- Up meta - Web3.0 world innovative meta universe financial agreement
- (to be deleted later) yyds, paid academic resources, please keep a low profile!
- Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
- 数据库系统原理与应用教程(011)—— 关系数据库
- @What happens if bean and @component are used on the same class?
猜你喜欢

Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt

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

Introduction and application of smoothstep in unity: optimization of dissolution effect

Completion report of communication software development and Application

About sqli lab less-15 using or instead of and parsing

Hi3516全系统类型烧录教程

30. Feed shot named entity recognition with self describing networks reading notes

UP Meta—Web3.0世界创新型元宇宙金融协议

SQL Lab (41~45) (continuous update later)

@Bean与@Component用在同一个类上,会怎么样?
随机推荐
Review and arrangement of HCIA
平安证券手机行开户安全吗?
跨域问题解决方案
EPP+DIS学习之路(2)——Blink!闪烁!
免备案服务器会影响网站排名和权重吗?
消息队列消息丢失和消息重复发送的处理策略
Up meta - Web3.0 world innovative meta universe financial agreement
EPP+DIS学习之路(1)——Hello world!
Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
UP Meta—Web3.0世界创新型元宇宙金融协议
SQL blind injection (WEB penetration)
Tutorial on the principle and application of database system (011) -- relational database
《通信软件开发与应用》课程结业报告
30. Feed shot named entity recognition with self describing networks reading notes
idm服务器响应显示您没有权限下载解决教程
HCIA复习整理
Detailed explanation of debezium architecture of debezium synchronization
千人规模互联网公司研发效能成功之路
Typescript interface inheritance
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题