Example code for number system conversion in C language data structures

  • 2020-05-17 05:58:17
  • OfStack

Example code for number system conversion in C language data structures

Number system conversion is YanWeiMin data structure of the example in the book, but the examples in the book is written in the form of pseudo code, mostly is not easy to understand and implement, created a lot of for beginners, here we are going to the detailed implementation, so that beginners debugging and running, and to learn from it.


#include <stdlib.h>
#include <stdio.h>
#include<malloc.h>
#define STACK_INIT_SIZE 10  // Defines the size of the memory initially requested  
#define STACK_INCREMENT 2  // every 1 Request the size of the extension when out of memory  
 
#define OVERFLOW 0 
#define FALSE 0 
#define TRUE 1 
#define ERROR 0 
#define INFEASIBLE 0 
#define OK 1 

typedef int SElemType; 
typedef int Status; 

int Length;
typedef struct SqStack{

     int stacksize;
     
     SElemType *top;
  SElemType *base;

}SqStack;


Status InitStack(SqStack &S){
    
if(!(S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof( SElemType))))
    
exit(-1);

S.top = S.base;

S.stacksize = STACK_INIT_SIZE;

return OK;
}

Status Push(SqStack &S , SElemType e){

 if(S.top - S.base >= S.stacksize)
 {
      S.base = (SElemType *)realloc(S.base,(S.stacksize+STACK_INCREMENT) * sizeof( SElemType));
      if(!S.base) 

         exit(OVERFLOW);

   S.top = S.base + S.stacksize;
   S.stacksize += STACK_INCREMENT;
 }

 *S.top++ = e;
 return OK;

}
void OutList(SqStack S ){
    
 S.top = S.base;

 for(int i = 0; i<Length ; i ++){
    printf("%d\t",*(S.top)++);
 
 }
       printf("\n"); //  Line feed after output  

}
Status StackEmpty(SqStack S) {

    if(S.top == S.base)
 {
 return TRUE;
 }else{
 
 return FALSE;
 }

}

Status Pop(SqStack &S,SElemType &e)
{
 if(S.top ==S.base)
 return ERROR;

 e = * --S.top;
 return OK;




}

void conversion() //  algorithm 3.1 

{

 SqStack s; // Order of the stack s 
 
    unsigned n, m; //  Non-negative integer  
    SElemType e; // The stack elements e 
 
    InitStack(s); //  Constructs an empty stack s 
 
    printf(" Please enter the 10 Hexadecimal number n ( >=0 )  = "); 
 
    scanf("%u", &n); //  Enter the nonnegative 10 Hexadecimal integer n 
 
    printf("\n Please enter the base you want to convert to:  "); 
 
    scanf("%u", &m); //  Enter the nonnegative 10 Hexadecimal integer n 
 
    printf("10 Hexadecimal number %u the 8 Hexadecimal number is ", n); 
 
    while (n) //  As long as n Is not equal to 0 Is cycle  
       // from n Entered for the user 10 Starting with the base number, 1 until n Is equal to the 0 So far,  
    { 
 
       Push(s, n % m); // n Divided by the 8 The remainder of (8 Base order ) Into the stack  
       // the n Divided by the 8 The remainder is pushed onto the stack s 
 
       // The remainder of pressing in first is zero 8 The base bit, the remainder of the press in 8 Base high  
       n = n / m; // make n Is equal to the n Divisible by 8 Enter the next cycle  
    } 
 
    // At the end of the loop, n Is equal to the 0 
 
    while (!StackEmpty(s)) //  As long as the stack s It keeps going around and around,  
       // Until the bottom of the stack pops up s So far as empty  
    { 
 
       Pop(s, e); //  Pops up the top element of the stack and assigns a value to it e 
       // Pop up stack in sequence s The top element of the stack is handed over e Back to the  
       // Here's what pops up first 8 Base high, what pops out is 8 Base order  
       printf("%d", e); //  In turn, the output e 
 
    } 
 
    // At the end of the loop, the stack s Is empty  
    printf("\n"); 
 



}

int main(){

 /**********************  Function declaration area  **********************/ 
    Status InitStack(SqStack &S); 
 
    Status Push(SqStack &S, SElemType e); 
 
    void OutList(SqStack S); 
 
    /**********************  Function execution area  **********************/ 
    conversion(); 
 
 
 

 
return 0;
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: