Deepen understanding and hex conversion function

One. Program run run shot

  1. two. Function Introduction

1.main

  1. The original binary digital input, using local variables primitivesystem 2. enter the number, using a local variable number hexadecimal 3. want to get input using local variables System 4. binary conversion, the calling function binaryconversion and Change ,. 5. Is obtained Objective number

  2. #include "stdio.h" 
    int decimalconversion(int number,int primitivesystem,int system);//转十进制函数 
    int binaryconversion(int number);//转二进制函数 
    void change(int number,int primitivesystem,int system);//进制相互转换函数 
    int judge(int number,int primitivesystem);//除十六进制外,判断非法函数 
    void input(int number,int primitivesystem);//输入函数 
    int main(void) {
     printf("请输入将输入数为什么进制数\n");
     int primitivesystem;
     scanf("%d",&primitivesystem);
     int number=0;
     int flag=1;
     printf("请输入该数字\n");
     char hexad[100];
     if(primitivesystem!=16){
     scanf("%d",&number);
        }
        else{
         scanf("%s",&hexad);
         for(int i=0;hexad[i]!='\0';i++){
             if(hexad[i]>='0'&&hexad[i]<='9'){
                 number=number*16+hexad[i]-'0';
             }
             else if(hexad[i]>='A'&&hexad[i]<='F'){
                 number=number*16+hexad[i]-'A'+10;
             }
             else if(hexad[i]>='a'&&hexad[i]<='f'){
                 number=number*16+hexad[i]-'a'+10;
             }// 将十六进制字符串转为十进制 
             else{
                 printf("该数字非法,请重新输入");
                 flag=0;
                 break; 
             }//判断十六进制输入是否非法 
         }
         if(flag==1){
             primitivesystem=10;
             input(number,primitivesystem);
        }
     }
     int remainder=0,copy=number;
     if(primitivesystem==2){
         judge(copy,primitivesystem);//调用判断非法函数 
         if(judge(copy,primitivesystem)){
             input(number,primitivesystem);//调用输入函数 
         }
     }
     else if(primitivesystem==8){
         judge(copy,primitivesystem);//调用判断非法函数
         if(judge(copy,primitivesystem)){
             input(number,primitivesystem);//调用输入函数
         }
        } 
     return 0;
    }

2.binaryconversion (converted to binary)

  1. Recursive incoming number into a binary number, divided into two cases, the number / 2 == 0 return value can be calculated using recursive results

  2. int binaryconversion(int number){
     if(number/2==0){
         return number%2;
     }
     else{
         return number%2+binaryconversion(number/2)*10;
     }
    }

3.change (each hex conversion)

  1. Carry out binary conversion, mainly through the decimal conversion of other binary to decimal, hexadecimal conversion and other ideas, incoming number, primitivesystem, the value system of

  2. void change(int number,int primitivesystem,int system)//进制相互转换函数{
        int sum=0;
        int product=1;
     if(primitivesystem==10)//数原进制为十进制{
         if(system==2){
             binaryconversion(number);
             printf("%d\n",binaryconversion(number));
     }
         else if(system==8){
             printf("%o\n",number);
         }
         else if(system==16){
             printf("%x\n",number);
         } 
         else{
             printf("%d\n",number);//因为若为十六进制数输入,已将转为十进制数 ,再进行函数转换 
         }
     }
     else if(primitivesystem==8)//数原进制为八进制{
         if(system==2){
            sum=decimalconversion(number,primitivesystem,system);
            binaryconversion(sum);
            printf("%d\n",binaryconversion(sum));
         }
         else if(system==10){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%d\n",sum);  
         }
         else if(system==16){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%x\n",sum);
         }
     }
     else if(primitivesystem==2)//数原进制为二进制{
         if(system==8){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%o\n",sum);
         }
         else if(system==10){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%d\n",sum);
         }
         else if(system==16){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%x\n",sum);
         }
     }
    }

4. judge (determined invalid entry)

  1. In addition to illegally enter the value judgment hex away, the incoming number and primitivesystem

  2. int judge(int copy,int primitivesystem)//判断非法函数{
     int remainder=0;
     int flag=1;
     do{
         remainder=copy%10;
         copy=copy/10;
         if(remainder>primitivesystem-1){
             printf("该数字非法,请重新输入\n");
             flag=0; 
             break;
         }
         }while(copy);
         return flag;
    }

5.input (input function)

  1. Number of inputs and outputs of the object system

  2. void input(int number,int primitivesystem)//输入函数{
     printf("请输入你想得到该数的什么进制数\n");
     int system; 
     scanf("%d",&system);
     printf("你将得到你想要的结果\n"); 
     change(number,primitivesystem,system);
     printf("感谢使用\n");
    }

6.decimalconversion (converted to decimal)

  1. Non-hexadecimal to decimal numbers, pass number, value primitivesystem, system of

  2. int decimalconversion(int number,int primitivesystem,int system)//转为十进制{
     int sum=0;
     int product=1;
     do{
         sum=sum+(number%10)*product;
         number=number/10;
         product=product*primitivesystem;
        }while(number);
        return sum;
    }

three. Mind mapping function

  1. The complete code

  2. #include "stdio.h" 
    int decimalconversion(int number,int primitivesystem,int system);//转十进制函数 
    int binaryconversion(int number);//转二进制函数 
    void change(int number,int primitivesystem,int system);//进制相互转换函数 
    int judge(int number,int primitivesystem);//除十六进制外,判断非法函数 
    void input(int number,int primitivesystem);//输入函数 
    int main(void) {
     printf("请输入将输入数为什么进制数\n");
     int primitivesystem;
     scanf("%d",&primitivesystem);
     int number=0;
     int flag=1;
     printf("请输入该数字\n");
     char hexad[100];
     if(primitivesystem!=16){
     scanf("%d",&number);
        }
        else{
         scanf("%s",&hexad);
         for(int i=0;hexad[i]!='\0';i++){
             if(hexad[i]>='0'&&hexad[i]<='9'){
                 number=number*16+hexad[i]-'0';
             }
             else if(hexad[i]>='A'&&hexad[i]<='F'){
                 number=number*16+hexad[i]-'A'+10;
             }
             else if(hexad[i]>='a'&&hexad[i]<='f'){
                 number=number*16+hexad[i]-'a'+10;
             }// 将十六进制字符串转为十进制 
             else{
                 printf("该数字非法,请重新输入");
                 flag=0;
                 break; 
             }//判断十六进制输入是否非法 
         }
         if(flag==1){
             primitivesystem=10;
             input(number,primitivesystem);
         }
     }
     int remainder=0,copy=number;
     if(primitivesystem==2){
         judge(copy,primitivesystem);//调用判断非法函数 
         if(judge(copy,primitivesystem)){
             input(number,primitivesystem);//调用输入函数 
         }
     }
     else if(primitivesystem==8){
         judge(copy,primitivesystem);//调用判断非法函数
         if(judge(copy,primitivesystem)){
             input(number,primitivesystem);//调用输入函数
         }
        } 
     return 0;
    }
    void change(int number,int primitivesystem,int system)//进制相互转换函数{
        int sum=0;
        int product=1;
     if(primitivesystem==10)//数原进制为十进制{
         if(system==2){
             binaryconversion(number);
             printf("%d\n",binaryconversion(number));
     }
         else if(system==8){
             printf("%o\n",number);
         }
         else if(system==16){
             printf("%x\n",number);
         } 
         else{
             printf("%d\n",number);//因为若为十六进制数输入,已将转为十进制数 ,再进行函数转换 
         }
     }
     else if(primitivesystem==8)//数原进制为八进制{
         if(system==2){
            sum=decimalconversion(number,primitivesystem,system);
            binaryconversion(sum);
            printf("%d\n",binaryconversion(sum));
         }
         else if(system==10){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%d\n",sum);  
         }
         else if(system==16){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%x\n",sum);
         }
     }
     else if(primitivesystem==2)//数原进制为二进制{
         if(system==8){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%o\n",sum);
         }
         else if(system==10){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%d\n",sum);
         }
         else if(system==16){
            sum=decimalconversion(number,primitivesystem,system);
            printf("%x\n",sum);
         }
     }
    }
    int binaryconversion(int number)//二进制转换函数{
     if(number/2==0){
         return number%2;
     }
     else{
         return number%2+binaryconversion(number/2)*10;
     }
    }
    int decimalconversion(int number,int primitivesystem,int system)//十进制转换函数{
     int sum=0;
     int product=1;
     do{
         sum=sum+(number%10)*product;
         number=number/10;
         product=product*primitivesystem;
        }while(number);
        return sum;
    }
    void input(int number,int primitivesystem)//输入函数{
     printf("请输入你想得到该数的什么进制数\n");
     int system; 
     scanf("%d",&system);
     printf("你将得到你想要的结果\n"); 
     change(number,primitivesystem,system);
     printf("感谢使用\n");
    }
    int judge(int copy,int primitivesystem)//判断非法函数{
    int remainder=0;
     int flag=1;
     do{
         remainder=copy%10;
         copy=copy/10;
         if(remainder>primitivesystem-1){
             printf("该数字非法,请重新输入\n");
             flag=0; 
             break;
         }
       }while(copy);
       return flag;
    }
    

four. Problems encountered

  1. Hex Input: not with the other binary inputs, through the understanding of the string, using% s input

  2. For illegal judgments figures, part of the code that appears prejudice other code to run, continue to set breakpoints and debug

  3. Forget the value of the initialization number, leading to a hexadecimal number base conversion disorder

Fives. Code peer assessment (Tao network 3 classes)

  1. Enter, he directly enter the number of strings, avoiding my input Points

  2. int Tennum(char a[],int b)
    {
     int len, i, num;
     int sum = 0;
     len = strlen(a);
     for (i = 0; i < len; i++) 
     {
         if (a[i] >= '0' && a[i] <= '9')
         {
             num = a[i] - '0';
         }
         else if (a[i] >= 'A' && a[i] <= 'F')
         {
             num = a[i] - 'A' + 10;
         }
         sum = sum * b + num;
     }
     return sum;
    }
  3. His Numchange function, use the decimal pow to turn each band, my decimalconversion function through the cycle, turn the achievement of binary decimal, Decimal and then turn each band

  4. void Numchange(int m, int b)
    {
    
     int n = m;
     int count = 0;
     if (m == 0) printf("0");
     while (n != 0)
     {
         n = n / b;
         count++;
     }
     int number;
     for (int i = count; i >= 1; i--)
     {
         number = m / (int)pow(b, i - 1);
         if (number < 10) {
             printf("%d", number);
         }
         else {
             printf("%c", number + 55);
         }
         m = m % (int)pow(b, i - 1);
     }
    }

six. to sum up

  1. I base for the conversion deeper understanding of each hexadecimal to decimal has a universal law, and converted to decimal hex is relatively easy for other
  2. By passing arguments to a function parameter, you can make the code more concise, the content more readable, while functions can call each other, you can use one function, and ultimately more complex to use.

Guess you like

Origin www.cnblogs.com/tylk/p/11815722.html