当前位置:网站首页>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;
}
边栏推荐
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- Zero shot, one shot and few shot
- Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- RHSA first day operation
- 111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
- 【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
- Will the filing free server affect the ranking and weight of the website?
- 数据库系统原理与应用教程(011)—— 关系数据库
- Tutorial on principles and applications of database system (009) -- conceptual model and data model
- 2022年在启牛开华泰的账户安全吗?
猜你喜欢
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
Flet tutorial 17 basic introduction to card components (tutorial includes source code)
EPP+DIS学习之路(1)——Hello world!
Attack and defense world - PWN learning notes
Tutorial on the principle and application of database system (011) -- relational database
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
@Bean与@Component用在同一个类上,会怎么样?
ENSP MPLS layer 3 dedicated line
[full stack plan - programming language C] basic introductory knowledge
Detailed explanation of debezium architecture of debezium synchronization
随机推荐
SQL head injection -- injection principle and essence
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
牛客网刷题网址
跨域问题解决方案
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
Zhimei creative website exercise
College entrance examination composition, high-frequency mention of science and Technology
What are the technical differences in source code anti disclosure
Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
(待会删)yyds,付费搞来的学术资源,请低调使用!
SQL lab 1~10 summary (subsequent continuous update)
2022 8th "certification Cup" China University risk management and control ability challenge
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
NGUI-UILabel
Learning and using vscode