zoukankan      html  css  js  c++  java
  • 蓝桥杯基础练习题5(简陋16进制转10进制)

    /*
    问题描述
      从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。
      注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。
    样例输入
    FFFF
    样例输出
    65535
    */
    #include<stdio.h>
    #include<string.h>
    #include <stdlib.h>
    #include <math.h>
    int main(void){
    //    printf("hello world!");
    
        int NUM_HEX = 1;        //16进制数个数 
        int DEC = 0;        //10进制数个数 
        
        
        int tag;    //循环标志 
        int num_tag;
        //int BIN_TAG=0;    //整体二进制码输入标志 
        int bin_tag=0;    //单个二进制码输入标志 
        int tempInt;        //临时变量 
        
        
        char Hex[20];        //定义一个长度为20的数组,为之后的操作做准备
        char Oct[80];        //16进制数二进制码的长度/4*3就是8进制数二进制码的长度 
        char Bin0[90];
        char Bin1[90];
        char Bin2[90];
        char Bin3[90];
        char Bin4[90];
        char Bin5[90];
        char Bin6[90];
        char Bin7[90];
        char Bin8[90];
        char Bin9[90];
        memset( Bin0, '', sizeof(Bin0) );
        memset( Bin1, '', sizeof(Bin1) );
        memset( Bin2, '', sizeof(Bin2) );
        memset( Bin3, '', sizeof(Bin3) );
        memset( Bin4, '', sizeof(Bin4) );
        memset( Bin5, '', sizeof(Bin5) );
        memset( Bin6, '', sizeof(Bin6) );
        memset( Bin7, '', sizeof(Bin7) );
        memset( Bin8, '', sizeof(Bin8) );
        memset( Bin9, '', sizeof(Bin9) );
        
        char *BIN_CODE;    //根据BIN_TAG确定使用哪一组二进制数组 
        BIN_CODE=Bin0;
        
        div_t div_temp;    
        
        //scanf("%d", &NUM_HEX);    //确定输入正整数的个数 
        //NUM_OCT = NUM_HEX;         //8进制数个数与16进制数相同 
        
        for(tag=0; tag<20; ++tag){
            Hex[tag]= '0';        //全部初始设置为0,防止不确定数据生成 
        } 
        for(tag=0; tag<80; ++tag){
            Oct[tag]= '0';        //全部初始设置为0,防止不确定数据生成 
        }
        
        /***TODO 计划每转换一组数据就将数据存储在Oct数组中,然后重新初始化
        Hex数组和Oct数组,再对Hex数组反复操作 将得到的数据分别存储在Binn数组中。。
         最后输出 
        
        */
        
        
        for(tag=0; tag<NUM_HEX; ++tag){
            fflush(stdin);        //清除缓存区 
            gets(Hex);            //输入NUM_HEX个16进制数 
            //printf("%d",strlen(Hex));
            
            if(tag==0)    BIN_CODE=Bin0;
            if(tag==1)    BIN_CODE=Bin1;
            if(tag==2)    BIN_CODE=Bin2;
            if(tag==3)    BIN_CODE=Bin3;
            if(tag==4)    BIN_CODE=Bin4;
            if(tag==5)    BIN_CODE=Bin5;
            if(tag==6)    BIN_CODE=Bin6;
            if(tag==7)    BIN_CODE=Bin7;
            if(tag==8)    BIN_CODE=Bin8;
            if(tag==9)    BIN_CODE=Bin9;
            for(num_tag=0; num_tag<strlen(Hex); ++num_tag){
                switch(Hex[num_tag]) {
                    case '0':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '1':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '2':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '3':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '4':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '5':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '6':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '7':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='0';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '8':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case '9':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case 'A':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case 'B':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='0';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case 'C':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case 'D':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='0';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case 'E':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='0';
                        BIN_CODE[tempInt+4]='';
                        break;
                    case 'F':
                        tempInt=num_tag*4;
                        BIN_CODE[tempInt]='1';
                        BIN_CODE[tempInt+1]='1';
                        BIN_CODE[tempInt+2]='1';
                        BIN_CODE[tempInt+3]='1';
                        BIN_CODE[tempInt+4]='';
                        break;
                    
                    default:    printf("error input
    ");
                }
                //++bin_tag;
            }
            //BIN_TAG++;        //每输入一次改变一次保存二进制数组的位置
        } 
        /* 
        gets(Hex);
        printf("%d",strlen(Hex));
        */ 
        //puts(Bin0);
        /*
        for(tag = 0; tag < NUM_HEX; ++tag){
            if(tag==0)    BIN_CODE=Bin0;
            if(tag==1)    BIN_CODE=Bin1;
            if(tag==2)    BIN_CODE=Bin2;
            if(tag==3)    BIN_CODE=Bin3;
            if(tag==4)    BIN_CODE=Bin4;
            if(tag==5)    BIN_CODE=Bin5;
            if(tag==6)    BIN_CODE=Bin6;
            if(tag==7)    BIN_CODE=Bin7;
            if(tag==8)    BIN_CODE=Bin8;
            if(tag==9)    BIN_CODE=Bin9;
            
            strcpy(Oct, BIN_CODE);
            //TEST
            puts(Bin0);
        } 
        */
        /*
        strcpy(Oct,  Bin0);
        puts(Oct);
        */
    
        
        
        /*******TODO
        一个16进制数转成的2进制必然是4的整数倍
    转换成8进制的对应代码需要为3的整数倍
    如何确定开头?
    如何在开头正确地添0并将每一位正确地后移?
        为防止数据覆盖,先从最后一位开始后移,第一位移动时,在原来的位置覆盖‘0’.
    怎么确定移动的个数呢?
        如果生成的二进制码能够被3整除,则不需要移动,如果不能,则需要后移的个数为3-(整个数组长除以3的余数)
        */ 
        
        
        for(tag=0; tag<NUM_HEX; ++tag){
            if(tag==0)    BIN_CODE=Bin0;
            if(tag==1)    BIN_CODE=Bin1;
            if(tag==2)    BIN_CODE=Bin2;
            if(tag==3)    BIN_CODE=Bin3;
            if(tag==4)    BIN_CODE=Bin4;
            if(tag==5)    BIN_CODE=Bin5;
            if(tag==6)    BIN_CODE=Bin6;
            if(tag==7)    BIN_CODE=Bin7;
            if(tag==8)    BIN_CODE=Bin8;
            if(tag==9)    BIN_CODE=Bin9;
            
            //printf("%d
    ", strlen(BIN_CODE));
            div_temp = div(strlen(BIN_CODE), 3);
            tempInt = 3 - div_temp.rem;                //需要后移的位数
            //printf("
    the number needs to put back is: %d
    ", tempInt);
            
            //printf("BEFORE the length of BIN_CODE is: %d
    ", strlen(BIN_CODE));
            for(num_tag = strlen(BIN_CODE)-1; num_tag >= 0; --num_tag) {    //循环后移 
                *(BIN_CODE+num_tag+tempInt) = *(BIN_CODE+num_tag);
                
            }
            //printf("NOW the length of BIN_CODE is: %d
    ", strlen(BIN_CODE));
            
            //前置0 
            for(num_tag=0; num_tag < tempInt; ++num_tag){
                *(BIN_CODE+num_tag) = '0';
            }
            //puts(BIN_CODE);
            
            
        }
        
        double RA = 0;
        double realInt = 0.00;
        for(num_tag=0; num_tag<strlen(BIN_CODE); num_tag++) {
                //printf("%d", num_tag); 
                /** 
                if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '0'){}
                if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '1'){printf("1");}
                if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '0'){printf("2");}
                if(*(BIN_CODE+num_tag) == '0' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '1'){printf("3");}
                if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '0'){printf("4");}
                if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '0' && *(BIN_CODE+num_tag+2) == '1'){printf("5");}
                if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '0'){printf("6");}
                if(*(BIN_CODE+num_tag) == '1' && *(BIN_CODE+num_tag+1) == '1' && *(BIN_CODE+num_tag+2) == '1'){printf("7");}
                */
            realInt = strlen(BIN_CODE) - num_tag - 1;
            
            //RA += pow(2, realInt);
            if(realInt != 0 && *(BIN_CODE+num_tag) == '1'){
                RA += pow(2, realInt);
            }
            //printf("R%f
    ", RA);
        }
        //printf("
    %d
    ", num_tag);
        if(*(BIN_CODE+num_tag-1) == '1') RA++;
        printf("%.0f", RA);
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        /*
        puts(Bin0);
        puts(Bin1);
        puts(Bin2);
        puts(Bin3);
        puts(Bin4);
        puts(Bin5);
        puts(Bin6);
        puts(Bin7);
        puts(Bin8);
        puts(Bin9);
        */
        
        
        return 0;
    } 

    从之前写的16转8稍微改了一下。。

  • 相关阅读:
    cadence中画焊盘注意事项
    频率带宽解释
    一种RC滤波电路的验证
    24L01-2.4G无线传输模块调节记录
    51中xdata,idata,data,pdata的区别
    调试24L01经验总结
    将scl分频
    I2C详细介绍
    汽车电源系统概述
    PCB命名规则-allegro
  • 原文地址:https://www.cnblogs.com/jzl123/p/6337660.html
Copyright © 2011-2022 走看看