




已阅读5页,还剩19页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第七次作业参考答案 (结构体) 第 1 题:计算日期的差值 (1)编写一函数,计算两个日期之间的时间差,并将其值返回。 日期以年、月、日表示。 “时间差”以天数表示。 注意考虑日期之间的闰年。 函数的输入参数为日期1和日期2, 函数的返回值为时间差,单位为天数。 (2)编写一程序,在主函数中输入两个日期,调用上述函数计算两个日期之间的时间差,并 将结果输出。 为了计算简便,假设用户输入的日期1总是早于日期2。 参考代码: #include struct date int year; int month; int day; ; int isLeap(int year); int dif(struct date a, struct date b); void main() struct date a, b; printf(“请输入日期1(空格分隔,年月日):n“); scanf(“%d%d%d“, printf(“请输入日期2(空格分隔,年月日,晚于日期1):n“); scanf(“%d%d%d“, printf(“相差天数为:“); printf(“ %d 天n“, dif(a, b); int isLeap(int year) /判断一个年份是否是闰年的函数 if(year%400=0 | (year%4=0 else return 0; int dif(struct date a, struct date b) int i; long day=0, day1=0, day2=0; int d213=0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,3 1,31,30,31,30,31; / day变量为年份a到年份b前一年的年份总天数 for(i=a.year; i” 。 (运算符前是结构体变量时用“.” ,是指向结构体变量的指针时用“-” ) 参考代码: #include #include #define N 3 /增加程序的可扩展性 struct student char name20; char num15; char place20; char id20; int age; char address100; char sex; char phone15; stuN; void input(struct student stu); void print(struct student stu); void search(struct student stu, char name); void main() char name20; input(stu); print(stu); printf(“n请输入想要查找的学生姓名: “); scanf(“%s“, name); search(stu, name); void input(struct student stu) for(int i=0; i #include #define LEN sizeof(struct polynomial) struct polynomial int a; int x; struct polynomial *next; ; struct polynomial *create(int); void print(struct polynomial *); struct polynomial *add(struct polynomial *, struct polynomial *); void main() struct polynomial *head1, *head2, *p0, *p; int m1, m2, i, n=0; printf(“请输入第一个多项式的项数:“); scanf(“%d“, printf(“请输入第一个多项式(一项一行升幂排列,格式: 系数 指数):n“); head1 = create(m1); printf(“请输入第二个多项式的项数:“); scanf(“%d“, printf(“请输入第二个多项式(一项一行升幂排列,格式: 系数 指数):n“); head2 = create(m2); p0 = add(head1,head2); p = p0; while(p != NULL) n+; p = p-next; printf(“n相加后的多项式项数为:%dn“, n); printf(“n相加后的多项式为:n“); print(p0); struct polynomial *create(int m) struct polynomial *p1, *p2, *head=NULL; p1 = p2 = (struct polynomial *) malloc(LEN); scanf(“%d %d“, head = p1; for(int i=1; ia, p2-next = p1; p2 = p1; p1-next = NULL; return(head); void print(struct polynomial *head) struct polynomial *p; p = head; while(p != NULL) printf(“%d %dn“, p-a, p-x); p = p-next; struct polynomial *add(struct polynomial *head1, struct polynomial *head2) struct polynomial *p1, *p2, *p3; p1 = head1; while(head2 != NULL) /将后者往前者中插 p3 = head2; head2 = p3-next; while(p1-x)x) p1 = p1-next; if(p1-x p3-x) /如果p1的次数大 if(p1 = head1) head1 = p3; p3-next = p1; else p2-next = p3; p3-next = p1; else if(p1-x = p3-x) /如果相等则相加 p1-a = p1-a + p3-a; if(p1-a = 0) /若相加后该项的系数为0,则将其删去 if(p1 = head1) head1 = p1-next; else p2-next = p1-next; p1 = p1-next; else /p1移动至末尾 p1-next = p3; p3-next = NULL; return(head1); 参考截图: 第 4 题:循环淘汰 有 N 个同学,编号分别为 1,2,3,N,围成一圈,随便选定一个整数 m,让大家按顺时 针依次报数,报到 m 的同学便会从圈子中退出,从而被淘汰,直到最后剩下一个人。 编写函数实现上述循环淘汰功能。 编写一个程序测试上述函数的功能,要求用户能够任意输入 N 与 m;程序输入最后剩 下人的编号。 参考代码: #include #include #define LEN sizeof(struct stu) struct stu int num; struct stu *next; ; struct stu *create(int n); int select(struct stu *head, int n, int m); void main() struct stu *head; int m, n, last; printf(“请输入N的值: “); scanf(“%d“, printf(“请输入m的值: “); scanf(“%d“, head = create(n); last = select(head, n, m); printf(“最后剩下同学的编号是 %d 号nn“, last); struct stu *create(int n) struct stu *p1,*p2; struct stu *head = NULL; p2 = p1 = (struct stu*) malloc(LEN); for(int i=1; inext = p1; p1-num = i; p2 = p1; p1 = (struct stu*) malloc(LEN); p2-next = head; return head; int select(struct stu *head, int n, int m) struct stu *p1=head, *p2=p1; if (n=1 | m=1) return n; for(int i=1; inext; p2 = p1; p1 = p1-next; p1 = p1-next; p2-next = p1; return p1-num; 参考截图: 第 5 题:工资单处理 (1) 编写函数:有两个单向链表,头指针分别为 list1、list2,链表中每一结点包含员工 号(员工号为关键字段,不重复) 、姓名、工资基本信息,请编一函数,把两个链表拼组成 一个链表,并返回拼组后的新链表,按员工号升序排列。 (2)编写函数:利用指针数组,实现按照工资进行升序排列的功能。返回排序完成的指 针数组 (3)编写一程序,分别输出按员工号排序后的新链表,以及按照工资排序的结果。 假设链表 list1 初始化内容为: 002, name002,3000, 005, name005,2500, 003, name003,3500 链表 list2 初始化内容为: 006, name006,2800, 004, name004,3700, 001, name001,3000, 007, name007, 3600, 参考代码: #include #include #include #define LEN sizeof(struct sal_count) struct sal_count char number10; char name10; int salary; struct sal_count *next; ; void main() struct sal_count *create(int); struct sal_count *link(struct sal_count *list1, struct sal_count *list2); void sort(struct sal_count *list, struct sal_count *sorted, int); struct sal_count *list1, *list2, *list, *p; struct sal_count *sorted; /定义sorted指针指向链表的各个节点 int n1, n2; printf(“请输入第一组员工的人数: “); scanf(“%d“, list1 = create(n1); printf(“请输入第二组员工的人数: “); scanf(“%d“, list2 = create(n2); list = link(list1, list2); printf(“n按员工号升序排列后的链表:n“); p = list; do printf(“%s, %s, %dn“, p-number, p-name, p-salary); p = p-next; while(p != NULL); sorted = (struct sal_count *) malloc(n1+n2) * sizeof(struct sal_count *); sort(list, sorted, n1+n2); printf(“n按工资升序排列后的结果:n“); for(int i=0; inumber, sortedi-name, sortedi-salary); struct sal_count *create(int n) struct sal_count *head=NULL, *p1, *p2; for(int i=0; inext=p1; p2 = p1; scanf(“%s%s%d“, p1-number, p1-name, p2-next = NULL; return(head); struct sal_count *link(struct sal_count *list1, struct sal_count *list2) struct sal_count *point1, *point2; point2 = list2; for(point1=list1; point1-next!=NULL; point1=point1-next);/使point1 指向list1的末尾 point1-next = point2; /连接list1和list2 /此法不能考虑头结点,故另辟一块空间作为起始节点 struct sal_count *x; x = (struct sal_count *)malloc(LEN); x-next = list1; struct sal_count *pre, *q=x-next, *p, *r; x-next = NULL;/开始使用插入法排序 while(q != NULL) pre = x; p = pre-next; while(p!=NULL r = q-next; pre-next = q; q-next = p; q = r; return(x-next); void sort(struct sal_count *list, struct sal_count *sorted, int n) int i, j, k; struct sal_count *temp, *point; for(point=list, i=0; point-next!=NULL; i+, point=point-next) sortedi = point; /将地址赋给各元素 sortedi = point; for(i=0; isalary) salary) k = j; temp = sortedi; sortedi = sortedk; sortedk = temp; 参考截图: 第 6 题:单向链表练习 设节点结构: 学号 姓名 后继结点指针 链表结构: head 编程。要求程序实现如下功能: a) 链表生成。键盘输入学生信息,建立一个节点按学号递增有序的单链表 A=a1,a2,an,比如包含 510 条记录; /假设输入的学号依次为 2010002, 2010005,2010009,2010007,2010003,2010000,姓名自己随便定义 b) 节点计数。 对单链表 A=a1,a2,an编写节点计数函数f, 求单链表中的节点个数。 主函数调用节点计数函数f,并将其返回值(整数)显示到屏幕; c) 对单链表 A=a1,a2,an编写函数fv,将它倒序为 A=an,an-1,a1; d) 编写输出单链表函数 list。每次操作(插入一个新节点或者倒序)之后,调用函数 list,在屏幕上显示链表的全部记录数据。 e) 编写一个函数 search,输入学号,检索链表 A,如果指定学号记录存在则返回指向 该节点的指针,主函数打印纪录信息。若学生纪录不存在,则返回空指针,主函数 给出检索失败的信息。 参考代码: #include #include struct student int num; char name20; struct student *next; ; void main() void list(struct student *head); struct student *create(); struct student *add(); int f(struct student *head); struct student *fv(struct student *head); struct student *search(struct student *head, int n); struct student *head, *p; int n, num; head = add(); n = f(head); printf(“当前链表包含的节点数为 %d 个n“,n); head = fv(head); printf(“链表逆序操作完成!n“,n); list(head); printf(“请输入要查询学生的学号: “); scanf(“%d“, p = search(head, num); if(p=0) printf(“没有该学生的信息!n“); else printf(“%dt%snn“, p-num, p-name); void list(struct student *head) struct student *p; printf(“当前链表内容为:n“); for(p=head; p!=NULL; p=p-next) printf(“%dt%sn“, p-num, p-name); printf(“n“); struct student *create() struct student *p = (struct student *) malloc(sizeof(struct student); printf(“请输入学号与姓名(空格分隔,学号为0时停止输入):n“); scanf(“%d%s“, p-next = NULL; if(p-num = 0) p = NULL; return p; struct student *add() struct student *head, *p0, *p1, *p2; p1 = create(); head = p1; list(head); p0 = create(); while(p0 != NULL) p1 = head; while(p0-num p1-num) p1 = p1-next; if(p0-num num) if(p1 = head) head = p0; else p2-next = p0; p0-next = p1; else p1-next = p0; p0-next = NULL; list(head); p0=create(); return head; int f(struct student *head) int n; struct student *p=head; for(n=0; p!=NULL; n+) p = p-next; return n; struct student *fv(struct student *head) struct student *p1, *p2, *p3; int n; n = f(head); if(n = 2) p1 = head; p2 = p1-next; p2-next = p1; p1-next = NULL; head = p2; else if(n 2) p1 = head; p2 = p1-next; p3 = p2-next; p2-next = p1; p1-next = NULL; while(true) if(p3-next = NULL) p3-next = p2; head = p3; break; else p1 = p2; p2 = p3; p3 = p3-next; p2-next = p1; return head; struct student *search(struct student *head, int num) struct student *p; for(p=head; p!=NULL; p=p-next) if(p-num = num) break; return p; 参考截图: 第 7 题:循环单链表(选作) 将上一题链表结构改为下面的形式,并在其上实现功能(a)(e)。 head 循环链表结构 参考代码: #include #include struct student int num; char name20; struct student *next; ; void main() void list(struct student *end); struct student *create(); struct student *add(); int f(struct student *end); struct student *fv(struct student *end); struct student *search(struct student *end, int n); struct student *end, *p; int n, num; end = add(); n = f(end); printf(“当前链表包含的节点数为 %d 个n“,n); end = fv(end); printf(“链表逆序操作完成!n“,n); list(end); printf(“请输入要查询学生的学号: “); scanf(“%d“, p = search(end, num); if(p=0) printf(“没有该学生的信息!n“); else printf(“%dt%snn“, p-num, p-name); void list(struct student *end) struct student *p=end-next; printf(“当前链表内容为:n“); printf(“%dt%sn“, p-num, p-name); for(p=p-next; p!=end-next; p=p-next) printf(“%dt%sn“, p-num, p-name); printf(“n“); struct student *create() struct student *p = (struct student *) malloc(sizeof(struct student); printf(“请输入学号与姓名(空格分隔,学号为0时停止输入):n“); scanf(“%d%s“, p-next = NULL; if(p-num = 0) p = NULL; return p; struct student
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农业科技推广及应用合作协议
- 合同管理智能审核工具
- 2025年亳州蒙城县高中阶段学校第二次公开引进人才12名考试模拟试题及答案解析
- 2025年嘉兴市秀洲区人民医院招聘编外合同制人员4人备考考试题库附答案解析
- 2025年大兴安岭漠河市公开招聘公益性岗位就业人员30人备考考试题库附答案解析
- 2025云南曲靖宣威市丰华街道中心学校招聘编制外学龄前教育辅助人员3人备考模拟试题及答案解析
- 2025-2030食品广告最小化可行产品测试与快速迭代方法论分析报告
- 商业合作市场调研与分析协议
- 2025年湖南郴州永兴县医疗卫生单位招聘专技人员67人考试参考题库及答案解析
- 2025济宁邹城市部分事业单位二次招聘工作人员(教育类)考试模拟试题及答案解析
- 人民币反假知识培训
- 夫妻吵架冷战协议书
- 《湿地生态的保护与利用:课件》
- 情人合同协议书短
- 生产承包劳务合同协议
- 教科版六年级科学上册全册教案【附:2022版科学课标解读】
- 酒店薪酬管理制度细则
- JJG643-2024标准表法流量标准装置
- 《年产量50万吨煤制乙二醇合成工段工艺设计》6400字(论文)
- 成都建材使用一网通系统-建材代理商操作手册
- 幼小衔接音乐课件
评论
0/150
提交评论