node.h
#ifndef node_h
#define node_h
#define NAME 20
#define DATA 9
#define SEX 3
#define S 10
#define AN 20
typedef struct info
{
char name[NAME];
char sex[SEX];
char birthday[DATA];
char workstartday[DATA];
char studylevel[S];
char post[S];
char address[AN];
char number[AN];
struct info * next;
}node;
node* NodeCreat(node *head,int n);
void NodeDestroy(node *head);
int NodeSize(node *head);
int NodeInsert(node *head,int n,FILE*m);
void NodeDelete(node *head,int n);
node* NodeGet(node *head,int n);
void NodePrint(node *head);
node* NodeSort(node *head,int style);
void main_menu();
#endif
node.c
#include<stdio.h>
#include<stdlib.h>
#include "node.h"
/*此链表计数与数组一样,下标从0开始*/
/*注意此处建立的是带空结点的单链表*/
node* NodeCreat(node *head,int n)/*创建单链表*/
{
int i;
node*p;
p=head=(node*)malloc(sizeof(node));
head->address[0]='\0';
head->birthday[0]='\0';
head->name[0]='\0';
head->number[0]='\0';
head->post[0]='\0';
head->sex[0]='\0';
head->studylevel[0]='\0';
head->workstartday[0]='\0';
for(i=0;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
if(i==n)break;
}
p->next=NULL;
return head;/*返回链表头指针*/
}
void NodeDestroy(node *head)/*回收内存*/
{
node* p;
if(head==NULL)return;
if(head->next==NULL){free(head);return;}
p=head->next;
while(p->next!=NULL)
{
p=head->next;
free(head);
head=p;
}
free(head);
}
int NodeSize(node *head)/*返回链表实际长度*/
{
int size;
for(size=0;head->next!=NULL;size++)
{
head=head->next;
}
return size;
}
int NodeInsert(node *head,int n,FILE*m)/*插入节点,下标从0开始
若n等于实际长度则在尾节点后面插入
否则都在第n个节点前插入*/
{
node *p;
int i,t=NodeSize(head);
for(i=0;i<n;i++)
head=head->next;
if(t-n==0) p=NULL;
else p=head->next->next;
head->next=(node*)malloc(sizeof(node));
head->next->next=p;
if(m==-1)return 0;
if(data_get(head->next,m)){free(head->next);head->next=NULL;return 0;}
else return 1;
}
void NodeDelete(node *head,int n)/*删除节点,n从0开始*/
{
node *p;
int i,flag=0;
if(n+1==NodeSize(head))flag=1;
for(i=0;i<n;i++)
{
head=head->next;
}
if(flag==1)p=NULL;
else p=head->next->next;
free(head->next);
head->next=p;
}
node* NodeGet(node *head,int n)/*取第n个数据的值,n从0开始*/
{
int i;
for(i=0;i<=n;i++)
{
head=head->next;
}
return head;
}
void NodePrint(node *head)/*打印链表*/
{
int i=1;
if(head==NULL)return;
printf(" \t%-20s%-6s%-10s%-10s%-10s%-10s%-20s%-20s\n","姓名","性别","生日","工作日期","学历","职位","住址","电话");
while(head)
{
printf("编号%d:",i++);
data_put(head);
head=head->next;
}
}
node* NodeSort(node *head,int style)/*按条件对链表排序*/
{
node *pre_p,*p,*pre_q,*q;
pre_q=head;
q=head->next;
while(q!=NULL)
{
pre_p=head;
p=head->next;
switch(style)
{
case 1:while(strcmp(q->name, p->name)>0 && p!=q)
{pre_p=p;p=p->next;}break;
case 2:while(strcmp(q->birthday, p->birthday)>0 && p!=q)
{pre_p=p;p=p->next;}break;
case 3:while(strcmp(q->workstartday, p->workstartday)>0 && p!=q)
{pre_p=p;p=p->next;}break;
case 4:while(strcmp(q->studylevel, p->studylevel)>0 && p!=q)
{pre_p=p;p=p->next;}break;
case 5:while(strcmp(q->post, p->post)>0 && p!=q)
{pre_p=p;p=p->next;}break;
case 6:while(strcmp(q->address, p->address)>0 && p!=q)
{pre_p=p;p=p->next;}break;
case 7:while(strcmp(q->number, p->number)>0 && p!=q)
{pre_p=p;p=p->next;}break;
}
if(p!=q)
{
pre_q->next =q->next ;
pre_p->next =q;
q->next =p;
q=pre_q->next ;
}
else
{
pre_q=q;
q=q->next ;
}
}
return head;
}
function.c
#include<windows.h>
#include "node.c"
extern node *workers,*h;//外部变量
extern FILE *Fp;
void function(int i,int a)//完成条件搜索 条件删除 等功能函数
{
node *p=NULL,*q=NULL,*p1=NULL;
int flag=1,j=0,numb[100],na,k;
char n[100];
printf("\t请输入职工|对应信息:");
scanf("%s",n);
workers=h->next;
for(na=1;workers;workers=workers->next)
{
switch(a)
{
case 1:if(strcmp(workers->name,n))goto END;break;
case 2:if(strcmp(workers->birthday,n))goto END;break;
case 3:if(strcmp(workers->workstartday,n))goto END;break;
case 4:if(strcmp(workers->studylevel,n))goto END;break;
case 5:if(strcmp(workers->post,n))goto END;break;
case 6:if(strcmp(workers->address,n))goto END;break;
case 7:if(strcmp(workers->number,n))goto END;break;
default:exit(1);
}
{
if(flag)printf(" \t\t%-20s%-6s%-10s%-10s%-10s%-10s%-20s%-20s\n","姓名","性别","生日","工作日期","学历","职位","住址","电话");
printf("编号 %d:",na++);
data_put(workers);
flag=0;
for(k=0,p1=h->next;p1!=workers;k++)
{
p1=p1->next;
}
numb[j++]=k;
}
END:;
}
if(flag)
{
printf("\t没有找到符合条件的员工信息!\n");
printf("3秒后返回上级菜单...\n");
Sleep(3000);
menu_2(i);
}
numb[j]=-1;
if(i==0){system("pause");getchar();return;}
else if(i==1)
{
printf("输入你要删除员工由上到下的序号:");
scanf("%d",&j);
getchar();
NodeDelete(h,numb[j-1]);
printf("删除成功!\n");
Sleep(1500);
Fp=fopen("workersdata.txt","w");
workers=h->next;
while(workers)
{
if(!strcmp(workers->name,"#"))break;
fileget();
workers=workers->next;
}
fclose(Fp);
}
else if(i==2)
{
printf("输入你要修改员工由上到下的序号:");
scanf("%d",&j);
getchar();
workers=NodeGet(h,numb[j-1]);
printf("\t请输入职工|姓名:");
gets(workers->name);
printf("\t请输入职工|性别:");
gets(workers->sex);
printf("\t请输入职工|生日:");
gets(workers->birthday);
printf("\t请输入职工|工作日期:");
gets(workers->workstartday);
printf("\t请输入职工|学历:");
gets(workers->studylevel);
printf("\t请输入职工|职位:");
gets(workers->post);
printf("\t请输入职工|住址:");
gets(workers->address);
printf("\t请输入职工|电话号码:");
gets(workers->number);
printf("修改成功!\n");
Sleep(1500);
Fp=fopen("workersdata.txt","w");
workers=h->next;
while(workers)
{
if(!strcmp(workers->name,"#"))break;
fileget();
workers=workers->next;
}
fclose(Fp);
}
}
main.c
#include<stdio.h>
#include<windows.h>
#include "node.h"
extern node *workers=NULL;
extern node *h=NULL;
extern FILE *Fp=NULL;//全局变量
void fileget()
{
fprintf(Fp,"%s %s %s %s %s %s %s %s\n",workers->name,workers->sex,workers->birthday,workers->workstartday, //同步保存到文件
workers->studylevel,workers->post,workers->address,workers->number);
}
int data_get(node * sp,FILE* m)
{
if(m==NULL)
{
printf("\t请输入职工|姓名:");
gets(sp->name);
if(sp->name[0]=='#')
return 1;
printf("\t请输入职工|性别:");
gets(sp->sex);
printf("\t请输入职工|生日:");
gets(sp->birthday);
printf("\t请输入职工|工作日期:");
gets(sp->workstartday);
printf("\t请输入职工|学历:");
gets(sp->studylevel);
printf("\t请输入职工|职位:");
gets(sp->post);
printf("\t请输入职工|住址:");
gets(sp->address);
printf("\t请输入职工|电话号码:");
gets(sp->number);
system("cls");
printf("请输入#结束录入\n");
}
else
{
fscanf(m,"%s %s %s %s %s %s %s %s\n",sp->name,sp->sex,sp->birthday,sp->workstartday,
sp->studylevel,sp->post,sp->address,sp->number);
}
return 0;
}
void data_put(node * sp)//员工信息输出函数
{
printf(" \t%-20s%-6s%-10s%-10s%-10s%-10s%-20s%-20s",sp->name,sp->sex,sp->birthday,sp->workstartday,sp->studylevel,sp->post,sp->address,sp->number);
printf("\n");
}
void smenu_1(int i)//选项的子菜单
{
function(i,1);
}
void smenu_2(int i)
{
function(i,2);
}
void smenu_3(int i)
{
function(i,3);
}
void smenu_4(int i)
{
function(i,4);
}
void smenu_5(int i)
{
function(i,5);
}
void smenu_6(int i)
{
function(i,6);
}
void smenu_7(int i)
{
function(i,7);
}
void menu_1()
{
Fp=fopen("workersdata.txt","a");
workers=h;
workers=NodeGet(workers,NodeSize(workers)-1);
while(NodeInsert(workers,NodeSize(workers),NULL))
{
workers=workers->next;
fileget();
}
fclose(Fp);
}
void menu_2(int i)
{
char num;
START:
system("cls");
printf("\t.************************************.\n");
printf("\t| 职工管理系统 |\n");
printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
printf("\t| |\n");
printf("\t| 1.按姓名查询 |\n");
printf("\t| 2.按生日查询 |\n");
printf("\t| 3.按工作日期查询 |\n");
printf("\t| 4.按学历查询 |\n");
printf("\t| 5.按职务查询 |\n");
printf("\t| 6.按住址查询 |\n");
printf("\t| 7.按电话号码查询 |\n");
printf("\t| 0.返回上一级 |\n");
printf("\t| |\n");
printf("\t`************************************`\n");
printf("\n\n");
switch(i)
{
case 0:printf("\t***查询操作***\n");break;
case 1:printf("\t***删除操作***\n");break;
case 2:printf("\t***修改操作***\n");break;
}
printf("\t请输入对应编号执行操作:");
num=getchar(); /*只接收第一个字符*/
while(getchar()!='\n'); /*防止输入字符过多时重复刷屏*/
switch(num)
{
case '1':smenu_1(i);goto START;
case '2':smenu_2(i);goto START;
case '3':smenu_3(i);goto START;
case '4':smenu_4(i);goto START;
case '5':smenu_5(i);goto START;
case '6':smenu_6(i);goto START;
case '7':smenu_7(i);goto START;
case '0':return;break;
default:goto START;
}
}
void menu_5()
{
char num;
START:
system("cls");
printf("\t.************************************.\n");
printf("\t| 职工管理系统 |\n");
printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
printf("\t| |\n");
printf("\t| 1.按姓名排序 |\n");
printf("\t| 2.按生日排序 |\n");
printf("\t| 3.按工作日期排序 |\n");
printf("\t| 4.按学历排序 |\n");
printf("\t| 5.按职务排序 |\n");
printf("\t| 6.按住址排序 |\n");
printf("\t| 7.按电话号码排序 |\n");
printf("\t| 0.返回上一级 |\n");
printf("\t| |\n");
printf("\t`************************************`\n");
printf("\n\n");
printf("\t请输入对应编号执行操作:");
num=getchar(); /*只接收第一个字符*/
while(getchar()!='\n'); /*防止输入字符过多时重复刷屏*/
switch(num)
{
case '1':h=NodeSort(h,1);break;
case '2':h=NodeSort(h,2);break;
case '3':h=NodeSort(h,3);break;
case '4':h=NodeSort(h,4);break;
case '5':h=NodeSort(h,5);break;
case '6':h=NodeSort(h,6);break;
case '7':h=NodeSort(h,7);break;
case '0':break;
default:goto START;
}
Fp=fopen("workersdata.txt","w");
workers=h->next;
while(workers)
{
if(!strcmp(workers->name,"#"))break;
fileget();
workers=workers->next;
}
fclose(Fp);
printf("文件排序已完成...");
Sleep(1500);
}
void main_menu()
{
char num;
system("mode con:cols=300 lines=200");/*定义窗口大小*/
system("color 0f"); /*定义背景颜色和前景颜色*/
START:
system("cls");
printf("\t.************************************.\n");
printf("\t| 职工管理系统 |\n");
printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
printf("\t| |\n");
printf("\t| 1.新增职工信息 |\n");
printf("\t| 2.条件查询 |\n");
printf("\t| 3.删除职工信息 |\n");
printf("\t| 4.修改职工信息 |\n");
printf("\t| 5.职工信息排序(文件) |\n");
printf("\t| 0.退出 |\n");
printf("\t| |\n");
printf("\t`************************************`\n");
printf("\n\n");
printf("\t请输入对应编号执行操作:");
num=getchar(); /*只接收第一个字符*/
while(getchar()!='\n'); /*防止输入字符过多时重复刷屏*/
switch(num)
{
case '1':menu_1();goto START;
case '2':menu_2(0);goto START;
case '3':menu_2(1);goto START;
case '4':menu_2(2);goto START;
case '5':menu_5();goto START;
case '0':return;
default:goto START;
}
}
int main()
{
system("@echo off"); //关闭提示
if(Fp=fopen("workersdata.txt","r")==NULL) //检测数据文件是否存在
{
system("mode con:cols=55 lines=38");/*定义窗口大小*/
system("cls"); //清屏
printf("您是第一次使用,请按任意键录入职工数据\n或关闭程序后将数据文件放到当前\n目录并命名为workersdata.txt\n");
system("pause");
system("cls");
if(!(Fp=fopen("workersdata.txt","w")))
{
printf("打开文件出错,请重新打开程序\n");
exit(1);
}
h=workers=NodeCreat(workers,0);
while(NodeInsert(workers,NodeSize(workers),NULL))
{
workers=workers->next;
fileget();
}
fclose(Fp);
NodeDestroy(h);
}
Fp=fopen("workersdata.txt","r");//将文件中信息读到内存中
h=workers=NodeCreat(workers,0);
while(!feof(Fp))
{
NodeInsert(workers,NodeSize(workers),Fp);
workers=workers->next;
}
fclose(Fp);
main_menu();
NodePrint(h->next);
NodeDestroy(h);
fclose(Fp);
return 0;
}