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

C Primer Plus 第五章 编程练习

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

C Primer Plus 第五章 编程练习

前言

C Primer Plus 第六版,作者:史蒂芬.普拉塔   中国工信出版社


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

#include 
#define M_PER_H 60
int main(void)
{
int min;

printf("Please enter the time in minuite,(<=0 quit):n");
while((scanf("%d",&min)==1)&&min>0)
{
printf("It's %d hour and %d minuites.n",min/M_PER_H,min%M_PER_H);
printf("Enter the time in minuite,(<=0 quit):n");
}
printf("Done.n");

return 0;
}

注:while循环的条件语句,先判定是否读取一个整数,然后与0比较,若不是整数,则直接退出循环。

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

#include 
int main(void)
{
int a;
int i=0;

printf("Enter an integer:n");
scanf("%d",&a);
while(i<=10)
{
printf("%d ",a++);
i++;
}

return 0;
}

3.编写一个程序,提示用户输入天数,然后将其转换成周数和天数。例如,用户输入18,则转换 成2周4天。以下面的格式显示结果:

18 days are 2 weeks, 4 days.

通过while循环让用户重复输入天数,当用户输入一个非正值时(如0或-20),循环结束。

#include 
const int D_PER_W =7;
int main(void)
{
int days;

printf("Please enter the days,(<=0 quit):n");
while(scanf("%d",&days)==1&&days>0)
{
printf("%d days are %d weeks,%d days.n",days,days/D_PER_W,days%D_PER_W);
printf("Enter the days,(<=0 quit):n");
}
printf("Done.n");

return 0;
}

注:整数相除得到值为去除小数部分的整数,求模运算则得到余数。

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

Enter a height in centimeters: 182

182.0 cm = 5 feet, 11.7 inches

Enter a height in centimeters (<=0 to quit): 168.7

168.7 cm = 5 feet, 6.4 inches

Enter a height in centimeters (<=0 to quit): 0

bye

#include 
#define INCH_PER_FEET 12
#define CM_PER_INCH 2.54
#define CM_PER_FEET 30.48
int main(void)
{
int feet;
float height,inch;
printf("Enter height in centimeters(<=0 to quit):n");
while(scanf("%f",&height)==1&&height>0)
{
feet=(int)height/CM_PER_FEET;
inch=(height/CM_PER_FEET-feet)*INCH_PER_FEET;
printf("%.1f cm=%d feet,%.1f inchesn",height,feet,inch);
printf("Enter height in centimeters(<=0 to quit):n");
}
printf("byen");

return 0;
}

注:两个浮点数相除得到浮点数,使用强制类型转换去掉小数部分转换成整数,即英尺部分的值。然后再将小数部分换算成英寸。

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

#include 
int main(void)
{
int days,money,count;
money=count=0;

printf("Enter the days(<=0 quit):n");
while(scanf("%d",&days)==1&&days>0)
{
while(count++ 

注:每次打印出计算结果后,改变的变量money、count重新赋值为0,避免影响下一次计算。

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

#include 
int main(void)
{
int days,count,money;
count=money=0;

printf("Enter the days(<=0 quit):n");
while(scanf("%d",&days)==1&&days>0)
{
while(count++ 

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

#include 
void X_3(double a);
int main(void)
{
double x;

printf("Enter a double for compute:n");
while(scanf("%lf",&x)==1)
X_3(x);

return 0;
}
void X_3(double a)
{
printf("%f * %f * %f=%lfn",a,a,a,a*a*a);
}

注:scanf() 输入double类型用%lf转换说明,printf()输出double类型用%f转换说明。

8.编写一个程序,显示求模运算的结果。把用户输入的第1个整数作为求模运算符的第2个运算对象,该数在运算过程中保持不变。用户后面输入的数是第1个运算对象。当用户输入一个非正值 时,程序结束。其输出示例如下:

This program computes moduli.

Enter an integer to serve as the second operand: 256

Now enter the first operand: 438

438 % 256 is 182

Enter next number for first operand (<= 0 to quit): 1234567

1234567 % 256 is 135

Enter next number for first operand (<= 0 to quit): 0

Done

#include 
int main(void)
{
int a,b,c;

printf("This program computes modull.n");
printf("Enter an integer to server as the second operand:");
scanf("%d",&b);
printf("Now enter the first operand:");
while(scanf("%d",&a)==1&&a>0)
{
c=a%b;
printf("%d %% %d is %dn",a,b,c);
printf("Enter next number for first operand(<=0 to quit):");
}
printf("Donen");

return 0;
}

注:本题演示求模运算,即两整数相除取余数,另外打印%字符需用转一系列%%。

9.编写一个程序,要求用户输入一个华氏温度。程序应读取double类型的值作为温度值,并把该 值作为参数传递给一个用户自定义的函数Temperatures()。该函数计算摄氏温度和开氏温度,并以 小数点后面两位数字的精度显示3种温度。要使用不同的温标来表示这3个温度值。下面是华氏温度 转摄氏温度的公式:

       摄氏温度 = 5.0 / 9.0 * (华氏温度 - 32.0)

       开氏温标常用于科学研究,0表示绝对零,代表最低的温度。下面是摄氏温度转开氏温度的公式:

       开氏温度 = 摄氏温度 + 273.16

       Temperatures()函数中用const创建温度转换中使用的变量。在main()函数中使用一个循环让 用户重复输入温度,当用户输入q或其他非数字时,循环结束。scanf()函数返回读取数据的数量, 所以如果读取数字则返回1,如果读取q则不返回1。可以使用==运算符将scanf()的返回值和1作比较,测试两值是否相等。

#include 
void Temperatures(double F);
int main(void)
{
double F;    

printf("Enter temperature in F:n");
while(scanf("%lf",&F)==1)
{
Temperatures(F);
printf("Enter temperature in F:n");
}
printf("Done.n");

return 0;
}
void Temperatures(double F)
{
double C,T;    
const double F_C1 =5.0/9.0;
const double F_C2=32.0;
const double C_T=273.16;

C=F_C1*(F-F_C2);
T=C+C_T;
printf("F tempertures %.2f=C tempertures %.2f=T tempertures %.2fn",F,C,T);
}

注:用const限定符定义的变量的值不能更改!​​​



总结

每个程序笔者亲自调试过,另外加上一点知识点解释,有助于对程序的理解。

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

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

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