当前位置:网站首页>Using stack to convert binary to decimal
Using stack to convert binary to decimal
2022-07-07 12:26:00 【Reluctant to let go】
How to convert binary into decimal ?
Give me a chestnut : such as (100101)2( This 2 It's a subscript ) The method of converting to decimal :
1*2^0+0*2^1+1*2^2+0*2^3+0*2^4+1*2^5
Knowing this, it's easy to do next :100101 Stack order 1->0->0->1->0->1
There's not much nonsense , Go straight to the code
First define a stack with a structure :
typedef struct {
ElemType *base;
ElemType *top;
int stacksize;
} sqstack;
Initialization stack :
void InitStack(sqstack *s) { // Initialization stack
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); //malloc To apply for space ,
if (!s->base) { // If the application fails
exit(0);
}
s->top = s->base; // The top of the stack is equal to the bottom of the stack
s->stacksize = STACK_INIT_SIZE;
}
Insert ( Pressing stack ) operation :
void Push(sqstack*s, ElemType e) { // The insert ( Pressing stack )
if (s->top - s->base >= s->stacksize) {// If the stack overflows
s->base = (ElemType*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(ElemType));//realloc To apply for space , Save the original data , And add a new space , To find a new space to store data
if (!s->base) {
exit(0);
}
}
*(s->top) = e;// Stack top assignment
s->top++;// To the top of the stack +1
}
Throw out ( Out of the stack ) operation :
void Pop(sqstack*s, ElemType*e) {// Throw operation
if (s->top == s->base) {
return;
}
*e = *--(s->top);
}
Calculate the length of the stack :
int StackLen(sqstack s) {// Calculate the length of the stack
return (s.top - s.base);
}
The main function :
int main() {
ElemType c;
sqstack s;
int len, sum = 0;
InitStack(&s);
printf(" Please enter binary number , Input # The symbol indicates the end !\n");
scanf("%c", &c);
while (c != '#') {
Push(&s, c);
scanf("%c", &c);
}
getchar();// Absorb spaces
len = StackLen(s);
printf(" The current capacity of the stack is :%d\n", len);
for (int i = 0; i < len; i++) {
Pop(&s, &c);
sum = sum + ((c - 48) << i);//c-48 from ascll Code table into shaping ,(c-48)<<i Bit operation is equivalent to 2^i
}
printf(" The converted decimal number is :%d\n", sum);
return 0;
}
The whole code :
#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) { // Initialization stack
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); //malloc To apply for space ,
if (!s->base) { // If the application fails
exit(0);
}
s->top = s->base; // The top of the stack is equal to the bottom of the stack
s->stacksize = STACK_INIT_SIZE;
}
void Push(sqstack*s, ElemType e) { // The insert ( Pressing stack )
if (s->top - s->base >= s->stacksize) {// If the stack overflows
s->base = (ElemType*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(ElemType));//realloc To apply for space , Save the original data , And add a new space , To find a new space to store data
if (!s->base) {
exit(0);
}
}
*(s->top) = e;// Stack top assignment
s->top++;// To the top of the stack +1
}
void Pop(sqstack*s, ElemType*e) {// Throw operation
if (s->top == s->base) {
return;
}
*e = *--(s->top);
}
int StackLen(sqstack s) {// Calculate the length of the stack
return (s.top - s.base);
}
int main() {
ElemType c;
sqstack s;
int len, sum = 0;
InitStack(&s);
printf(" Please enter binary number , Input # The symbol indicates the end !\n");
scanf("%c", &c);
while (c != '#') {
Push(&s, c);
scanf("%c", &c);
}
getchar();// Absorb spaces
len = StackLen(s);
printf(" The current capacity of the stack is :%d\n", len);
for (int i = 0; i < len; i++) {
Pop(&s, &c);
sum = sum + ((c - 48) << i);//c-48 from ascll Code table into shaping ,(c-48)<<i Bit operation is equivalent to 2^i
}
printf(" The converted decimal number is :%d\n", sum);
return 0;
}
边栏推荐
- Introduction and application of smoothstep in unity: optimization of dissolution effect
- gcc 编译报错
- EPP+DIS学习之路(2)——Blink!闪烁!
- Inverted index of ES underlying principle
- TypeScript 接口继承
- sql-lab (54-65)
- (待会删)yyds,付费搞来的学术资源,请低调使用!
- Upgrade from a tool to a solution, and the new site with praise points to new value
- 110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
- What is a LAN domain name? How to parse?
猜你喜欢
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Sonar:cognitive complexity
Zero shot, one shot and few shot
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
静态Vxlan 配置
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
H3C HCl MPLS layer 2 dedicated line experiment
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
idm服务器响应显示您没有权限下载解决教程
随机推荐
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Sonar:cognitive complexity
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
idm服务器响应显示您没有权限下载解决教程
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
Is it safe to open Huatai's account in kainiu in 2022?
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
牛客网刷题网址
Hi3516 full system type burning tutorial
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
【统计学习方法】学习笔记——支持向量机(上)
如何理解服装产业链及供应链
Introduction to three methods of anti red domain name generation
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
EPP+DIS学习之路(2)——Blink!闪烁!
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
千人规模互联网公司研发效能成功之路
问题:先后键入字符串和字符,结果发生冲突
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]