鲁老师布置的综合实验的答案
/*myHead.h*/
struct date
{
int year;
int month;
int day;
};
struct worker
{
char num[8];
char name[12];
struct date birthday;
struct worker *next;
};
#define LEN sizeof(struct worker)
struct worker *ins(struct worker *head,struct worker *p);
void input(struct worker *head);
struct worker *del(struct worker *head,struct date *time);
struct worker *search(struct worker *head,char *p);
void print(struct worker *head);
void printworker(struct worker *p);
int birthcmp(struct date *birthday, struct date *p);
struct worker *creat(int n);
void workerin(struct worker *p);
struct worker *sort(struct worker *head );
/*主调函数info_Manage.c*/
#include<stdio.h>
#include<malloc.h>
#include"myHead.h"
void main()
{
struct worker *head,*pointer,*newworker;
int n;
char number[8];
struct date *time;
printf("请输入职工的个数:/n");
scanf("%d",&n);
head=creat(n);
printf("请输入职工的数据:/n");
input(head);
getchar();
head=sort(head);
printf("你输入的数据排序后是:/n");
print(head);
printf("请输入要查找的职工的编号:/n");
gets(number);
pointer=search(head,number);
if(pointer==NULL)
printf("没找到你输入的编号的职工!/n");
else
{
printf("你要查找的职工的数据如下:/n");
printworker(pointer);
}
newworker=(struct worker *)malloc(LEN);
printf("请输入待插入的结点的数据:/n");
workerin(newworker);
head=ins(head,newworker);
printf("插入后的链表是:/n");
print(head);
time=(struct date *)malloc(sizeof(struct date));
printf("请输入截止的日期,程序将为你删除50周岁以上的职工信息:/n");
scanf("%d%d%d",&(time->year),&(time->month),&(time->day));
head=del(head,time);
if(head==NULL)
printf("链表已为空!/n");
else
{
printf("删除后的结果是:/n");
print(head);
}
}
/*生日比较的函数 超过50返回1,不超过返回0 birthcmp.c*/
#include"myHead.h"
int birthcmp(struct date *birthday, struct date *p)
{
if((p->year)-(birthday->year)>50) return(1);
else if((p->year)-(birthday->year)<50) return(0);
else if((p->month)-(birthday->month)>0) return(1);
else if((p->month)-(birthday->month)<0) return(0);
else if((p->day)-(birthday->day)>=0) return(1);
else return(0);
}
/*对链表进行排序的函数*/
#include<stdio.h>
#include"myHead.h"
#include<string.h>
struct worker *sort(struct worker *head )
{
struct worker *temp,*pa1,*pa2,*ah,*bh;
ah=head;
bh=head->next;
ah->next=NULL;
while(bh!=NULL)
{
pa1=ah;
pa2=pa1->next;
if(strcmp(bh->num,pa1->num)<=0)
{
temp=bh->next;
bh->next=ah;
ah=bh;
bh=temp;
}
else
{
while((pa2!=NULL)&&(strcmp(pa2->num,bh->num)<0))
{
pa1=pa2;
pa2=pa1->next;
}
if(pa2==NULL)
{
pa1->next=bh;
temp=bh->next;
bh->next=NULL;
bh=temp;
}
else if(strcmp(pa2->num,bh->num)>=0)
{
temp=bh->next;
bh->next=pa2;
pa1->next=bh;
bh=temp;
}
}
}
return(ah);
}
/*查找职工信息的函数*/
#include<string.h>
#include"myHead.h"
#include<stdio.h>
struct worker *search(struct worker *head,char *p)
{
struct worker *p1=head,*p2=head;
while((p1!=NULL)&&(strcmp(p1->num,p)))
{
p2=p1;
p1=p2->next;
}
return(p1);
}
/*file name printworker 输出单个职工的数据*/
#include<stdio.h>
#include"myHead.h"
void printworker(struct worker *p)
{
printf("学号 姓名 生日/n");
printf("%s %s %d//%d//%d/n",p->num,p->name,(p->birthday).year,(p->birthday).month,(p->birthday).day);
}
/*输出链表 print.c*/
#include<string.h>
#include<myHead.h>
#include<stdio.h>
void print(struct worker *head)
{
struct worker *p1=head,*p2=head;
printf("学号 姓名 生日 /n");
while(p1!=NULL)
{
printf("%s %s %d//%d//%d/n",p1->num,p1->name,(p1->birthday).year,(p1->birthday).month,(p1->birthday).day);
p2=p1;
p1=p2->next;
}
}
/*insert a new worker*/
#include<string.h>
#include"myHead.h"
#include<stdio.h>
struct worker *ins(struct worker *head,struct worker *p)
{
struct worker *p1=head,*p2=head;
if(strcmp(head->num,p->num)>=0)
{
p->next=head;
head=p;
}
else
{
while((p1!=NULL)&&(strcmp(p1->num,p->num)<0))
{
p2=p1;
p1=p2->next;
}
p2->next=p;
p->next=p1;
}
return(head);
}
/*输入链表的函数*/
#include<stdio.h>
#include"myHead.h"
void input(struct worker *head)
{
struct worker *p1=head,*p2=head;
while(p1!=NULL)
{
scanf("%s%s%d%d%d",p1->num,p1->name,&(p1->birthday).year,&(p1->birthday).month,&(p1->birthday).day);
p2=p1;
p1=p2->next;
}
}
/*删除结点的函数 del.c*/
#include"myHead.h"
#include<stdio.h>
struct worker *del(struct worker *head,struct date *time)
{
struct worker *p1,*p2;
while((head!=NULL)&&(birthcmp(&(head->birthday),time)))
head=head->next;
if(head!=NULL)
{
p2=head;
p1=head->next;
while(p1!=NULL)
{
if(birthcmp(&(p1->birthday),time))
p2->next=p1->next;
else
p2=p1;
p1=p2->next;
}
}
return(head);
}
/*开空间的函数*/
#include<stdio.h>
#include<malloc.h>
#include"myHead.h"
struct worker *creat(int n)
{
int i=0;
struct worker *head,*p1,*p2;
while(i<n)
{
p1=(struct worker *)malloc(LEN);
if(i==0)
head=p1;
else
p2->next=p1;
p2=p1;
i++;
}
p2->next=NULL;
return(head);
}
/*输入单个结构体的函数*/
#include<stdio.h>
#include"myHead.h"
void workerin(struct worker *p)
{
scanf("%s%s%d%d%d",p->num,p->name,&(p->birthday).year,&(p->birthday).m
4000
onth,&(p->birthday).day);
}
- HCNA综合实验答案
- RIPv1和v2综合实验试题的(答案和配置过程) 推荐
- 《零基础入门学习汇编语言》检测点,实验,课后题答案
- 不容错过---CCNA实际应用综合实验
- 实验7综合练习
- 超详细OSPF的基本概念;OSPF多区域配置和综合实验
- cisco路由器综合实验之 ospf在多域环境下的应用
- 山东理工大学ACM平台题答案关于C语言 1177 C语言实验——时间间隔
- 实验五数据库完整性 课外练习题答案
- 《网络通信软件综合实验》课程综合实验报告
- 王爽汇编语言第六章学习笔记和实验五答案
- CCNA的一个综合实验(经典)
- Java 2实用教程(第三版)实验指导与习题解答and实验模版代码及答案(一)
- 实验5-8 综合练习
- 【实验】综合实验(VLAN间通信,ACL技术,DNS/WEB服务器架设,RIP路由)
- OSPFv2的综合实验试题分析第1例(CCNP阶段) 推荐
- 2006年全国硕士研究生入学统一考试西医综合科目试题及其答案
- 实验二 线性表综合实验之静态链表