高校教师人事管理系统 (c 和链表 )

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define FORMAT printf("编号 姓名 性别 出生日期(格式:2010 08 01) 参加工作时间 工资 学院 职称 学位\n ");
#define LEN sizeof(struct teacher)

struct teacher *creat(void);
void print(struct teacher *head);
void save(struct teacher *head);
struct teacher *read(void);
struct teacher *add(void);
struct teacher *array(struct teacher *head);
struct teacher *del();
struct teacher *modify();
void find();
void statistics();

struct date
{
int year,month,day;
};


typedef struct teacher //编号、姓名、性别、出生日期、参加工作时间、工资、学院、职称(助教、讲师、副教授、教授)、学位(学士、硕士、博士)
{
long number;
int salary;
char name[20], sex[10], date_time[20], college[20],title[20], degree[20] ;
struct date birth;
struct teacher *next;
}TE;



int n=0,m=0,n_1=0; //n:记录教师数目;m和n_1都是记录读取文件中的教师数目;

TE * creat(void) //函数功能:创建teacher信息
{
TE *head=NULL, *p1=NULL, *p2=NULL;

p1=p2=(TE *)malloc(LEN);
printf("请输入教师信息:\n");
FORMAT;
scanf("%ld%s%s%d%d%d%s%d%s%s%s",&p1->number, p1->name, p1->sex, &p1->birth.year,&p1->birth.month,&p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
while(p1->number!=0)
{
n+=1;
if(n == 1) head=p1;
else p2->next=p1;
p2=p1;
p1=(TE *)malloc(LEN);
scanf("%ld%s%s%d%d%d%s%d%s%s%s",&p1->number, p1->name, p1->sex, &p1->birth.year,&p1->birth.month,&p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
//printf("OK!\n");
}
p2->next=NULL;
free(p1);

return(head);
}


void print(TE *head) //函数功能 :输出信息
{
TE *p1=head;
printf("共有教师 %d 名\n",m);
FORMAT;
if(head!=NULL)
do
{
printf("%ld\t%s\t%s\t%d_%d_%d\t%s\t%d\t%s\t%s\t%s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
p1=p1->next;
}while(p1!=NULL);
}


TE *del() //函数功能:删除信息
{
long can_num;
TE *head,*p1,*p2;
p1=p2=head=read();
print(head);
printf("请输入要删除的教师编号\n");
scanf("%ld",&can_num);
while(p1->number != can_num && p1->next !=NULL)
{ p2=p1; p1=p1->next; } //找出p1指向的节点
if(p1->number == can_num)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("del: %ld\n",can_num);
n--;
printf("还有教师%d位\n",n);
}
else printf("没有你要查找的教师!\n");
save(head);
return(head);
}

void find() //函数功能 :查找
{
int select;
long find_num;
char find_name[20],find_time[20],find_college[20],find_title[20];
TE *p1,*p2,*head;
p1=p2=head=read();
print(head);
printf("按下列选项查询教师信息\n");
printf("1.编号 2.姓名 3.参加工

作时间 4.学院 5.职称 \n 请选择:");
scanf("%d",&select);
if(select==1)
{
printf("请输入编号:");
scanf("%ld",&find_num);
while(p1->number != find_num && p1->next !=NULL)
{ p2=p1; p1=p1->next; }
if(p1->number == find_num )
{
FORMAT;
printf("%ld %s %s %d %d %d %s %d %s % s %s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
}
else printf("没有要查找的教师信息!!\n");
}
if(select==2)
{
printf("请输入姓名:");
scanf("%s",find_name);
while(strcmp(p1->name,find_name)!=0 && p1->next !=NULL )
{ p2=p1; p1=p1->next; }
if(strcmp(p1->name,find_name)==0)
{
FORMAT;
printf("%ld %s %s %d %d %d %s %d %s % s %s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
}
else printf("没有要查找的教师信息!!\n");
}
if(select==3)
{
int n_n=0;
printf("请输入参加工作时间:");
scanf("%s",find_time);

while(p1 != NULL)
{
if(strcmp(p1->date_time,find_time)==0)
{
if(n_n ==0) FORMAT;
printf("%ld %s %s %d %d %d %s %d %s % s %s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
n_n++;
}
p1=p1->next;
}
if(n_n == 0) printf("没有要查找的教师信息!!\n");
}
if(select==4)
{
int n_n=0;
printf("学院:");
scanf("%s",find_college);
while(p1 != NULL )
{
if(strcmp(p1->college,find_college)==0 )
{
if(n_n ==0) FORMAT;
printf("%ld %s %s %d %d %d %s %d %s % s %s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
n_n++;
}
p2=p1; p1=p1->next;
}
if(n_n == 0) printf("没有要查找的教师信息!!\n");
}
if(select==5)
{
int n_n=0;
printf("职称:");
scanf("%s",find_title);
while(p1!=NULL )
{
if(strcmp(p1->title,find_title) ==0 )
{
if(n_n ==0) FORMAT;
printf("%ld %s %s %d %d %d %s %d %s % s %s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
n_n++;
}
p2=p1; p1=p1->next;
}
if(n_n == 0) printf("没有要查找的教师信息!!\n");
}

}


void statistics() //函数功能 :统计
{
int birth_num=0,sum=0,title_num[4]={0},de_num[3]={0};
float proport=0;
TE *head,*p1,*p2;
p1=head=read();
print(p1);

while(p1 != NULL)
{
if(p1->birth.year >= 1980) birth_num++;
p2=p1; p1=p1->next; sum++;
}
proport=(float)birth_num*100/sum;
printf("\n1980后教师共有 %d 位,80后教师占教师总数的比率是:%4.2f \n",birth_num,proport);


sum=0;
p1=head;
while(p1 != NULL)
{
if(strcmp(p1->title,"助教")==0) title_num[0]++;
if(strcmp(p1->title,"讲师")==0) title_num[1]++;
if(strcmp(p1->title,"副教授")==0) title_num[2]++;
if(strcmp(p1->title,"教授")==0) title_num[3]++;
p2=p1; p1=p1->next; sum++;
}
for(int i=0; i<4 ;i++) sum+=title_num[i] ;
proport=(float)(title_num[2]+title_num[3])/sum;
printf("\n助教%d位、讲师%d位、副教授%d位、教授%d位。\n ",title_num[0],title_num[1],title_num[2],title_num[3]);
printf("职称(包括副教授和教授)的比例:%4.2f\n",proport);

sum=0;
p1=head;
while(p1 != NULL)
{
if(strcmp(p1->degree,"学士")==0) de_num[0]++;
if(strcmp(p1->degree,"硕士")==0) de_num[1]++;
if(strcmp(p1->degree,"博士")==0) de_num[2]++;
p2=p1; p1=p1->next; sum++;

}
proport=(float)de_num[2]/sum;
printf("\n学士%d位、硕士%d位、博士%d位\n ",de_num[0],de_num[1],de_num[2]);
printf("博士学位的教师占教师总数的比例:%4.2f\n",proport);

long salary=0; sum=0;
p1=head;
while(p1 != NULL)
{
salary+=p1->salary; sum++;
p2=p1; p1=p1->next;
}
proport=(float)salary/sum;
printf("\n教师的平均工资是:%.0f\n\n",proport);

}


TE *modify() //函数功能:修改教师信息
{
long change;
int select;
TE *head,*p1,*p2;
p1=p2=head=read();
print(head);
printf("请输入要修改的教师编号:");
scanf("%ld",&change);
while(p1->number != change && p1->next !=NULL)
{ p2=p1; p1=p1->next; } //找出p1指向的节点
if(p1->number == change )
{
printf("将要修改的教师的信息\n");
FORMAT;
printf("%ld %s %s %d %d %d %s %d %s % s %s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
printf("请输入要修改的选项\n");
printf("1.编号 2.姓名 3.性别 4.出生日期 5.参加工作时间 \n 6.工资 7.学院 8.职称 9.学位\n请输入:");
scanf("%d",&select);
printf("请输入该项新的信息:");
if(select==1) { scanf("%ld", p1->number);}
if(select==2) { scanf("%s",p1->name); }
if(select==3) { scanf("%s",p1->sex); }
if(select==4) { printf("格式如:2000,01,02");scanf("%d,%d,%d",p1->birth.year,p1->birth.month,p1->birth.day);}
if(select==5) { scanf("%s",p1->date_time);}
if(select==6) { scanf("%d",p1->salary); }
if(select==7) { scanf("%s",p1->college); }
if(select==8) { scanf("%s",p1->title);}
if(select==9) { scanf("%s",p1->degree); }
}
save(head);

return(head);
}




void save(TE *head) //函数功能:保存文件
{
FILE *out,*num;
TE *p1=head;

putchar(10);
if ((num=fopen("number.dat", "w+"))==NULL)
{ printf("Can not open this file!\n"); exit(0); }
fprintf(num,"%d",n);
fclose(num);

if ((out=fopen("teacher.dat", "w+"))==NULL)
{ printf("Can not open this file!\n"); exit(0); }
fclose(out);


if ((out=fopen("teacher.dat", "r+"))==NULL)
{ printf("Can not open this file!\n"); exit(0); }
for(int i=0; i{
fprintf(out,"%ld %s %s %d %d %d %s %d %s % s %s\n",p1->number, p1->name, p1->sex,p1->birth.year,p1->birth.month,p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
p1=p1->next;
}
fclose(out);
putchar(10);
}

TE *read(void) //函数功能:读取文件
{
FILE *fp,*num;
TE *head=NULL, *p1=NULL, *p2=NULL;

if ((num=fopen("number.dat", "r"))==NULL)
{ printf("Can not open this file!\n"); exit(0); }
fscanf(num,"%d",&n_1);
n=n_1;
if(n == 0) { printf("没有教师信息!\n",n); exit(0); }
fclose(num);

if ((fp=fopen("teacher.dat", "r"))==NULL)
{ printf("Can not open this file!\n"); exit(0); }
for(int i=1; i<=n_1 ; i++ )
{
p1=(TE *)malloc(LEN);
fscanf(fp,"%ld%s%s%d%d%d%s%d%s%s%s\n",&p1->number, p1->name, p1->sex, &p1->birth.year,&p1->birth.month,&p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
m+=1;
if(m == 1) head=p1;
else p2->next=p1;
p2=p1;

}
p2->next=NULL;
fclose(fp);

return(head);
}

TE *add(void) //函数功能:增加教师信息
{
TE *head,*p1,*p2;
head=read();
p2=head;
n=n_1;
while(p2->next != NULL)
{
p2=p2->next;
}
p1=(TE *)malloc(LEN);
printf("请输入增加的教师信息:\n");
FORMAT;
scanf("%ld%s%s%d%d%d%s%d%s%s%s",&p1->number, p1->name, p1->sex, &p1->birth.year,&p1->birth.month,&p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
while(p1->number!=0)
{
n+=1;
if(n == 1) head=p1;
else p2->next=p1;
p2=p1;
p1=(TE *)malloc(LEN);
scanf("%ld%s%s%d%d%d%s%d%s%s%s",&p1->number, p1->name, p1->sex, &p1->birth.year,&p1->birth.month,&p1->birth.day, &p1->date_time, &p1->salary, p1->college, p1->title, p1->degree);
printf("OK!\n");
}
p2->next=NULL;
free(p1);

head=array(head);
save(head);
return (head);
}

TE *array(TE *head) //函数功能:排序
{
TE *p0,*p1,*p2,*h;
h=p1=p2=head;

if(n<=1) return(h);
if(n==2 && (p1->number > p1->next->number) ) {p2=p1->next; h=p2; p1->next=p2->next; p2->next=p1; return(h); }
for(int i=0; i{
p1=p2=h;
for(int j=0; j{

p2=p1->next;
if((p1==head) && (p1->number > p2->number) ) { h=p2; p1->next=p2->next; p2->next=p1;p0=p1; }
else if((p1->number > p2->number) ) { p0->next=p2; p1->next=p2->next; p2->next=p1; p0=p1; }
else { p1->next=p2; p2=p2->next; p0=p1; }

}
}
return(h);
}

void main()
{
int sect=3;
TE *h;
printf("欢迎使用高校教师人事管理系统\n\n");
printf("1.创建教师信息 2.增加 3.阅读 4.删除 5.修改 6.查找 7.统计\n请选择:");
scanf("%d",§);
if(sect == 1 ) {h=creat(); array(h); save(h);} //函数功能 :创建
if(sect == 2 ) h=add(); //函数功能 :增加
if(sect == 3 ) {h=read();print

(h);} //函数功能 :读取信息
if(sect == 4 ) h=del(); //函数功能 :删除
if(sect == 5 ) h=modify(); //函数功能 :修改
if(sect == 6 ) find(); //函数功能 :查找
if(sect == 7 ) statistics(); //函数功能 :统计

}





相关文档
最新文档