栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C PRIMER PLUS第九章第10题进制转换

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C PRIMER PLUS第九章第10题进制转换

编写一个to_base_n()函数接受两个在2-10范围内的参数,然后以第1个参数中制定的进制打印第2个参数的数值。例如,to_base_n(129,8)显示的结果为201,也就是129的八进制数。在一个完整的程序中测试该函数。

以下为代码:

#include 

void to_base_n(int n, int x);
int main(void)
{
	int n, x;
	
	printf("Enter a number and the base for the form of prints.n");
	printf("Now enter the number in the demical system: ");
	scanf("%d", &x);
	printf("Enter the base of it(2-10): ");
	scanf("%d", &n);
	while(n < 2 && n > 10)
	{
		printf("Enter the base(2-10): ");
		scanf("%d", &n);
	}
	to_base_n(n, x); 
	return 0;
 } 
 
 void to_base_n(int n, int x)
 {
 	if(x < 0)
 	{
 		x *= -1;
 		printf("-");
	 }
 	if(x > 0)
 	{
 		to_base_n(n, x / n);
 		printf("%d", x % n);
	 }
 	return;
 }
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738226.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号