C Primer Plus 6 第五章编程练习

时间:2023-02-14 12:15:47
1.
#include<stdio.h>
#define M 60	//一小时等于60分钟

int main(void)
{
	int hour;	//小时格式
	int min;	//分钟格式
	int a;	//小时分钟格式中的分钟

	printf("Please enter a time(min):");
	scanf("%d", &min);
	while(min>0)
	{
		hour = min / M;	//小时
		a = min % M;	//分钟
		printf("%d min is %d hour %d min.\n", min, hour, a);
		printf("Do you want to exit, enter a time(time<=0).\n");
		scanf("%d", &min);
	}
	printf("Done!\n");
	return 0;
}

C Primer Plus 6 第五章编程练习

2.

//打印包括输入数在内的比输入数大10的所有整数
#include<stdio.h>
int main(void)
{
	int a;	//用户输入数
	int i = 0;	//计数器
	printf("Enter a number(int):");
	scanf("%d", &a);
	printf("%d", a);	//原数

	while(++i<=10)	//打印10次
	{
		a++;
		printf(" %d", a);	//各数间空一格
	}
	printf("\n");	//最后一个数打印之后换行
	return 0;
}

C Primer Plus 6 第五章编程练习

3.

//将天数以"XX周XX天"的格式显示
#include<stdio.h>
#define M 7	//一周=7天

int main(void)
{
	int day;
	int week;
	int left;

	printf("Enter a day number:");
	scanf("%d", &day);

	while(day>0)
	{
		week = day / M;	//周
		left = day % M;	//天
		printf("%d is %d week, %d day.\n", day, week, left);
		printf("you can continue(enter 0 to quit):");
		scanf("%d", &day);
	}
	printf("Done!\n");
	return 0;
}

C Primer Plus 6 第五章编程练习

4.

//将XX厘米用"XX英寸XX英尺"的格式显示
#include<stdio.h>
#define M 30.48	//一英尺=30.48厘米
#define N 2.54	//一英寸=2.54厘米

int main(void)
{
	float cm;
	int feet;	//利用int型进行截断
	float inch;
	float left;	//阶段部分转换成英寸

	printf("Enter your height in centimeters:");
	scanf("%f", &cm);

	while(cm>0)
	{
		feet = cm / M;
		left = cm - (feet * M);
		inch = left / N;
		printf("%.1f cm = %d feet, %.1f inchs\n", cm, feet, inch);
		printf("Enter your height in cmtimeters(<=0 to quit):");
		scanf("%f", &cm);  
	}
	printf("Done!\n");
	return 0;
}

C Primer Plus 6 第五章编程练习

5.

#include <stdio.h>
int main(void)
{
	int count, sum;
	int i;

	count = 0;
	sum = 0;
	printf("Enter the number of your workdays:");
	scanf("%d", &i);

	while(count++<i)
	{
		sum = sum + count;
	}
	printf("sum = %d\n", sum);

	return 0;
}

C Primer Plus 6 第五章编程练习

6.

//计算整数的平方
#include<stdio.h>
int main(void)
{
	int m;
	int n;
	int i;

	m = 0;
	n = 0;

	printf("Enter a number:");
	scanf("%d", &i);

	while(m++<i)
	{
		n = m * m;
		printf("%d:\t%d\n", m, n);
	}
	return 0;
}

C Primer Plus 6 第五章编程练习

7.

//设计一个计算并打印浮点数立方值的函数
#include<stdio.h>
void three(double n);	//函数原型

int main(void)
{
	double m;

	printf("Enter a number(double):");
	scanf("%lf", &m);	//lf(小写)转换说明表明读入double类型的值,Lf则是long double类型!
	three(m);	//函数调用(实参)

	return 0;
}
void three(double n)	//形参
{
	printf("%.2lf:\t%.2lf\n", n, n*n*n);
}

C Primer Plus 6 第五章编程练习

8.

//求模运算
#include<stdio.h>
int main(void)
{
	const int b;	//运算对象2
	int a;	//运算对象1

	printf("This program computes moduli.\n");
	printf("Enter a integer to serve as the second operand:");
	scanf("%d", &b);
	printf("Enter the first operand:");
	scanf("%d", &a);
	printf("%d %% %d is %d\n", a, b, a%b);
	
	while(a>0)
	{
		printf("Enter next number for first operand(<=0 to quit):");
		scanf("%d", &a);
		printf("%d %% %d is %d\n", a, b ,a%b);
		printf("Enter next number for first operand(<=0 to quit):");
		scanf("%d", &a);
	}
	printf("Done!\n");
	return 0;
}

C Primer Plus 6 第五章编程练习

9.

//华氏温度转摄氏温度和开氏温度
#include<stdio.h>
void Temperatures(double x);	//函数原型
int main(void)
{
	double m;	//华氏温度值

	printf("输入一个华氏温度值:");
	while(scanf("%lf", &m)==1)	//scanf()函数返回读取数据的数量,若读取数字则返回1,读取字符则不返回1
	{
		Temperatures(m);
		printf("输入一个华氏温度:");
		scanf("%lf", &m);
	}
	printf("Done!\n");

	return 0;
}
//函数功能:将华氏温度转换为摄氏温度和开氏温度并打印三个值
void Temperatures(double x)
{
	const double a = 5.0;
	const double b = 9.0;
	const double c = 32.0;
	const double d = 273.16;
	double n;	//摄氏温度
	double k;	//开氏温度

	n = a / b * (x - c);	//计算摄氏温度
	k = n + d;	//计算开氏温度

	printf("华氏温度\t摄氏温度\t开氏温度\n");
	printf("%-15.2lf\t%-15.2lf\t%-15.2lf\n", x, n, k);
}
C Primer Plus 6 第五章编程练习