前言

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


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

#include <stdio.h>
#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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
#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 inches\n",height,feet,inch);
printf("Enter height in centimeters(<=0 to quit):\n");
}
printf("bye\n");return 0;
}

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

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

#include <stdio.h>
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++<days)
money+=count;
printf("Get money %d $ in %d days.\n",money,days);
money=0;
count=0;
printf("Enter the days(<=0 quit):\n");
}
printf("Done.\n");return 0;
}

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

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

#include <stdio.h>
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++<days)
money+=count*count;
printf("Get money %d $ in %d days.\n",money,days);
count=0;
money=0;
printf("Enter the days(<=0 quit):\n");
}
printf("Done.\n");return 0;
}

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

#include <stdio.h>
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=%lf\n",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 <stdio.h>
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 %d\n",a,b,c);
printf("Enter next number for first operand(<=0 to quit):");
}
printf("Done\n");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 <stdio.h>
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;    /*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 %.2f\n",F,C,T);
}

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


总结

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

C Primer Plus 第五章 编程练习相关推荐

  1. c primer plus 第五章编程练习

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 目录 文章目录 前言 ##1.编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创 ...

  2. C++ Primer Plus 第七章编程题练习

    C++ Primer Plus 第七章编程题练习 第一题 题目描述 编写一个程序,不断要求用户输入两个数,直到其中的一个为0.对于每两个数,程序将使用一个 函数来计算它们的调和平均数,并将结果返回给m ...

  3. c++primer plus 第13章 编程题第2题

    c++primer plus 第13章 编程题第2题 #pragma once #ifndef CD_H_ #define CD_H_ //base classclass Cd { private:c ...

  4. c++primer plus 第11章 编程题第7题

    c++primer plus 第11章 编程题第7题 #pragma once #ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include<iostream ...

  5. C Primer Plus 第五章 复习题编程练习 答案

    第五章 复习题&编程练习 复习题 1. 假设所有变量的类型都是int,下列各项变量的值是多少: 2. 假设所有变量的类型都是int,下列各项变量的值是多少: 3. 对下列各表达式求值: 4. ...

  6. C++primer plus第四章编程练习自编程序

    //第四章编程练习 //eg.1显示信息 #include <iostream> using namespace std; const int SIZE = 20; int main() ...

  7. 《C Primer Plus》第五章 编程习题

    目录 5.11.1 5.11.2 5.11.3 5.11.4 5.11.5 5.11.6 5.11.7 5.11.8 5.11.9 5.11.1 编写一个程序,把分钟表示的时间转换成小时和分钟表示的时 ...

  8. (八十)第五章编程练习

    1.编写一个要求用户输入两个整数的程序.该程序将计算并输出这两个整数之间(包括这两个整数)的所有整数的和.这里假设先输入较小的整数.例如,如果用户输入的是2和9,则程序指出2-9之间的所有整数和为44 ...

  9. C++Primer plus第五章知识点+习题答案

    目录 一.for循环 二.while循环 三.do while 循环 四.基于范围的for循环 五.循环和文本输出 六.嵌套循环和二维数组 一.for循环 1.1 for循环结构体的组成部分 for ...

最新文章

  1. qq传java文件_java学习:基于TCP的网络文件传输(可改写的QQ文件传输)
  2. 二进制异或--7.18待完善
  3. Android4.4 ContentResolver查询图片无效 及 图库删除 添加图片后,ContentResolver不更新的问题解决...
  4. 有问有答 | Storm技术内幕与实践精华问答
  5. 大数据技术之kafka (第 3 章 Kafka 架构深入) 分区策略在分析
  6. php计算对角线,python计算对角线有理函数插值的方法
  7. bat怎么发起网络请求_因为一个跨域请求,我差点丢了饭碗
  8. IE下javascript cookie path
  9. Python+Appium+夜神模拟器安装与简单运行(2/2)
  10. python发送邮件程序
  11. sql server 如何在一个数据库中操作另一个数据库中的数据
  12. 三个优秀的Android图表开源控件
  13. Matthew Dean Schwartz《Quantum Field Theory and The Standard Model》(马修·迪恩·施瓦茨《量子场论与标准模型》)中文目录
  14. 【转】丹尼斯·里奇,那个给乔布斯提供肩膀的巨人
  15. Win10版本怎么转换?小编教你一键切换Windows版本
  16. 教你做一个最简版的倒计时计时器,新手也能秒懂。
  17. 阿里码农自述:老婆失业了,周围同事也不断被裁
  18. 如何用matlab画一个球
  19. 61. 请简述self在类中的意义?
  20. 华硕电脑连接不上wifi_华硕电脑连不上无线网怎么办

热门文章

  1. java毕业设计——基于java+JSP+sqlserver的智能在线考试信息管理系统设计与实现(毕业论文+程序源码)——智能在线考试信息管理系统
  2. Bluetooth Profile Specification之1.1 A2DP 之Audio Codec(音频编解码器)-SBC
  3. Kaggle: ImageNet Dog Breed Classification (Pytorch)
  4. 解决 error: called object ‘xxx‘ is not a function or function pointer
  5. 盒子科技笔试Java_丰巢科技 Java高级笔试面试题 PDF 下载
  6. 去哪儿网——项目管理平台助力研发效率提升
  7. 爱招飞软件开发工具与 Arduino 与 ESP32 的关系
  8. [装机版]小路工作室GhostXP 2008贺岁版
  9. yolov5 + second_classify -- 代码
  10. android记事本的设计报告,安卓记事本开发设计报告.pdf