Conversion ideas:
This code realizes the conversion from decimal to binary, octal and hexadecimal. The code body is implemented by stack. Let's first look at the method of converting decimal to binary. The conversion law is to divide the decimal number by 2 to get the remainder, that is, the remainder first divided is the highest bit of binary, and the last remainder is the lowest bit of binary. We know that the main feature of the stack is first in and last out, which can help us achieve the effect that the remainder that comes out first is the highest. Just put the remainder into the stack in turn and then out of the stack in turn. We take the decimal number 10 to binary as an example, and the specific conversion process is shown in the figure below.
Implementation of specific conversion:
Stack has two storage structures, sequential storage and chain storage. We use sequential storage here. Then we need to define a structure with an array of int type and a top to store the subscripts of the top elements of the stack. The specific definition is shown in the figure below:
Next, we need to write several operations about the stack, such as stack creation, stack entry, stack exit, stack destruction, etc.
We can create a new stack.c file to write the stack operation functions, and a new stack.h file to define the structure and declaration functions, so that the header file corresponding to this file can be directly included where the stack is used next time.
The function implementation part adopts the menu mode, and the user can choose the number of hexadecimal conversion, which needs the user to exit manually.
The specific codes are as follows:
stack.c file:
#include "stack.h" #include "stdlib.h" #include "stdio.h" //Create an empty stack Sqstack * Stack_Creat(Sqstack * Stack) { Stack = (Sqstack *)malloc(sizeof(Sqstack)); if(Stack == NULL) { printf("Space is full!\n"); return 0; } Stack->top = -1; //Empty stack return Stack; } //Destroy stack void Stack_Destory(Sqstack * Stack) { free(Stack); } //Out of stack ElemType Stack_Pop(Sqstack * Stack) { ElemType e=-1; if(Stack->top == -1) printf("The stack is empty and cannot be out of the stack!\n"); else { e = Stack->data[Stack->top]; Stack->top--; } return e; } //Push void Stack_Push(Sqstack * Stack,ElemType e) { if(Stack->top == MaxSize - 1) { printf("The stack is full and cannot be stacked!\n"); } else { Stack->top++; Stack->data[Stack->top] = e; } } //Determine whether the stack is empty int Stack_Empty(Sqstack * Stack) { return (Stack->top == -1 ); }
stack.h file:
#ifndef __STACK_H #define __STACK_H #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int top; }Sqstack; Sqstack * Stack_Creat(Sqstack * Stack); void Stack_Destory(Sqstack * Stack); ElemType Stack_Pop(Sqstack * Stack); void Stack_Push(Sqstack * Stack,ElemType e); int Stack_Empty(Sqstack * Stack); #endif
In the main function implementation part of the function, you can create a. c file at will. The code is as follows:
#include "stack.h" #include <stdio.h> void DTB(int num,Sqstack * Stack) { char num_b[33] ; //Used to store the converted binary char * temp = num_b; while(num) //Push { Stack_Push(Stack,num%2); num /= 2; } while(!Stack_Empty(Stack))//Stack until stack is empty { *temp = Stack_Pop(Stack) + '0'; temp++; } *temp = '\0'; printf("The binary number corresponding to this number is:%s\n",num_b); } void DTO(int num,Sqstack * Stack) { char num_o[12] ; //Used to store the converted binary char * temp = num_o; while(num) //Push { Stack_Push(Stack,num%8); num /= 8; } while(!Stack_Empty(Stack))//Stack until stack is empty { *temp = Stack_Pop(Stack) + '0'; temp++; } *temp = '\0'; printf("The octal number corresponding to this number is:%s\n",num_o); } void DTH(int num,Sqstack * Stack) { char num_h[12] ; //Used to store the converted binary char * temp = num_h; int top_num; while(num) //Push { Stack_Push(Stack,num%16); num /= 16; } while(!Stack_Empty(Stack))//Stack until stack is empty { top_num = Stack_Pop(Stack); if(top_num>9) *temp = top_num - 10 + 'A'; else *temp = top_num + '0'; temp++; } *temp = '\0'; printf("The hexadecimal number corresponding to this number is:%s\n",num_h); } void run() { int flag=1,a,num_d; Sqstack * Stack; Stack = Stack_Creat(Stack); printf("Please enter a decimal number:"); scanf("%d",&num_d); while(flag) { printf("**************************************\n"); printf("* 1.Decimal to binary *\n"); printf("* 2.Decimal to octal *\n"); printf("* 3.Decimal to hexadecimal *\n"); printf("* 4.Reenter decimal numbers *\n"); printf("* 5.sign out *\n"); printf("**************************************\n"); printf("Please select the mode:"); scanf("%d",&a); switch(a) { case 1: DTB(num_d,Stack);break; case 2: DTO(num_d,Stack);break; case 3: DTH(num_d,Stack);break; case 4: printf("Please re-enter:");scanf("%d",&num_d);break; case 5: Stack_Destory(Stack);flag = 0; break; default: printf("This option is not available") ;break; } } } void main() { run(); }