好文档 - 专业文书写作范文服务资料分享网站

最新C语言程序设计(第3版)何钦铭 颜 晖 第9章 结构

天下 分享 时间: 加入收藏 我要投稿 点赞

1 2 3 4

第9章 结构

【练习9-1】定义一个能够表示复数的结构类型,一个复数包括实数与虚数两个部分。

解答:

struct complex{ float real; float imaginary; };

【练习9-2】人的出生日期由年、月、日组成,请在例 9-1 中的通讯录结构中增加一个成员:出生日期,用嵌套定义的方式重新定义该结构类型。

解答: struct date{ int year; int month; int day; };

struct student{ int num;

5

6

7

8

9

10 11

12

13

14

15

16

17

18

19

1

20

char name[10];

struct date birthday; int computer,english,math;

double average; };

【练习9-3】例 9-1 中,如果要计算的是三门课程的课程平均成绩,应该如何改写程序?

解答:

#include struct student{ int num; char name[10];

int computer,english,math;

double average; };

int main(void) { int i, n;

double math_sum,english_sum,computer_sum;

2

21

22

23

24

25 26

27

28

29

30

31

32

33

34

35

36

37

38

39 40 41

struct student s1;

printf(\ scanf(\

printf(\the student's number, name and course scores:\\n\ math_sum=english_sum=computer_sum=0; for(i=1;i<=n;i++){

printf(\

scanf(%uter);

math_sum+=s1.math; english_sum+=s1.english; computer_sum+=s1.computer; }

printf(\,math_sum/n,english_sum/n,computer_sum/n);

return 0; }

42

43

44

45

46 47 48

49

50

51

52

53 54 55 56 57

58

3

59 60

【练习9-4】定义一个包含 5 名学生信息的结构数组,并对该结构数组的所有元素进行初始化。

解答:

struct student{ int num; char name[10];

int computer, english, math;

};

struct student s[5]={{30101, \张一\李二\80,85,90},{40231, \王三\赵四\\刘五\

【练习9-5】参考例 9-2,输入并保存 10 个学生的成绩信息,分别输出平均成绩最高和最低的学生信息。

解答:

#include struct student{ int num; char name[10];

int computer,english,math;

double average;

4

61

62

63

64

65

66

67 68 69

70 71

72

73

74

75

76

77

78

79

};

int main(void) {

int i,n,max,min;

struct student students[50];

printf(\ scanf(\ for(i=0;i

printf(\ printf(\

scanf(\ printf(\

scanf(\ printf(\

scanf(\ printf(\

scanf(\ printf(\

5

80

81

82

83 84 85

86

87

88

89

90

91

92

93

94

95

96

97

最新C语言程序设计(第3版)何钦铭 颜 晖 第9章 结构

1234第9章结构【练习9-1】定义一个能够表示复数的结构类型,一个复数包括实数与虚数两个部分。解答:structcomplex{floatreal;floatimaginary;};【练习9-2】人的出生日期由年、月、日组成,请在例9-1中的通讯
推荐度:
点击下载文档文档为doc格式
8yt5h9xk0p371qz5d0ci05ej21u0yu00k1m
领取福利

微信扫码领取福利

微信扫码分享