赞
踩
题目:
代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- /*
- * 十六进制转十进制
- * 作者:踏歌行
- * 日期:2018 - 3 - 17
- **/
-
- int main()
- {
- int i;
- int len = 0; //计算SUM长度
- char SUM[50]; //存储结果
- double index = 0; //16的多少次幂
- long long sum = 0; //结果
- int s_n = 0; //计算十六进制数组长度
- double sixteen_1[8];//将输入的字符串数组转换成实数存入其中
- char sixteen[8]; //输入的字符串数组
- scanf("%s",sixteen);
- for(i = 0; i < 8; i++)
- {
- switch(sixteen[i])
- {
- case '0':
- sixteen_1[s_n++] = 0;
- break;
- case '1':
- sixteen_1[s_n++] = 1;
- break;
- case '2':
- sixteen_1[s_n++] = 2;
- break;
- case '3':
- sixteen_1[s_n++] = 3;
- break;
- case '4':
- sixteen_1[s_n++] = 4;
- break;
- case '5':
- sixteen_1[s_n++] = 5;
- break;
- case '6':
- sixteen_1[s_n++] = 6;
- break;
- case '7':
- sixteen_1[s_n++] = 7;
- break;
- case '8':
- sixteen_1[s_n++] = 8;
- break;
- case '9':
- sixteen_1[s_n++] = 9;
- break;
- case 'A':
- sixteen_1[s_n++] = 10;
- break;
- case 'B':
- sixteen_1[s_n++] = 11;
- break;
- case 'C':
- sixteen_1[s_n++] = 12;
- break;
- case 'D':
- sixteen_1[s_n++] = 13;
- break;
- case 'E':
- sixteen_1[s_n++] = 14;
- break;
- case 'F':
- sixteen_1[s_n++] = 15;
- break;
- }
- }
- index = (double)s_n;
- for(i = 0; i < s_n; i++)
- {
- sum += sixteen_1[i] * pow(16,index - 1);
- index--;
- }
- if(sum == 0)
- {
- SUM[len++] = '0';
- }
- else
- {
- for(; sum > 0; )
- {
- SUM[len++] = sum % 10 + '0';
- sum /= 10;
- }
- }
- for(i = len - 1; i >= 0; i--)
- {
- printf("%c",SUM[i]);
- }
- return 0;
- }
运行结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。