c primer plus 第五章编程练习

这篇博客包含一系列C语言编程练习,涉及时间分钟转小时分钟、打印指定范围内整数、天数转周数和天数、身高厘米和英寸转换、按用户输入天数累计收入计算、整数平方和、立方计算、模运算显示及华氏温度转换为摄氏和开氏温度。每个练习都有详细的代码实现,适合初学者巩固C语言基础知识。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

目录

文章目录

前言

##1、编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间。使用#define或const创建一个表示60的符号常量或const变量。通过while循环让用户重复输入值,直到用户输入小于或等于0的值才停止循环。

##2、编写一个程序,提示用户输入一个整数,然后打印从该数到比该数大10的所有整数(例如,用户输入5,则打印5~15的所有整数,包括5和15)。要求打印的各值之间用一个空格、制表符或换行符分开。

##3、编写一个程序,提示用户输入天数,然后将其转换成周数和天数。例如,用户输入18,则转换成2周4天。以下面的格式显示结果:18 days are 2 weeks,4days.通过while循环让用户重复输入天数,当用户输入一个非正值时(如0或-20),循环结束。

##4、编写一个程序,提示用户输入一个身高(单位:厘米),并分别以厘米和英寸为单位显示该值,允许有小数部分。程序应该能让用户重复输入身高,直到用户输入一个非正值。其输出示例如下:Enter a height in centimeters:182

182.0 cm = 5 feet,11.7 inchesEnter a height in centimeters(<=0 toquit):168.7

168.0cm = 5 feet,64inchesEnter a height in centimeters(<=0 toquit):0

bye

##5.修改程序addemupc(程序清单5.13),你可以认为addemupc是计算20天里赚多少钱的程序(假设第1天赚$1、第2天赚$2、第3天赚$3,以此类推)。修改程序,使其可以与用户交互,根据用户输入的数进行计算(即,用读入的一个变量来代替 20)。 

## 6、修改编程练习5的程序,使其能计算整数的平方和(可以认为第1天赚$1、第2天赚$4、第3天赚$9,以此类推,这看起来很不错)。C没有平方函数,但是可以用n*n来表示n的平方。

## 7.编写一个程序,提示用户输入一个double类型的数,并打印该数的立方值。自己设计一个函数计算并打印立方值。main()函数要把用户输入的值传递给该函数。

##8、编写一个程序,显示求模运算的结果。把用户输入的第1个整数作为求模运算符的第2个运算对象,该数在运算过程中保持不变。用户后面输入的数是第1个运算对象。当用户输入一个非正值时,程序结束。其输出示例如下:This program computes moduli.Enter an integer to serve as the second operand:256 Now enter the first operand:438438 8 256 is182Enter next number for first operand(<=0 to quit): 12345671234567 8256 is 135Enter next number for first operand(<=0 toquit):0 Done 

##9编写一个程序,要求用户输入一个华氏温度。程序应读取double类型的值作为温度值,并把该值作为参数传递给一个用户自定义的函数Temperatures()。该函数计算摄氏温度和开氏温度,并以小数点后面两位数字的精度显示3种温度。要使用不同的温标来表示这3个温度值。下面是华氏温度转摄氏温度的公式:摄氏温度=5.0/9.0*(华氏温度-32.0)开氏温标常用于科学研究,0表示绝对零,代表最低的温度。下面是摄氏温度转开氏温度的公式开氏温度=摄氏温度+273.16Temperatures()函数中用const创建温度转换中使用的变量。在main()函数中使用一个循环,让用户重复输入温度,当用户输入q或其他非数字时,循环结束。scanf()函数返回读取数据的数量,所以如果读取数字则返回1,如果读取a则不返回1。可以使用==运算符将scanf()的返回值和1作比较,测试两值是否相等。



前言

大一的编程练习,主要针对c primer plus 第五章的编程练习

##1、编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间。使用#define或const创建一个表示60的符号常量或const变量。通过while循环让用户重复输入值,直到用户输入小于或等于0的值才停止循环。

#include <stdio.h>
#define M_PRE_H 60
int main()
{
	int min , hour,left;
	printf("The program can converts minutes to hours and minutes.\n");
	printf("Now enter the number of minutes.\n");
	printf("And enter a number that is 0 or negative to end.\n");
	scanf("%d",&min);
	while(min>0)
	{
		hour = min / M_PRE_H;
		left = min % M_PRE_H;
		printf("%d minutes is %d hour(s) and %d minute(s).\n",min,hour,left);
		scanf("%d",&min);
	}
	return 0;
}

##2、编写一个程序,提示用户输入一个整数,然后打印从该数到比该数大10的所有整数(例如,用户输入5,则打印5~15的所有整数,包括5和15)。要求打印的各值之间用一个空格、制表符或换行符分开。

#include <stdio.h>
int main()
{
	int num , condition;
	printf("Please enter a number.\n");
	scanf("%d",&num);
	condition = num +10;
	while(num<=condition)
	{
		printf("%d ",num);
		num++;
	}
	return 0;
}

##3、编写一个程序,提示用户输入天数,然后将其转换成周数和天数。例如,用户输入18,则转换成2周4天。以下面的格式显示结果:
18 days are 2 weeks,4days.通过while循环让用户重复输入天数,当用户输入一个非正值时(如0或-20),循环结束。

#include <stdio.h>
const int D_TO_M = 7;
int main()
{
	int day , week , left;
	printf("Please enter the number of days.\n");
	printf("enter a number that is 0 or negative to end.\n");
	scanf("%d",&day);
	while(day>0)
	{
		week = day / D_TO_M;
		left = day % D_TO_M;
		printf("%d day(s) is %d week(s) and %d day(s).\n",day,week,left);
		scanf("%d",&day);
	}
	return 0;
	
}

##4、编写一个程序,提示用户输入一个身高(单位:厘米),并分别以厘米和英寸为单位显示该值,允许有小数部分。程序应该能让用户重复输入身高,直到用户输入一个非正值。其输出示例如下:
Enter a height in centimeters:182

182.0 cm = 5 feet,11.7 inches
Enter a height in centimeters(<=0 toquit):168.7

168.0cm = 5 feet,64inches
Enter a height in centimeters(<=0 toquit):0

bye

#include <stdio.h>
#define C_TO_F 30.48
#define C_TO_I 2.54
int main()
{
	float  cmh , inch;
	int feet;
	printf("Enter a height in centimeters:");
	scanf("%f",&cmh);
	while(cmh>0)
	{
		feet = cmh / C_TO_F;
		inch = (cmh - feet * C_TO_F)/C_TO_I;
		printf("%.1f cm = %d feet, %.1f inches\n",cmh,feet,inch);
		printf("Enter a height in centimeters (<= to quit):");
		scanf("%f",&cmh);
	}
	printf("bye\n");	
}

##5.修改程序addemupc(程序清单5.13),你可以认为addemupc是计算20天里赚多少钱的程序(假设第1天赚$1、第2天赚$2、第3天赚$3,以此类推)。修改程序,使其可以与用户交互,根据用户输入的数进行计算(即,用读入的一个变量来代替 20)。 

#include <stdio.h>
int main()
{
	int day = 0;
	int money , condition;
	int sum = 0;
	printf("It's a program that calculates");
	printf("how much money you've made in some days.\n");
	printf("How many days do you want to calculate?\n");
	scanf("%d",&condition);
	while(day++<condition)
	{
		printf("Please enter the number of money you earn on the %d day.\n",day);
		scanf("%d",&money);
		sum = sum + money;
		printf("You earn $%d on the %d day.\n",money,day);
	}
	printf("You earn $%d in %d days!\n",sum,condition);
}

## 6、修改编程练习5的程序,使其能计算整数的平方和(可以认为第1天赚$1、第2天赚$4、第3天赚$9,以此类推,这看起来很不错)。C没有平方函数,但是可以用n*n来表示n的平方。

#include <stdio.h>
int main()
{
	int n = 0;
	int square;
	while(n++>=0)
	{
		square = n * n;
		printf("%d's square is %d.\n",n,square);
	}
}

## 7.编写一个程序,提示用户输入一个double类型的数,并打印该数的立方值。自己设计一个函数计算并打印立方值。main()函数要把用户输入的值传递给该函数。

#include <stdio.h>
void cube(double n);
int main()
{
	double num;
	printf("Please enter a number that is in double type.\n");
	scanf("%lf",&num);
	cube(num);
}
void cube(double n)
{
	double cube;
	cube = n * n * n;
	printf("%lf's cube is %lf.\n",n,cube);	
}

##8、编写一个程序,显示求模运算的结果。把用户输入的第1个整数作为求模运算符的第2个运算对象,
该数在运算过程中保持不变。用户后面输入的数是第1个运算对象。当用户输入一个非正值时,程序结束。其输出示例如下:
This program computes moduli.
Enter an integer to serve as the second operand:256 Now enter the first operand:438438 8 256 is182
Enter next number for first operand(<=0 to quit): 12345671234567 8256 is 135
Enter next number for first operand(<=0 toquit):0 Done 

#include <stdio.h>
int main()
{
	int secondn , firstn , mo;
	printf("This program computes moduli.\n");
	printf("Enter an integer to serve as the second operand:");
	scanf("%d",&secondn);
	printf("Now enter the first operand:");
	scanf("%d",&firstn);
	while(firstn>0)
	{
		mo = firstn % secondn;
		printf("%d %% %d is %d\n",firstn,secondn,mo);
		printf("Enter next number for first oprand(<=0 to quit):");
		scanf("%d",&firstn);
	}
	printf("Done");
}

 

##9编写一个程序,要求用户输入一个华氏温度。程序应读取double类型的值作为温度值,并把该值
作为参数传递给一个用户自定义的函数Temperatures()。该函数计算摄氏温度和开氏温度,并以小数点后面两位数字的精度显示3种温度。要使用不同的温标来表示这3个温度值。下面是华氏温度转摄氏温度的公式:
摄氏温度=5.0/9.0*(华氏温度-32.0)
开氏温标常用于科学研究,0表示绝对零,代表最低的温度。下面是摄氏温度转开氏温度的公式
开氏温度=摄氏温度+273.16
Temperatures()函数中用const创建温度转换中使用的变量。在main()函数中使用一个循环,让用户重复输入温度,当用户输入q或其他非数字时,循环结束。scanf()函数返回读取数据的数量,所以如果读取数字则返回1,如果读取a则不返回1。可以使用==运算符将scanf()的返回值和1作比较,测试两值是否相等。

#include <stdio.h>
void Temperatures(double n);
int main()
{
    double tem;
    int num;
	printf("Please enter the temperature in Fahrenheit.\n");
	num = scanf("%lf",&tem);
	printf("Enter q or other symbols that are not number to quit.\n");
	while(num == 1)
	{
		Temperatures(tem);
		printf("Please enter the next temperature in Fahrenheit.\n");
		num = scanf("%lf",&tem);
	}
	return 0;
 } 
 void Temperatures(double n)
 {
 	const double F_TO_K = 273.16;
 	double temc,temk;
 	temc = 5.0 / 9.0 * (n - 32.0);
 	temk = n + F_TO_K;
 	printf("%.2lf 摄氏度 = %.2lf 开式温度\n",n,temc);
 	printf("%.2lf 摄氏度 = %.2lf 开式温度\n",n,temk);
 }

若有错误还望指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值