当前位置:网站首页>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;
}
边栏推荐
- NGUI-UILabel
- RHSA first day operation
- Configure an encrypted web server
- Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
- 浅谈估值模型 (二): PE指标II——PE Band
- Vxlan 静态集中网关
- 普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
- 108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
- SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
- 小红书微服务框架及治理等云原生业务架构演进案例
猜你喜欢
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
Attack and defense world ----- summary of web knowledge points
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
静态Vxlan 配置
问题:先后键入字符串和字符,结果发生冲突
盘点JS判断空对象的几大方法
Epp+dis learning path (1) -- Hello world!
Configure an encrypted web server
30. Feed shot named entity recognition with self describing networks reading notes
随机推荐
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
Will the filing free server affect the ranking and weight of the website?
@Bean与@Component用在同一个类上,会怎么样?
Apache installation problem: configure: error: APR not found Please read the documentation
Introduction and application of smoothstep in unity: optimization of dissolution effect
Up meta - Web3.0 world innovative meta universe financial agreement
UP Meta—Web3.0世界创新型元宇宙金融协议
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
powershell cs-UTF-16LE编码上线
ES底层原理之倒排索引
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Static routing assignment of network reachable and telent connections
盘点JS判断空对象的几大方法
Is it safe to open an account in Ping An Securities mobile bank?
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
编译 libssl 报错
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
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)