当前位置:网站首页>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;
}边栏推荐
- Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
- Several methods of checking JS to judge empty objects
- 108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
- DOM parsing XML error: content is not allowed in Prolog
- 消息队列消息丢失和消息重复发送的处理策略
- Simple network configuration for equipment management
- Review and arrangement of HCIA
- Idea 2021 Chinese garbled code
- Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
- How much does it cost to develop a small program mall?
猜你喜欢

Zhimei creative website exercise

PowerShell cs-utf-16le code goes online
![[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation](/img/14/6e440f3c4e04d9b322f0c3f43e213c.png)
[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation

千人规模互联网公司研发效能成功之路

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

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

关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例

跨域问题解决方案
![108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]](/img/c0/8a7b52c46eadd27cf4784ab2f32002.png)
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]

(待会删)yyds,付费搞来的学术资源,请低调使用!
随机推荐
Epp+dis learning path (1) -- Hello world!
Introduction to three methods of anti red domain name generation
The road to success in R & D efficiency of 1000 person Internet companies
NGUI-UILabel
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
Minimalist movie website
Problem: the string and characters are typed successively, and the results conflict
Tutorial on principles and applications of database system (009) -- conceptual model and data model
平安证券手机行开户安全吗?
《通信软件开发与应用》课程结业报告
The function of adding @ before the path in C #
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
千人规模互联网公司研发效能成功之路
【统计学习方法】学习笔记——提升方法