当前位置:网站首页>Utiliser la pile pour convertir le binaire en décimal
Utiliser la pile pour convertir le binaire en décimal
2022-07-07 12:26:00 【Je ne peux pas le laisser tomber.】
Comment convertir le binaire en décimal?
Prends un marron.:Par exemple,(100101)2(C'est2C'est un indice.)Méthode de conversion en décimale:
1*2^0+0*2^1+1*2^2+0*2^3+0*2^4+1*2^5
C'est facile à faire après:100101Ordre de pile1->0->0->1->0->1
Pas grand - chose.,Directement au Code
Définissez d'abord une pile avec une structure:
typedef struct {
ElemType *base;
ElemType *top;
int stacksize;
} sqstack;Initialiser la pile:
void InitStack(sqstack *s) { //Initialiser la pile
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); //mallocDemande d'espace,
if (!s->base) { //Si la demande échoue
exit(0);
}
s->top = s->base; //Le Haut de la pile est égal au bas de la pile
s->stacksize = STACK_INIT_SIZE;
}Insérer(Pile de pression)Fonctionnement:
void Push(sqstack*s, ElemType e) { //Opération d'insertion(Pile de pression)
if (s->top - s->base >= s->stacksize) {// Si la pile déborde
s->base = (ElemType*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(ElemType));//reallocDemande d'espace, Enregistrer les données originales , Et ajouter un nouvel espace , Pour trouver un nouvel espace pour stocker les données
if (!s->base) {
exit(0);
}
}
*(s->top) = e;// Affectation du Haut de la pile
s->top++;//En haut de la pile+1
}Jetez(Hors de la pile)Fonctionnement:
void Pop(sqstack*s, ElemType*e) {// Lancer l'opération
if (s->top == s->base) {
return;
}
*e = *--(s->top);
}Calculer la longueur de la pile:
int StackLen(sqstack s) {//Calculer la longueur de la pile
return (s.top - s.base);
}Fonction principale:
int main() {
ElemType c;
sqstack s;
int len, sum = 0;
InitStack(&s);
printf("Veuillez saisir un nombre binaire,Entrée# Le symbole indique la fin !\n");
scanf("%c", &c);
while (c != '#') {
Push(&s, c);
scanf("%c", &c);
}
getchar();//Espace absorbant
len = StackLen(s);
printf("La capacité actuelle de la pile est:%d\n", len);
for (int i = 0; i < len; i++) {
Pop(&s, &c);
sum = sum + ((c - 48) << i);//c-48Parascll Conversion de la table de codes en forme ,(c-48)<<iL'Opération bit est équivalente à2^i
}
printf(" Le nombre décimal converti est :%d\n", sum);
return 0;
}Tout le 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) { //Initialiser la pile
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); //mallocDemande d'espace,
if (!s->base) { //Si la demande échoue
exit(0);
}
s->top = s->base; //Le Haut de la pile est égal au bas de la pile
s->stacksize = STACK_INIT_SIZE;
}
void Push(sqstack*s, ElemType e) { //Opération d'insertion(Pile de pression)
if (s->top - s->base >= s->stacksize) {// Si la pile déborde
s->base = (ElemType*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(ElemType));//reallocDemande d'espace, Enregistrer les données originales , Et ajouter un nouvel espace , Pour trouver un nouvel espace pour stocker les données
if (!s->base) {
exit(0);
}
}
*(s->top) = e;// Affectation du Haut de la pile
s->top++;//En haut de la pile+1
}
void Pop(sqstack*s, ElemType*e) {// Lancer l'opération
if (s->top == s->base) {
return;
}
*e = *--(s->top);
}
int StackLen(sqstack s) {//Calculer la longueur de la pile
return (s.top - s.base);
}
int main() {
ElemType c;
sqstack s;
int len, sum = 0;
InitStack(&s);
printf("Veuillez saisir un nombre binaire,Entrée# Le symbole indique la fin !\n");
scanf("%c", &c);
while (c != '#') {
Push(&s, c);
scanf("%c", &c);
}
getchar();//Espace absorbant
len = StackLen(s);
printf("La capacité actuelle de la pile est:%d\n", len);
for (int i = 0; i < len; i++) {
Pop(&s, &c);
sum = sum + ((c - 48) << i);//c-48Parascll Conversion de la table de codes en forme ,(c-48)<<iL'Opération bit est équivalente à2^i
}
printf(" Le nombre décimal converti est :%d\n", sum);
return 0;
}边栏推荐
- 小红书微服务框架及治理等云原生业务架构演进案例
- 110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
- 千人规模互联网公司研发效能成功之路
- 什么是局域网域名?如何解析?
- 【统计学习方法】学习笔记——提升方法
- Inverted index of ES underlying principle
- 即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
- Niuke website
- @Bean与@Component用在同一个类上,会怎么样?
- SQL head injection -- injection principle and essence
猜你喜欢

File upload vulnerability - upload labs (1~2)

SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)

问题:先后键入字符串和字符,结果发生冲突

Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
![An error occurred when vscade tried to create a file in the target directory: access denied [resolved]](/img/14/9899f5a765872fb3238be4305a2dc7.png)
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]

盘点JS判断空对象的几大方法

Attack and defense world ----- summary of web knowledge points

Introduction and application of smoothstep in unity: optimization of dissolution effect

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)
随机推荐
Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
The road to success in R & D efficiency of 1000 person Internet companies
如何理解服装产业链及供应链
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
Problem: the string and characters are typed successively, and the results conflict
What are the top-level domain names? How is it classified?
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
2022年在启牛开华泰的账户安全吗?
Will the filing free server affect the ranking and weight of the website?
NGUI-UILabel
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
zero-shot, one-shot和few-shot
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
idm服务器响应显示您没有权限下载解决教程
How to understand the clothing industry chain and supply chain
Is it safe to open an account in Ping An Securities mobile bank?