当前位置:网站首页>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;
}
边栏推荐
- 盘点JS判断空对象的几大方法
- Let digital manage inventory
- Up meta - Web3.0 world innovative meta universe financial agreement
- Zero shot, one shot and few shot
- 【深度学习】图像多标签分类任务,百度PaddleClas
- Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
- 110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
- Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
- 2022 8th "certification Cup" China University risk management and control ability challenge
- 112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
猜你喜欢
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
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
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
随机推荐
Review and arrangement of HCIA
About web content security policy directive some test cases specified through meta elements
ES底层原理之倒排索引
Hi3516全系统类型烧录教程
zero-shot, one-shot和few-shot
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
File upload vulnerability - upload labs (1~2)
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Apache installation problem: configure: error: APR not found Please read the documentation
问题:先后键入字符串和字符,结果发生冲突
UP Meta—Web3.0世界创新型元宇宙金融协议
Detailed explanation of debezium architecture of debezium synchronization
Hi3516 full system type burning tutorial
About sqli lab less-15 using or instead of and parsing
Static routing assignment of network reachable and telent connections
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
Let digital manage inventory
Static comprehensive experiment
ps链接图层的使用方法和快捷键,ps图层链接怎么做的