当前位置:网站首页>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;
}

原网站

版权声明
本文为[Reluctant to let go]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071027393654.html