当前位置:网站首页>利用棧來實現二進制轉化為十進制
利用棧來實現二進制轉化為十進制
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;
}
边栏推荐
- Unity中SmoothStep介绍和应用: 溶解特效优化
- When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
- 【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
- AirServer自动接收多画面投屏或者跨设备投屏
- [play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
- Completion report of communication software development and Application
- Is it safe to open Huatai's account in kainiu in 2022?
- <No. 8> 1816. 截断句子 (简单)
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
猜你喜欢
idm服务器响应显示您没有权限下载解决教程
Unity中SmoothStep介绍和应用: 溶解特效优化
"Series after reading" my God! It's so simple to understand throttling and anti shake~
Hi3516全系统类型烧录教程
数据库系统原理与应用教程(007)—— 数据库相关概念
Completion report of communication software development and Application
MATLAB实现Huffman编码译码含GUI界面
idea 2021中文乱码
@What happens if bean and @component are used on the same class?
The road to success in R & D efficiency of 1000 person Internet companies
随机推荐
什么是局域网域名?如何解析?
ENSP MPLS layer 3 dedicated line
Improve application security through nonce field of play integrity API
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
Completion report of communication software development and Application
[neural network] convolutional neural network CNN [including Matlab source code 1932]
<No. 9> 1805. 字符串中不同整数的数目 (简单)
Unity中SmoothStep介绍和应用: 溶解特效优化
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
防红域名生成的3种方法介绍
EPP+DIS学习之路(2)——Blink!闪烁!
Rationaldmis2022 advanced programming macro program
2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
EPP+DIS学习之路(1)——Hello world!
RHSA first day operation
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
Review and arrangement of HCIA
College entrance examination composition, high-frequency mention of science and Technology
Vxlan 静态集中网关
什么是ESP/MSR 分区,如何建立ESP/MSR 分区