C语言详解第五版答案_第1页
C语言详解第五版答案_第2页
C语言详解第五版答案_第3页
C语言详解第五版答案_第4页
C语言详解第五版答案_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第1章1软件3位,字节,存储单元,主存储器,辅助存储器,LAN,WAN1a、b、c的值相加,把和存储到x;y除以z,将结果存储到x;c减b,然后加a,将结果存储到d;z加1,并将结果存储到z;celsius加,并将结果存储到kelvin。3源程序,编译器,编辑器(字处理器)1问题需求,分析,设计,实现,测试和验证,维护1算法细化(1) 获得以公里表示的距离。(2) 把距离转化成英里。2.1 用英里表示的距离是用公里表示的距离的0.621倍。(3) 显示用英里表示的距离。第2章节1a.void, double, returnb.printfc.MAX_ENTRIES, Gd.time, xyz1

2、23, this_is_a_long_onee.Sue's,part#2,"char", #include3预处理器,#define和#include-33double, int, char1Enter two integers> 5 7m = 10 n = 21 3My name is Jane Doe.I live in Ann Arbor, MIand I have 11 years of programming experience.1/* This is a comment? */* This one seems like a comment doe

3、snt it */1a. 22/7为37/22为0 22 % 7为1 7 % 22为7b. 16 / 15 为1 15 / 16为0 16 % 15为115 % 16为15c. 23 / 3 为7 3 / 23为0 23 % 3为2 3 % 23为3d. 16 / -3 为? -3 / 16为? 16 % -3为? -3 % 16为? (?意为结果是变化的)3a. 3 g. 未定义 m. b. ? h. 未定义 n. c. 1 i. ? o. 1d. j. 3 p. 未定义e. ? k. q. 3f. l. 9 r. (?意为结果是变化的)5a.white为c.orange为0e.lime为2

4、b.green为d.blue为f.purple为1printf("Salary is %10.2fn",salary);3xisiis100iis100xis1在调用scanf获得数据之前先调用printf显示提示。当调用scanf后有数据回应时调用printf。提示用于交互式程序而不用于批处理程序。批处理程序应该回显输入,交互程序也可以回显输入。第3章1问题输入double hours /* number of hours worked */double rate /* hourly rate of pay */ 问题输出double gross /* gross sal

5、ary */ 算法(1)输入工作时间hours和每小时所付的工资率rate。(2)计算总工资gross。2.1把hours * rate的值赋给gross。(3)显示总工资gross。3问题输入double reg_hours /* number of regular hours worked */double ot_hours /* number of overtime hours worked */double rate /* hourly rate of pay */ 问题输出double gross /* gross salary */ 算法(1)输入常规工作时数(reg_hours)

6、、加班时数(ot_hours)和每小时所付工资比率rate。(2)计算总工资gross。2.1 将reg_hours * rate的值赋给gross。2.2 前一步gross的值再加上ot_hours * 1.5 * rate。(3)显示总工资gross。1a. sqrt(u + v) * pow(w,2) b. log(pow(x,y) c.sqrt(pow(x y, 3) d. fabs(x * y w / z)1设计阶段1HI MOM被用较大的印刷体字母垂直地输出。1a. b. c.* * * * 31.42 * * * *d. e. 3函数参数被用来在一个程序的各个模块之间、主程序与其

7、模块之间传递信息。参数使函数更容易被其他函数或程序复用。带参数的函数是建造更大程序的基石。第4章3x = 3.0 y = 4.0 z = 2.0 flag = 0 ! ( flag | ( y + z >= x z ) 0 1 1 011(TRUE)0(FALSE)1(TRUE)1(TRUE)5ans为2。1a.not lessb.greater than1. if (x > y ) x = x + 10.0; printf("x Biggern"); else printf("x Smallern"); printf("y is

8、%.2fn",y); 3. if (engine_type = 'J') printf("Jet engine"); speed_category = 1; else printf("Propellers"); speed_category = 2; 1额外的程序常量:CAP_GALLONS 100 /* maximum number of gallons (in thousands) for basic fee */ 额外的程序变量:int excess /* number of gallons over CAP_GALLON

9、S */ comp_use_charge的改写算法:(1)used 为 current previous(2)if used > CAP_GALLONS excess 为used GAP_GALLONS use_charge 为 CAP_GALLONS * PRE_1000_CHG + excess * PER_1000_CHG * 2else use_charge 为 used * PER_1000_CHG1.语句部分工资税结果if (salary < 0.0 )?23500.00 < 0.00 为假else if (salary < 15000.00 )23500.

10、00 < 15000.00 为假else if (salary < 30000.00 )23500.00 < 30000.00 为真tax = (salary 15000.00)* 0.18 3.if ( pH > 7 ) if (pH < 12 ) printf("Alkaline"); else printf("Very alkaline"); else if (pH = 7 ) printf("Neutral"); else if (pH > 2) printf("Acidic&qu

11、ot;); else printf("Very acidic");1.redblue yellow第5章1a. 计数循环(1) 初始化sum为0。(2) 设置lcv为0。(3)while lcv<35(4)获得下一个考试成绩。(5)把考试成绩与sum相加。(6)把lcv的值加1。3控制文件结束的循环(1)初始化count为0。(2)获得第一个温度并存储输入状态。(3) while 输入状态并不表示已到达文件末尾(4) if温度>100, count加1;(5) 获得下一个温度并存储输入状态。10 101 92 83 74 65 51a. Enter an int

12、eger> 5 5 25 125 625 b. Enter an integer> 6 6 36 216 1296c. Enter an integer> 7 7 49 343 2401 一般地,该循环显示n,n2,n3和n4。3count = 0; sum = 0; while (count < 5) printf("Next number> "); scanf("%d", &next_num); sum += next_num; count += 1; printf("%d numbers were a

13、dded; ", count); printf("their sum is %d.n",sum);1当n = 8时:语 句addsum作 用sum = 0; 0初始化sum为0odd = 1; 1初始化odd为1odd < n; 1<8为真sum += odd; 1sum = 0 + 1odd +=23odd = 1 + 2odd < n; 3<8 为真sum += odd; 4sum = 1 + 3odd +=25odd = 3 + 2odd < n; 5<8 为真sum +=odd; 9sum = 4 + 5odd +=27

14、odd = 5 + 2odd < n; 7<8为真sum += odd; 16sum = 9 + 7odd += 29odd = 7 + 2odd < n; 9<8 为假退出循环printf("Sum ofOutput: Sum of positive odd numbers less than8 is 16.3两个问题的答案都为0。5+i -j; n = i * j; m = i + j; j-; p = i + j;7.a.1 10 2 8 3 6 4 4 5 2b. j = 10; for ( i = 0; i < 5; +i) printf(&q

15、uot;%d %dn", i+1,j); j -= 2; 1. 任何小于8 000桶的初始化供给。3.Number of barrels currently in tank> 0 barrels are available. Enter number of gallons removed> After removal of 7581.00 gallons (180.50 barrels), 8170.30 barrels are available. Enter number of gallons removed> After removal of 7984.20

16、gallons (190.10 barrels),Only 7980.20 barrels are left. * WARNING *Available Supply is less than 10 percent of tank's 80000.00-barrel capacity.1步骤a初始化循环控制变量。 步骤c循环重复的条件。 步骤e循环控制变量的更新。1a. * b. *1while循环较好,因为do-while在每次迭代时对相同的条件检测两次。1for (status = fscanf(hdd_file, "%d", &next_hdd); s

17、tatus = 1; status = fscanf(hdd_file, "%d",&next_hdd) if (next_hdd > heat_deg_days) heat_deg_days = next_hdd; coldest_mon = ct; +ct; 1for ( count = 0; count <= n; +count) printf("DEBUG* * * count = %dn", count); sum += count; printf("DEBUG* * * sum = %dn", sum)

18、; 第6章1.void sum_n_avg(double n1, /* input numbers */ double n2, double n3, double *sump, /* output sum of the three numbers */double *avgp) /* output average of the numbers */3引 用何处合法数据类型值&manymain int *指向带灰底的单元valpsubdouble *指向带灰底的单元codemainchar'g'&codemainchar *指向不带底纹的单元countpsubin

19、t *指向带网格的单元*countpsubint14*valpsubdoubleletpsubchar *指向不带底纹的单元&xmaindouble *指向带灰底的单元1函数调用num1num2num3order(&num3, &num2);81210order(&num2, &num1);128order(&num3, &num2);108对函数order的这种调用顺序将num1、num2、num3变成降序(从最大到最小)。节1void onef(int dat, int *outlp, int *out2p) int tmp; two

20、f(dat, &tmp, out2p); . void twof(int indat, int *result1p, int *result2p)1因为有很多值需要返回,这只能通过使用输出参数实现。第7章1表示误差出现在当一个double类型变量的尾数包含的位(二进制位)数不足以精确表示某个分数时;抵消误差发生在当两个在数量上相差悬殊的数进行某种运算时,其结果是较小的数丢失。3 x y m n10.5 7.2 5 2a. x / (double)m b. x / m c. (double)(n * m) d. (double)(n / m) + y e. (double)(n / m)

21、 1a. 3 b. 'E' c. -11typedef enum monday, tuesday, wednesday, thursday, friday, saturday, sunday dat_t;a.0b.3c.0(FALSE)d.fridaye.wednesdayf.1(TRUE)节12.0.3.0就是一个这样的区间。第8章节1x3是一个有效的变量名,x3是对数组x第4个元素的引用。3double sq_root11; int cube11;1之前 节x0x1x2x3x4x5x6x7之后 x0x1x2x3x4x5x6x71#include<math.h>

22、#define MAX_SIZE 11. double cubeMAX_SIZE; int sq_rootMAX_SIZE; int i; for (i = 0; i < MAX_SIZE; +i) sq_rooti = sqrt(double)i); cubei = i * i * i; 1seg_len = sqrt(pow(xi+1 xi, 2) + pow(yi+1 yi, 2);3sum = 0; for (i = 0; i < LIST_SIZE; i += 2) sum += listi;1如果一个数组的几个元素都被某个函数处理,传递整个数组数据比传递单个的元素更好。

23、3/* * Gets data to place in dbl_arr until value of sentinel* is encountered in the input.* Returns number of values stored through dbl_sizep.* Stops input prematurely if there are more than dbl_max data* values before the sentinel or if invalid data is encountered.* Pre: sentinel and dbl_max are def

24、ined and dbl_max* is the declared size of dbl_arr* Post: returns 1 for no error, and 0 for any error conditions.*/intfill_to_sentinel(int dbl_max, /* input declared size of dbl_arr */double sentinel,/* input end of data value in input list */double dbl_arr,/* output array of data */int *dbl_sizep)/*

25、 output number of data values stored in dbl_arr */ double data;int i, status; int result = 1; /* Sentinel input loop */ i = 0; status = scanf("%lf",&data); while (status = 1 && data != sentinel && i < dbl_max) dbl_arri = data; i+; status = scanf("%lf",&

26、data); /* Issues error message on premature exit */ if (status != 1) printf("n* Error in data format *n"); printf("* Using first %d data values *n", i); result = 0; else if (data != sentinel) printf("n* Error: too much data before sentinel *n"); printf("* Using fir

27、st %d data values *n", i); result = 0; /* Sends back size of used portion of array, and error status */ *dbl_sizep = i; return (result); 5.| | $ | $ - | $ ch is -.节1a.n-1作为匹配的位置返回。 b. 第一个匹配的位置被返回。3为实现降序排序,用index_of_max代替所出现的每个index_of_min,用largest代替所有的smallest来改写选择排序算法。节1a. int i, j, k; for (i

28、= 0; i < MAXCRS; +i) printf("Processing course number %d: n", i); for (j = 0; j < 5; +j) printf(" Campus %dn", j); for (k = 0; k < 4; +k) printf(" Enter number of "); switch (k) case 0 : printf("Freshmen > "); break; case 1 : printf("Sophomore

29、s > "); break; case 2 : printf("Juniors > "); break; case 3 : printf("Seniors > "); break; scanf("%d", &enrollijk); b. int i, j, jcnt; /* Number of juniors. */ jcnt = 0; for (i = 0; i < MAXCRS; +i) for (j = 0; j < 5; +j) jcnt += enrollij2;c. /* *

30、 Compute the number of students in a course who have a * specific rank. * returns -1 if rank or course is out of range. */ int find_students (int enroll54, int rank, int course) int i, cnt = 0; if (rank >= 0 && rank <= 3) && (course >= 0 && course < MAXCRS) for (i

31、 = 0; i < 5; +i) cnt += enrollcourseirank; else cnt = -1; return (cnt); 用 printf("Number of sophomores in course 2 is %dn", find_students(enroll, 1, 2);d. int i, j, total, upper; total = 0; upper = 0; for (j = 0; j < 5; +j) for (i = 0; i < MAXCRS; +i) upper += enrollij2 + enrollij

32、3; printf("Number of upperclass students on campus "); printf("%d is %d.n", j, upper); total += upper; printf("Total upperclass students on all campuses is %d.n",total);1.a. sales1fall, sales1winter, sales1spring, sales1summerb. sales0spring, sales1spring, sales2spring,

33、 sales3spring, sales4springc. sales0fall, sales1fall, sales2fall, sales3fall, sales4fall sales0winter, sales1winter, sales2winter, sales3winter, sales4winter sales0spring, sales1spring, sales2spring, sales3spring, sales4spring sales0summer, sales1summer, sales2summer, sales3summer, sales4summer第9章1b

34、3 char blanks = ""或 char blanks30 = ""1Ad John Quincy Join1John Adams1a. if (strcmp(name1,name2) = 0) printf("Names match.n"); else printf("Names do not match.n");b. if (strcmp(w1,w2) < 0) strcpy(word, w1); else strcpy(word, w2);c. int i, len; len = strlen(s1); i = strlen(s2); if (i < len) len = i; /* Since len will never be larger than STR_LEN, no need to check for overflow of strings. */ for (i = 0; i < len && s1i = s2i; +i) mtchi = s1i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论