您的位置:首页 > 其它

鲁老师布置的综合实验的答案

2007-06-08 17:13 85 查看

 /*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);
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: