当前位置:网站首页>利用棧來實現二進制轉化為十進制
利用棧來實現二進制轉化為十進制
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;
}
边栏推荐
- NGUI-UILabel
- 免备案服务器会影响网站排名和权重吗?
- 数据库系统原理与应用教程(007)—— 数据库相关概念
- 数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
- "Series after reading" my God! It's so simple to understand throttling and anti shake~
- 平安证券手机行开户安全吗?
- Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
- wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
- 2022年在启牛开华泰的账户安全吗?
- idea 2021中文乱码
猜你喜欢
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Tutorial on principles and applications of database system (009) -- conceptual model and data model
PowerShell cs-utf-16le code goes online
Simple network configuration for equipment management
[neural network] convolutional neural network CNN [including Matlab source code 1932]
Completion report of communication software development and Application
Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本
消息队列消息丢失和消息重复发送的处理策略
《通信软件开发与应用》课程结业报告
浅谈估值模型 (二): PE指标II——PE Band
随机推荐
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
从工具升级为解决方案,有赞的新站位指向新价值
TypeScript 接口继承
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
College entrance examination composition, high-frequency mention of science and Technology
@What happens if bean and @component are used on the same class?
Problem: the string and characters are typed successively, and the results conflict
Improve application security through nonce field of play integrity API
NGUI-UILabel
SQL blind injection (WEB penetration)
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
开发一个小程序商城需要多少钱?
Zero shot, one shot and few shot
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Inverted index of ES underlying principle
Rationaldmis2022 array workpiece measurement
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
NGUI-UILabel
Processing strategy of message queue message loss and repeated message sending
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具