📄 chenxiaosen.c
字号:
/*题目:人事基本信息管理系统*/
/*设计方案:本程序分为七个模块:1、新建人事信息模块 2、查询模块 3、删除模块 4、添加模块 5、修改模块 6、打印模块 7、数据备份与还原模块。
每个模块相互独立。且每个模块只完成一个功能,用一个函数实现。在主函数中调用这些模块。每个模块处理的信息即人事基本信息用结构体存储,数据结构采用链表实现,最终以文件形式存储于磁盘上。*/
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct person_b)
struct person_b
{
char sex;
char marage;
unsigned long id;
char name[10];
char dep[10];
char addr[10];
char tel[12];
struct person_b * next;
};/* 个人基本信息*/
main()
{
int choose=2; /* 操作选项*/
struct person_b * head;
struct person_b * per;
static char *filename="abc";/*存储信息的文件名,默认为abc*/
unsigned long id;/*员工编号*/
/*以下为各函数的申明*/
struct person_b * newcreat();
struct person_b * search(struct person_b *,unsigned long);
struct person_b * read_file(char filename[]);
void write_file(struct person_b *,char filename[]);
struct person_b *del_p(struct person_b *,unsigned long);
struct person_b *insert_p(struct person_b *,struct person_b *);
void print(struct person_b * );
struct person_b *rewrite(struct person_b *,unsigned long);
void backup_r(void);
printf("\nchoose one operation!");
printf("\n1:newcreat 2:search 3:delete 4:insert 5:rewrite 6:print 7:backup or recovery 0:exit\n");
scanf("%d",&choose);
while (choose != 0)
{
switch (choose)
{
case 1:
{
printf("\ninput the filename you newcreat:\n");
scanf("%s",&filename);
head = newcreat();
print(head);
write_file(head,filename);
break;
}
case 2:
{
printf("search!\n");
printf("\n input the id:");
scanf("%lu",&id);
head = read_file(filename);
search(head,id);
break;
}
case 3:
{
printf("\ndelete a person!");
printf("\n input the id:\n");
scanf("%lu",&id);
head = read_file(filename);
head = del_p(head,id);
write_file(head,filename);
break;
}
case 4:
{
printf("\ninsert\n");
per = (struct person_b *)malloc(LEN);
printf("\ninput a person's information like:\n");
printf("\nid name sex marage dep addr tel\n");
scanf("%lu %s %c %c %s %s %s",&per->id,&per->name,&per->sex,&per->marage,
&per->dep,&per->addr,&per->tel);
head = read_file(filename);
head = insert_p(head,per);
write_file(head,filename);
break;
}
case 5:
{
printf("\nrewrite!!\n");
printf("\nrewrite a person with id=?\n");
scanf("%lu",&id);
head = read_file(filename);
head = rewrite(head,id);
write_file(head,filename);
break;
}
case 6:
{
printf("\nprint all person!\n");
if ((head = read_file(filename)) == NULL)
{
printf("\nno information can be print!\n please newcreat\n");
}
else
{
print(head);
}
break;
}
case 7:
{
backup_r();
break;
}
default:
{
printf("\nchoose a valid operate try again!\n");
}
}
printf("\nchoose one operation!");
printf("\n1:newcreat 2:search 3:delete 4:insert 5:rewrite 6:print 7:backup or recovery 0:exit\n");
scanf("%d",&choose);
}
}
/*以链表形式创建人事信息,每个员工信息用结构体存储,返回指向头节点的指针,当输入编号为 0 时结束创建。
*/
struct person_b * newcreat()
{
struct person_b * head;
struct person_b * p1;
struct person_b * p2;
int n = 0;
p1 = p2 = (struct person_b *)malloc(LEN);
printf("\nnewcreat!!\n");
printf("\ninput person information as follow format end with id=0 \n");
printf("\nEMPLOYEE_ID NAME SEX MARRAGE DEP ADDR TEL\n");
scanf("%lu %s %c %c %s %s %s",&p1->id,&p1->name,&p1->sex,&p1->marage,
&p1->dep,&p1->addr,&p1->tel);
head = NULL;
while (p1->id != 0)
{
n=n+1;
if (n == 1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2 = p1;
p1 = (struct person_b *)malloc(LEN);
printf("\n");
scanf("%lu",&p1->id);
if (p1->id == 0)
{
free(p1);
break;
}
scanf("%s %c %c %s %s %s",&p1->name,&p1->sex,&p1->marage,
&p1->dep,&p1->addr,&p1->tel);
}
printf("\nNEWVREAT OVER!!\n");
p2->next=NULL;
return (head);
}
/*给出链表的头节点和编号进行查询,返回头节点。若查询失败给出相应信息*/
struct person_b * search(struct person_b *head,unsigned long id )
{
struct person_b * p1;
char c='y';
if (head == NULL)
{
printf("\nNO INFORMATION CAN BE SEARCHED!");
return(head);
}
while((c == 'y') || (c == 'Y'))
{
p1 = head;
while ((id != p1->id) && (p1->next != NULL))
{
p1=p1->next;
}
if (id==p1->id)
{
printf("\nthe person's information you search is:");
printf("\nEMPLOYEE_ID NAME SEX MARRAGE DEP ADDR TEL");
printf("\n%ld\t%s\t%c\t%c\t%s\t%s\t%s\t",p1->id,p1->name,
p1->sex,p1->marage, p1->dep,p1->addr,p1->tel);
printf("\nsearch success!\n");
}
else
{
printf("\n%ld not been found!\n",id);
}
printf("\n continue!! y/n\n");
scanf("%lu",id);
scanf("%c",&c);
if ((c=='n') || (c == 'N'))
{
break;
}
printf("input the id:\n");
scanf("%lu",&id);
}
return(head);
}
/*以二进制形式读取filename中内容,并将其放入一个链表当中,返回该链表头节点,操作失败给出提示*/
struct person_b * read_file(char filename[])
{
struct person_b *head;
struct person_b *p1;
struct person_b *p2;
FILE *fp;
fp = fopen(filename,"rb");
if (fp == NULL)
{
printf("\ncan not open this file!");
return NULL;
}
p2 = p1 = (struct person_b *)malloc(LEN);
if (fread(p2,sizeof(struct person_b),1,fp) != 1)
{
printf("\nfile read error!");
return NULL;
}
head = p2;
while (p2->next != NULL)
{
p1 = (struct person_b *)malloc(LEN);
p2->next=p1;
p2 = p1;
if (fread(p2,sizeof(struct person_b),1,fp) != 1)
{
printf("\nfile read error!");
return NULL;
}
}
fclose(fp);
free(p1);
return(head);
}
/*将以head为头节点的链表写入文件filename当中,操作不成功给出提示*/
void write_file(struct person_b * head,char filename[])
{
struct person_b * p;
FILE * fp;
fp = fopen(filename,"wb");
p = head;
if (fp == NULL)
{
printf("\ncannot open this file!!!");
return;
}
if (p == NULL)
{
printf("\nno information can be writen!");
return;
}
do
{
if (fwrite(p,sizeof(struct person_b),1,fp) != 1)
{
printf("\nfile write error");
break;
}
p=p->next;
}while(p!=NULL);
fclose(fp);
}
/*删除以head为头节点的链表当中编号为id的记录*/
struct person_b *del_p(struct person_b *head,unsigned long id )
{
struct person_b *p1;
struct person_b *p2;
char c='y';
if (head == NULL)
{
printf("\nNO INFOMATION CAN BE DELETE!");
return NULL;
}
while ((c=='y') || (c=='Y'))
{
p1 = head;
while ((id != p1->id) && (p1->next != NULL) )
{
p2=p1;
p1=p1->next;
}
if (id == p1->id)
{
if (p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("\ndelete:%lu success!\n",id);
}
else
{
printf("\n%lu not been found!\n",id);
}
printf("\n continue?? y/n\n");
scanf("%lu",&id);
scanf("%c",&c);
if ((c=='n') || (c=='N'))
{
break;
}
printf("input the id:\n") ;
scanf("%lu",&id);
}
return(head);
}
/*将per这条记录按关键字即编号升序插入到以head为头节点的链表当中,并进行了异常处理*/
struct person_b *insert_p(struct person_b *head,struct person_b *per)
{
struct person_b *p0;
struct person_b *p1;
struct person_b *p2;
struct person_b *p3;
int flag=0;
char c='y';
if (per == NULL)
{
printf("\nno information canbe insert!\n");
return head;
}
if (head == NULL)
{
printf("\n person information is empty! please newcreat!\n");
return head;
}
while ((c=='y') || (c=='Y'))
{
p1=head;
p0=per;
p3=head;
while (p3 != NULL)
{
if (per->id == p3->id)
{
flag=1;
break;
}
p3=p3->next;
}
if (flag)
{
printf("\n insert failed! the id has been used!\n");
free(per);
break;
}
while ((p0->id > p1->id) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if (p0->id <= p1->id)
{
if (head==p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
printf("\ncontinue?? y/n?\n");
scanf("%lu",&per->id);
scanf("%c",&c);
if ((c=='n') || (c=='N'))
{
break;
}
per=(struct person_b *)malloc(LEN);
printf("\ninput a person's information like:\n");
printf("\nid name sex marage dep addr tel\n");
scanf("%lu %s %c %c %s %s %s",&p1->id,&p1->name,&per->sex,&per->marage,
&per->dep,&per->addr,&per->tel);
printf("\n insert success!!!!!!\n");
}
return(head);
}
/*输出以head为头节点的链表,即输出全部人事信息。*/
void print(struct person_b * head)
{
struct person_b *p;
printf("\nEMPLOYEE_ID NAME SEX MARRAGE DEP ADDR TEL");
p = head;
if (head != NULL)
{
do
{
printf("\n%lu\t%s\t%c\t%c\t%s\t%s\t%s\t",p->id,p->name,
p->sex,p->marage, p->dep,p->addr,p->tel);
p=p->next;
}while(p!=NULL);
}
printf("\n print over!!\n");
}
/*改写以head为头节点的链表当中编号为id的那条记录*/
struct person_b *rewrite(struct person_b *head,unsigned long id)
{
struct person_b * p1;
char c = 'y';
if (head == NULL)
{
printf("\nNO SUCH PERSON!");
return NULL;
}
while ((c=='y') || (c=='Y'))
{
p1=head;
while ((id!=p1->id) && (p1->next!=NULL) )
{
p1=p1->next;
}
if (id==p1->id)
{
printf("\nthe person you want to rewrite is:");
printf("\nemployee_id name sex marrage dep addr tel\n");
printf("\n%lu\t%s\t%c\t%c\t%s\t%s\t%s\t",p1->id,p1->name,
p1->sex,p1->marage,p1->dep,p1->addr,p1->tel);
printf("\ninput rewrite information\n");
scanf("%lu %s %c %c %s %s %s",&p1->id,&p1->name,&p1->sex,&p1->marage,
&p1->dep,&p1->addr,&p1->tel);
printf("\nrewrite success!\n");
}
else
{
printf("\n%lu not been found!\n",id);
}
printf("\ncontinue?? y/n?\n");
scanf("%lu",&id);
scanf("%c",&c);
if ((c=='n') || (c=='N'))
{
break;
}
printf("\ninput the id:\n");
scanf("%lu",&id);
}
return(head);
}
/*将一个文件当中的内容复制到另一个文件中去,两文件均有用户指定*/
void backup_r( void)
{
FILE *in;
FILE * out;
char in_file[10];
char out_file[10];
printf("\n input the source filename which you backup or recvery:\n");
scanf("%s",in_file);
printf("\n input the dest filename you want it be there!!\n");
scanf("%s",out_file);
if ((in = fopen(in_file,"rb")) == NULL)
{
printf("\n can not open the source file!! maybe it isn't exist!");
return;
}
if ((out = fopen(out_file,"wb")) == NULL)
{
printf("\n cannot open the dest file\n!!");
return;
}
while (!feof(in))
{
fputc(fgetc(in),out);
}
printf("\noperate success!!!\n");
fclose(in);
fclose(out);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -