《C语言程序设计(第3版)何钦铭 颜 晖 第9章结构.doc》由会员分享,可在线阅读,更多相关《C语言程序设计(第3版)何钦铭 颜 晖 第9章结构.doc(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流C语言程序设计(第3版)何钦铭 颜 晖 第9章 结构【精品文档】第 10 页第9章 结构【练习9-1】定义一个能够表示复数的结构类型,一个复数包括实数与虚数两个部分。解答:struct complex float real; float imaginary;【练习9-2】人的出生日期由年、月、日组成,请在例 9-1 中的通讯录结构中增加一个成员:出生日期,用嵌套定义的方式重新定义该结构类型。解答:struct dateint year;int month;int day;struct studentint num;char name10;struct d
2、ate birthday; int computer,english,math;double average;【练习9-3】例 9-1 中,如果要计算的是三门课程的课程平均成绩,应该如何改写程序?解答:#includestruct student int num; char name10; int computer,english,math; double average;int main(void) int i, n;double math_sum,english_sum,computer_sum;struct student s1; printf(Input n:); scanf(%d,
3、&n); printf(Input the students number, name and course scores:n); math_sum=english_sum=computer_sum=0; for(i=1;i=n;i+) printf(No.%d:,i); scanf(%d%s%d%d%d,&s1.num,s1.name,&s1.math,&s1.english,&puter); math_sum+=s1.math; english_sum+=s1.english; computer_sum+=puter; printf(math_ave:%.2lfnenglish_ave:%
4、.2lfncomputer_ave:%.2lfn,math_sum/n,english_sum/n,computer_sum/n); return 0;【练习9-4】定义一个包含 5 名学生信息的结构数组,并对该结构数组的所有元素进行初始化。解答:struct student int num;char name10; int computer, english, math; struct student s5=30101, 张一,69,75,84 ,30132, 李二,80,85,90,40231, 王三,71,91,74,40754, 赵四,65,76,87,50426, 刘五,81,92,
5、73;【练习9-5】参考例 9-2,输入并保存 10 个学生的成绩信息,分别输出平均成绩最高和最低的学生信息。解答:#includestruct student int num; char name10; int computer,english,math; double average;int main(void) int i,n,max,min; struct student students50; printf(Input n:); scanf(%d,&n); for(i=0;in;i+) printf(Input the info of No.%d:n,i+1); printf(num
6、ber:); scanf(%d,&studentsi.num); printf(name:); scanf(%s,&studentsi.name); printf(math score:); scanf(%d,&studentsi.math); printf(english score:); scanf(%d,&studentsi.english); printf(computer score:); scanf(%d,&puter);studentsi.average=(studentsi.math+studentsi.english+puter)/3.0; max=min=0; for(i=
7、1;in;i+) if(studentsmax.averagestudentsi.average) min=i; printf(max_average_infortmation:n);printf(number:%d,name:%s,math:%d,english:%d,computer:%d,average:%.2lfn,studentsmax.num,studentsmax.name,studentsmax.math,studentsmax.english,puter,studentsmax.average); printf(min_average_information:n);print
8、f(number:%d,name:%s,math:%d,english:%d,computer:%d,average:%.2lfn,studentsmin.num,studentsmin.name,studentsmin.math,studentsmin.english, puter, studentsmin.average); return 0;【练习9-6】定义一个 struct student 类型的结构指针,用其实现一个学生信息的输入和输出。解答:struct student /* 学生信息结构定义 */int num; /* 学号 */char name10; /* 姓名 */int
9、 computer, english, math ; /* 三门课程成绩 */double average; /* 个人平均成绩 */s, *p;p = &s;scanf(%d%s%d%d%d, &p-num, p-name, &p-math, &p-english, &p-computer);【练习9-7】改写例 9-3 中的函数 update_score( ),将第一个形参改为结构数组形式。解答:int update_score(struct student s,int n,int num,int course,int score) int i,pos; for(i=0;in;i+) if
10、(si.num=num) break; if(idata.a Dp.data.a5对于以下结构定义,(*p)-str+中的+加在 D 。struct int len; char *str; *p;A 指针 str 上 B指针 p 上Cstr 指向的内容上 D语法错误二、填空题1“.”称为 成员_(分量)_运算符,“-”称为_指向_运算符。2完成下列程序,该程序计算 10 名学生的平均成绩。#include #include struct student int num;char name20; int score;struct student stud10;int main(void)int
11、i , sum = 0 ;for(i = 0; i 10; i+) scanf(%d%s%d ,studi.num,_studi.name_,studi.score);sum += studi.score;printf(aver = %d n, sum/10);return 0;3下列程序读入时间数值,将其加 1 秒后输出,时间格式为:hh: mm: ss,即小时:分钟:秒,当小时等于 24 小时,置为 0。#includestruct int hour, minute, second; time;int main(void)scanf(%d: %d: %d,_&time.hour, &tim
12、e.minute, &time.second_);time.second+;if( _time.second_ = 60) _time.minute+ _; time.second = 0; if(time.minute = 60) time.hour+; time.minute = 0; if( _time.hour = 24_ ) time.hour = 0; printf (%d: %d: %d n, time.hour, time.minute, time.second );return 0;4写出下面程序的运行结果 1 2 A B 。struct s1 char c1, c2; in
13、t n;struct s2 int n; struct s1 m; m = 1, A, B, 2 ;int main(void)printf(“%dt%dt%ct%cn”, m.n, m.m.n, m.m.c1, m.m.c2); return 0;5写出下面程序的运行结果 23, wang, 98.5, wang 。struct abcint a; float b; char *c; ;int main(void)struct abc x = 23,98.5,wang;struct abc *px = &x;printf(%d, %s, %.1f, %s n, x.a, x.c, (*px)
14、.b, px-c );return 0;三、程序设计题1. 时间换算:用结构类型表示时间内容(时间以时、分、秒表示),输入一个时间数值,再输入一个秒数n(n60),以h:m:s的格式输出该时间再过n秒后的时间值(超过24点就从0开始计时)。试编写相应程序。解答:#includestruct time int hour; int minute; int second;int main(void) int n; struct time tim; printf(Enter time:); scanf(%d%d%d,&tim.hour,&tim.minute,&tim.second); printf(
15、Enter seconds:); scanf(%d,&n); tim.hour=(tim.hour*60*60+tim.minute*60+tim.second+n)/3600%24; tim.minute=(tim.minute*60+tim.second+n)/60%60; tim.second=(n+tim.second)%60; printf(%d:%d:%d,tim.hour,tim.minute,tim.second); return 0; 2. 计算两个复数之积:编写程序,利用结构变量求解两个复数之积: (3+4i) (5+6i)。提示:求解(a1+a2i)(b1+b2i),乘积
16、的实部为:a1b1 - a2b2,虚部为:a1b2 + a2b1。解答:#includestruct complex int real,im;struct complex cmult(struct complex a,struct complex b);int main(void) struct complex a=3,4,b=5,6,c; c=cmult(a,b);printf(%d+%di)(%d+%di)=%d+%din,a.real,a.im,b.real,b.im,c.real,c.im); return 0; struct complex cmult(struct complex
17、a,struct complex b) struct complex c; c.real=a.real*b.real-a.im*b.im; c.im=a.real*b.im+a.im*b.real; return c;3. 平面向量加法:输入两个二维平面向量V1=(x1,y1)和V2=(x2,y2)的分量,计算并输出两个向量的和向量。试编写相应程序。解答:#includestruct vector float x,y;struct vector vec(struct vector v1,struct vector v2);int main(void) struct vector v1,v2,v
18、; printf(x1 and y1:); scanf(%f%f,&v1.x,&v1.y); printf(x2 and y2:); scanf(%f%f,&v2.x,&v2.y); v=vec(v1,v2); printf(v=(%f,%f),v.x,v.y); return 0;struct vector vec(struct vector v1,struct vector v2) struct vector v; v.x=v1.x+v2.x; v.y=v1.y+v2.y; return v;4. 查找书籍:从键盘输入 10 本书的名称和定价并存入结构数组中,从中查找定价最高和最低的书的名
19、称和定价,并输出。试编写相应程序。解答:#include#define NUMBER 10struct book char name10; float price;int main(void) int i,maxl,minl,n; struct book testNUMBER; for(i=0;iNUMBER;i+) scanf(%s%f,testi.name,&testi.price); maxl=minl=0; for(i=1;in;i+) if(testmaxl.pricetesti.price) minl=i; printf(Max Price:%f,%sn, testmaxl.pri
20、ce,testmaxl.name); printf(Min Price:%f,%sn, testminl.price,testminl.name); return 0;5通讯录排序:建立一个通讯录,通讯录的结构包括:姓名、生日、电话号码;其中生日又包括三项:年、月、日。编写程序,定一个嵌套的结构类型,输入 n(n10)个联系人信息,再按照他们的年龄从大到小依次输出其信息。试编写相应程序。解答:#includestruct date int year; int month; int day; struct friends_list char name10; struct date birthda
21、y; char phone15; void sort(struct friends_list s,int n); int main(void) int i,n; struct friends_list friends10; printf(Input n:); scanf(%d,&n); printf(Input %d friends name ,birthday and phone:,n); for(i=0;in;i+)scanf(%s%d%d%d%s,friendsi.name,&friendsi.birthday.year,&friendsi.birthday.month,&friends
22、i.birthday.day,&friendsi.phone); sort(friends,n); for(i=0;in;i+)printf(%s%d/%d/%d%sn,friendsi.name,friendsi.birthday.year,friendsi.birthday.month,friendsi.birthday.day,friendsi.phone); return 0; void sort(struct friends_list s,int n) int i,j,index; struct friends_list temp; for(i=0;in-1;i+) index=i;
23、 for(j=i+1;jsj.birthday.year) index=j; else if(sindex.birthday.year=sj.birthday.year)&(sindex.birthday.monthsj.birthday.month) index=j; else if(sindex.birthday.year=sj.birthday.year)&(sindex.birthday.month=sj.birthday.month)&(sindex.birthday.daysj.birthday.day) index=j; temp=si; si=sindex; sindex=te
24、mp; 6按等级统计学生成绩:输入 10 个学生的学号、姓名和成绩,输出学生的成绩等级和不及格人数。每个学生的记录包括学号、姓名、成绩和等级,要求定义和调用函数 set_grade()根据学生成绩设置其等级,并统计不及格人数,等级设置:85100 为 A,7084 为 B,6069 为 C,059 为 D。试编写相应程序。解答:#include#define N 10struct students int num; char name20; int score; char grade;int set_grade(struct students *p);int main(void) struct
25、 students studentN; int i,count; printf(Input 10 students number,name and score: n); for(i=0;iN;i+) printf(No.%d:,i+1);scanf(%d%s%d,&studenti.num,studenti.name,&studenti.score); count=set_grade(student); printf(The count (60): %dn,count); printf(The student grade:n); for(i=0;iN;i+)printf(%d %s %cn,studenti.num,studenti.name,studenti.grade); return 0;int set_grade(struct students *p) int i,n=0; for(i=0;iscore=85) p-grade=A; else if(p-score=70&p-scoregrade=B; else if(p-score=60&p-scoregrade=C; else p-grade=D; n+; return n;
限制150内