十进制的转换
2016-03-25 18:55
375 查看
#include <stdio.h> int main (void) { const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; int convertedNumber[64]; long int numberToConvert; int nextDigit, base, index = 0; // get the number and the base printf ("Number to be converted? "); scanf ("%ld", &numberToConvert); printf ("Base? "); scanf ("%i", &base); // convert to the indicated base do { convertedNumber[index] = numberToConvert % base; ++index; numberToConvert = numberToConvert / base; Character Arrays 111 } while ( numberToConvert != 0 ); // display the results in reverse order printf ("Converted number = "); for (--index; index >= 0; --index ) { nextDigit = convertedNumber[index]; printf ("%c", baseDigits[nextDigit]); } printf ("\n"); return 0; }
Program 7.7 Output
Number to be converted? 10
Base? 2
Converted number = 1010
Program 7.7 Output (Rerun)
Number to be converted? 128362
Base? 16
Converted number = 1F56A
1.取余数,放进数组,但为逆序
2. 创建一个字符数组16位,放进16base所有数字对应的字母
3.逆序输出时,令nextdigit等于数组中的一个余数,对应字符数组的字符输出即可。
4.对base应该有个判断:若base为零则run time error;base=1则进入无限循环;base超过16则要扩展数组16.
相关文章推荐
- 使用 策略模式 控制用户的登录与否的行为
- 递归
- GitHub机器学习项目,你了解多少?
- GitHub深度学习项目,你了解多少?
- 网络流入门—用于最大流的Dinic算法
- 最简单的二进制神经网络
- Git命令使用个人总结
- TOP排行新闻列表。
- 纠正对Fragment Transaction BackStack的误解
- mybatis中mapping文件like查询方式
- Java Learning:并发中的同步锁(synchronized)
- 求整数组里最大子数组的和
- 简单的logback日志收集服务端
- 找到⼀个未知长度单链表的倒数第K个节点
- Struts2 - 常用的constant总结
- 机器学习基础(二十三)—— 概念、定义
- js 里 写两个日期的相差时间
- android学习之intent跳转3引用值得传递
- 【Linux】ubuntu apt-get apt-cache命令 使用
- git pull vs fetch/rebase/merge