当前位置:网站首页>利用棧來實現二進制轉化為十進制
利用棧來實現二進制轉化為十進制
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;
}
边栏推荐
- Hi3516全系统类型烧录教程
- ES底层原理之倒排索引
- 编译 libssl 报错
- 盘点JS判断空对象的几大方法
- The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
- 浅谈估值模型 (二): PE指标II——PE Band
- 110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
- NGUI-UILabel
猜你喜欢
MATLAB实现Huffman编码译码含GUI界面
RHSA first day operation
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
Completion report of communication software development and Application
Upgrade from a tool to a solution, and the new site with praise points to new value
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Solutions to cross domain problems
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
Several methods of checking JS to judge empty objects
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
随机推荐
Present pod information to the container through environment variables
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
What is a LAN domain name? How to parse?
zero-shot, one-shot和few-shot
ENSP MPLS layer 3 dedicated line
How to connect 5V serial port to 3.3V MCU serial port?
<No. 8> 1816. Truncate sentences (simple)
About web content security policy directive some test cases specified through meta elements
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
顶级域名有哪些?是如何分类的?
SQL Lab (41~45) (continuous update later)
Steps of redis installation and self startup configuration under CentOS system
About sqli lab less-15 using or instead of and parsing
Idea 2021 Chinese garbled code
How to understand the clothing industry chain and supply chain
从工具升级为解决方案,有赞的新站位指向新价值
Rationaldmis2022 advanced programming macro program
Flet tutorial 17 basic introduction to card components (tutorial includes source code)
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels