C语言程序设计现代方法第16章答案
Chapter 16
Answers to Selected Exercises 2. [was #2; modified]
(a)
struct {
double real, imaginary;
} c1, c2, c3;
(b)
struct {
double real, imaginary;
} c1 = {0.0, 1.0}, c2 = {1.0, 0.0}, c3;
(c) Only one statement is necessary:
c1 = c2;
(d)
c3.real = c1.real + c2.real;
c3.imaginary = c1.imaginary + c2.imaginary;
4. [was #4; modified]
(a)
typedef struct {
double real, imaginary;
} Complex;
(b) Complex c1, c2, c3;
(c)
Complex make_complex(double real, double imaginary)
{
Complex c;
c.real = real;
c.imaginary = imaginary;
return c;
}
(d)
Complex add_complex(Complex c1, Complex c2)
{
Complex c3;
c3.real = c1.real + c2.real;
c3.imaginary = c1.imaginary + c2.imaginary;
return c3;
}
11. [was #10; modified] The a member will occupy 8 bytes, the union e will take 8 bytes (the largest member, c, is 8 bytes long), and the array f will require 4 bytes, so the total space allocated for s will be 20 bytes.
14. [was #12; modified]
(a)
double area(struct shape s)
{
if (s.shape_kind == RECTANGLE)
return s.u.rectangle.height * s.u.rectangle.width;
else
return 3.14159 * s.u.circle.radius * s.u.circle.radius; }
(b)
struct shape move(struct shape s, int x, int y)
{
struct shape new_shape = s;
new_shape.center.x += x;
new_shape.center.y += y;
return new_shape;
}
(c)
struct shape scale(struct shape s, double c)
{ struct shape new_shape = s;
if (new_shape.shape_kind == RECTANGLE) {
new_shape.u.rectangle.height *= c;
new_shape.u.rectangle.width *= c;
} else
new_shape.u.circle.radius *= c;
return new_shape;
}
15. [was #14]
(a) enum week_days {MON, TUE, WED, THU, FRI, SAT, SUN};
(b) typedef enum {MON, TUE, WED, THU, FRI, SAT, SUN} Week_days;
17. [was #16] All the statements are legal, since C allows integers and
enumeration values to be mixed without restriction. Only (a), (d), and (e) are safe. (b) is not meaningful if i has a value other than 0 or 1. (c) will not yield a meaningful result if b has the value 1.
Answers to Selected Programming Projects
1. [was #6; modified]
#include <stdio.h>
#define COUNTRY_COUNT \
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
struct dialing_code {
char *country;
int code;
};
const struct dialing_code country_codes[] =
{{"Argentina", 54}, {"Bangladesh", 880},
{"Brazil", 55}, {"Burma (Myanmar)", 95},
{"China", 86}, {"Colombia", 57},
{"Congo, Dem. Rep. of", 243}, {"Egypt", 20}, {"Ethiopia", 251}, {"France", 33}, {"Germany", 49}, {"India", 91}, {"Indonesia", 62}, {"Iran", 98}, {"Italy", 39}, {"Japan", 81}, {"Mexico", 52}, {"Nigeria", 234}, {"Pakistan", 92}, {"Philippines", 63}, {"Poland", 48}, {"Russia", 7}, {"South Africa", 27}, {"South Korea", 82}, {"Spain", 34}, {"Sudan", 249}, {"Thailand", 66}, {"Turkey", 90}, {"Ukraine", 380}, {"United Kingdom", 44}, {"United States", 1}, {"Vietnam", 84}};
int main(void)
{
int code, i;
printf("Enter dialing code: ");
scanf("%d", &code);
for (i = 0; i < COUNTRY_COUNT; i++)
if (code == country_codes[i].code) {
printf("The country with dialing code %d is %s\n", code, country_codes[i].country);
return 0;
}
printf("No corresponding country found\n");
return 0;
}
3. [was #8]
#include <stdio.h>
#include "readline.h"
#define NAME_LEN 25
#define MAX_PARTS 100
struct part {
int number;
char name[NAME_LEN+1];
int on_hand;
};
int find_part(int number, const struct part inv[], int np); void insert(struct part inv[], int *np);
void search(const struct part inv[], int np);
void update(struct part inv[], int np);
void print(const struct part inv[], int np);
/********************************************************** * main: Prompts the user to enter an operation code, * * then calls a function to perform the requested * * action. Repeats until the user enters the * * command 'q'. Prints an error message if the user * * enters an illegal code. * **********************************************************/ int main(void)
{
char code;
struct part inventory[MAX_PARTS];
int num_parts = 0;
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != '\n') /* skips to end of line */ ;
switch (code) {
case 'i': insert(inventory, &num_parts);
break;
case 's': search(inventory, num_parts);
break;
case 'u': update(inventory, num_parts);
break;
case 'p': print(inventory, num_parts);
break;
case 'q': return 0;
相关推荐:
- [求职职场]加法运算定律的运用练习题
- [求职职场]大型石油化工工业过程节能新技术
- [求职职场]2015-2020年中国箱纸板行业分析与投资
- [求职职场]NADEX-IWC5A点焊机故障代码
- [求职职场]英语阅读 非常有用
- [求职职场]鲁卫疾控发〔2012〕2号(联合,印发山东
- [求职职场]2014年莆田公务员行测技巧:数字推理的
- [求职职场]基于最近发展区理论的高中数学课堂有效
- [求职职场]与贸易有关的知识产权协议
- [求职职场]【王风范】微演说·职场演说三
- [求职职场]新时代国珍健康大课堂
- [求职职场]群论期末考试复习题
- [求职职场]施工现场消防安全专项施工方案(范本)-
- [求职职场]初中物理光学知识点归纳完美版
- [求职职场]毕业设计总结与体会范文
- [求职职场]江南大学2018年上半年展示设计第1阶段
- [求职职场]景尚乡民兵参战支前保障方案
- [求职职场]【优质】2019年工会职工之家建设工作总
- [求职职场]数据库技术与应用—SQL Server 2008(第
- [求职职场]汽车变速箱构造与工作原理
- 首钢工业区工业遗产资源保护与再利用研
- 第4课 《大学》节选
- 2016程序文件——检验检测结果发布程序
- 2011年高考试题文言文阅读全解释__2011
- 化学是一门基础的自然科学
- 海外做市商制度的借鉴意义
- 外国建筑史复习资料(
- 七年级下思想品德期末综合测试(二)
- 思政课部2013年上学期教学工作总结
- 电大国际公法任务3 0004
- 《圆的认识》教学设计
- 中国轨道交通牵引变流器行业市场发展调
- 中泰证券#定期报告:坚守时代硬科技和
- 浅论企业财务管理与企业经营投资风险的
- 大功率半导体激光器光纤耦合技术调研报
- 中国传统家具的现状与发展探讨
- Broadcom数字电视芯片助海尔扩展高清电
- 新HSK4词汇练习 超全(五)
- 2013届高考数学单元考点复习12
- 雨霖铃精品课件




