当前位置:网站首页>Application of C language stack: binary conversion
Application of C language stack: binary conversion
2022-04-22 05:28:00 【Li Feiyu_ smile】
The application of the stack : The decimal system in binary conversion is converted to binary number
List of articles
Preface
Stack : In the data structure : First in, then out , The elements of the advanced stack go out later , And it happens that when we convert binary , You need to reverse the number when you convert it , At this time, it is an application scenario that uses the characteristics of stack
Tips : The following is the main body of this article , The following cases can be used for reference
Two 、 Use steps
1. Import and stock in
The code is as follows ( Example ):
#include<stdio.h> // Call basic input / Output library file
#include<stdlib.h> // Call the standard library header file ,malloc The function is in this header file
#include<process.h> // Use system library files , Pause for a moment , See the execution result
2. Code implementation
The code is as follows ( Example ):
This example is to 23 An example of converting decimal numbers to binary numbers :
Source code , Can be used between
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#define MAX 50 // The maximum storage space of the stack
typedef struct Stack
{
int top; // Top pointer of stack
int sa[MAX]; // Stack capacity
}RStack;
// Generate an empty stack
RStack *init()
{
RStack *SS;
SS = (struct Stack*)malloc(sizeof(struct Stack)); // To apply for space
SS->top = 0;
return SS;
}
// Into the stack
void push(RStack *S, int x)
{
if (S->top == MAX)
{
printf(" Stack full ");
exit(0); // Exit procedure
}
S->sa[S->top] = x;
S->top++; // Add up 1( Always point to the previous bit of the element at the top of the stack )
}
// Judge whether the stack is empty
bool isEmpty(RStack S)
{
if (S.top == 0)
{
return true;
}
return false;
}
// Out of the stack
int pop(RStack* S)
{
if (isEmpty(*S))
{
printf(" The stack is empty ");
exit(0);
}
return S->sa[--S->top];
}
// The process of printing
void display(RStack *S)
{
while (S->top != 0)
{
printf("%d", pop(S)); // Use the stack to print the results , Just to verify the operation rules of the stack
}
}
// Decimal to binary
void convert(RStack *S, int x)
{
int n = 0; // The elements in the stack
while (x > 0)
{
n = x % 2;
push(S, n);
x = x / 2;
}
}
int main()
{
RStack* S;
S = init();
convert(S, 23); // Find a decimal as 23 Binary number of
printf("23 Binary number of :");//
display(S);
system("pause");
return 0;
}
The result is :23 Binary number of :10111
It's used here url Data requested by the network .
summary
There are many applications of stack , But for new students , I'm learning C The process of language , It also involves the problem of binary conversion , This article is also for students to associate with the knowledge they have learned to solve the existing difficulties .
版权声明
本文为[Li Feiyu_ smile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210618260365.html
边栏推荐
- Meet DOM
- Aiming position of unity shooting game
- Complexité temporelle et spatiale
- Mysql database for the 11th time
- Access denied for user 'root' @ '% MySQL problem (solved)
- GBase 8s V8.8 SQL 指南:教程-5.2(3)
- Parsing of thread safe classes: (2.8-3.6)
- 深圳-西双版纳
- Analysis of database log level: (2022.2.21-2022.2.27)
- Access denied for user ‘root‘@‘% mysql在问题(解决)
猜你喜欢

Shenzhen Xishuangbanna

Neural network -- basic idea

Idea 2021.1 Useful settings

(2022.1.31-2022.2.14) template mode analysis

2022-1-17 to 2022-1-30 iterator mode

Realization of mathematical function curve editor with minscript script language

使用easyexcel导出excel表格

Fundamentals of graphics - flood

Neural network -- BP neural network

MySQL數據庫第十一次作業-視圖的應用
随机推荐
Codeforces Round #781 (Div. 2) ABCD
GBase 8s V8.8 SQL 指南:教程-6.1.1(2)
Measuring the global recursive DNS infrastructure: a view from the edge
Access denied for user ‘root‘@‘% mysql在问题(解决)
Visitor mode from 2022-1-3 to 2022-1-16
GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.2 (1)
[WPF] data template selector
Leetcode 1561. Maximum number of coins you can get
GBase 8s V8.8 SQL 指南:教程-5.3
Auto.js 画布设置防锯齿paint.setAntiAlias(true);
The eleventh job of MySQL database - Application of view
用minscript脚本语言实现数学函数曲线编辑器
Leetcode 1387. Sort integers by the power value
Von Neumann architecture
GBase 8s V8.8 SQL 指南:教程-6.1.2(1)
abcabc
Delete Xunlei and see the uninstall residue in the folder right-click menu
Fundamentals of graphics - screen spatial reflection (SSR)
時間複雜度和空間複雜度
Analysis of database log level: (2022.2.21-2022.2.27)