当前位置:网站首页>利用棧來實現二進制轉化為十進制
利用棧來實現二進制轉化為十進制
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;
}边栏推荐
- [data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
- 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
- Upgrade from a tool to a solution, and the new site with praise points to new value
- 数据库系统原理与应用教程(008)—— 数据库相关概念练习题
- idea 2021中文乱码
- Niuke website
- 源代码防泄密中的技术区别再哪里
- Static comprehensive experiment
- The function of adding @ before the path in C #
- Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
猜你喜欢

什么是ESP/MSR 分区,如何建立ESP/MSR 分区

关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例

《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~

@What happens if bean and @component are used on the same class?
![108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]](/img/c0/8a7b52c46eadd27cf4784ab2f32002.png)
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
![112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]](/img/b6/6dfe9be842204567096d1f4292e8e7.png)
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]

数据库系统原理与应用教程(007)—— 数据库相关概念

SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)

Flet tutorial 17 basic introduction to card components (tutorial includes source code)

<No. 9> 1805. 字符串中不同整数的数目 (简单)
随机推荐
Vxlan 静态集中网关
问题:先后键入字符串和字符,结果发生冲突
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
SQL Lab (41~45) (continuous update later)
DOM parsing XML error: content is not allowed in Prolog
Is it safe to open an account in Ping An Securities mobile bank?
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
免备案服务器会影响网站排名和权重吗?
Let digital manage inventory
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
Problem: the string and characters are typed successively, and the results conflict
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
Detailed explanation of debezium architecture of debezium synchronization
Is it safe to open Huatai's account in kainiu in 2022?
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
2022 8th "certification Cup" China University risk management and control ability challenge
Hi3516 full system type burning tutorial