#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
typedef struct worker {
char name[9]; //姓名
char number[5]; //编号
char telephone[12]; //电话
char lab[30]; //实验内容
int flag; //是否请假,1表示请假
int workload; //工作量
struct worker* next;
} worker;
void set_color(int color) //设置颜色
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
int txt_number(char* txt)// 读取不同菜单文件行数,便于创建菜单控制循环次数
{
FILE *fp;
int num=0;
char c;
fp = fopen(txt,"r");
if(fp)
{
while((c = fgetc(fp))!= EOF)
{
if(c==' ')
num++;
}
}
fclose(fp);
return (num/5); //除5是因为每行有5个空格
}
void changeTxt(worker*head)// 重新把链表数据读入文件,生成新文件
{
FILE *ft = fopen("temp.txt","w");
worker *p = head->next;
while(p)
{
fprintf(ft,"%s %s %s %s %d %d\n",p->name,p->number,p->telephone,p->lab,p->flag,p->workload);
p=p->next;
}
fclose(ft);
remove("值班信息.txt");
rename("temp.txt","值班信息.txt");
}
void creat(worker*head)// 从文件中读取数据创建人员链表
{
FILE* fp;
int num;
worker*tail;
int i;
if(head==NULL)
{
printf("创建失败!\n");
return;
}
fp = fopen("值班信息.txt","r");
if (fp == NULL)
{
printf("错误文件打不开!\n");
return;
}
num = txt_number("值班信息.txt");
tail=head;
for(i=0;i< num;i++)
{
worker*node = (worker*)malloc(sizeof(worker));
fscanf(fp,"%s%s%s%s%d%d",node->name,node->number,node->telephone,node->lab,&node->flag,&node->workload);
tail->next = node;
tail = node;
}
tail->next=NULL;
fclose(fp);
}
void printList(worker*head)// 输出值班人员信息
{
int i = 1;
worker*p=head->next;
printf("\n\t---------------------------------------------------------------------------------\n");
printf("\n\t序号\t姓名\t编号\t 电话\t 实验内容\t 请假\t 工作量\t\n");
printf("\t---------------------------------------------------------------------------------\n");
while(p)
{
printf("\t%d\t%s\t%s\t %s\t%s\t%d\t %d\t\n",i++,p->name,p->number,p->telephone,p->lab,p->flag,p->workload);
printf("\t---------------------------------------------------------------------------------\n");
p=p->next;
}
}
void delet_member(worker*head)// 删除值班信息
{
int key;
char people[9];
char number[9];
worker*p;
worker*pre;
printf("\n请问您想按以下哪种删除?\n\n\t1.编号删除\n\t2.姓名删除\n\n请输入您的选择: ");
set_color(0xc);
scanf("%d",&key); set_color(0x7);
if(key==2)
{
printf("请输入您需要删除人员的姓名:");
set_color(0xc);scanf("%s",people);set_color(0x7);
p=head->next;
pre=head;
while(p && strcmp(people,p->name))
{
pre=p;
p=p->next;
}
if(p==NULL)
{
printf("\n\n\t\t\t\t删除失败!未找到该人员的信息。\n");
}
else
{
pre->next=p->next;
free(p);
printf("\n\n\t\t\t\t删除成功!人员详细信息如下:\n\n");
printList(head);
changeTxt(head);
}
}
else if(key==1){
printf("请输入您需要删除人员的编号:");
set_color(0xc);scanf("%s",number);set_color(0x7);
p=head->next;
pre=head;
while(p&&strcmp(number,p->number))
{
pre=p;
p=p->next;
}
if(p==NULL)
printf("\n\n\t\t\t\t删除失败!未找到该人员的信息。\n");
else{
pre->next=p->next;
free(p);
printf("\n\n\t\t\t\t删除成功!人员详细信息如下:\n\n");
printList(head);
changeTxt(head);
}
}
else{
printf("\n\n\t\t\t-----提示:您的输入有误!\n\n");
}
}
void change_member(worker* head){ //编辑值班信息
int key;
worker* p;
char people[9];
char number[9];
printf("\n请问您想按以下哪种方式查找到该人员?\n\n\t1.编号\n\t2.姓名\n\n请输入您的选择: ");
set_color(0xc);scanf("%d",&key); set_color(0x7);
if(key==2){
printf("请输入人员的姓名:");
set_color(0xc);scanf("%s",people);set_color(0x7);
p=head->next;
while(p&&strcmp(people,p->name))
p=p->next;
if(p==NULL)
printf("\n\n\t\t\t\t修改失败!未找到该人员的信息。\n");
else{
printf("\n\n\t\t\t\t查找成功!人员详细信息如下:\n\n");
printf("\t-------------------------------------------------------------------------\n");
printf("\n\t姓名\t编号\t 电话\t 实验内容\t\t请假\t工作量\t\n");
printf("\t-------------------------------------------------------------------------\n");
printf("\t%s\t%s\t %s\t%s\t %d\t %d\t\n",p->name,p->number,p->telephone,p->lab,p->flag,p->workload);
printf("\t-------------------------------------------------------------------------\n\n");
printf("请重新输入该人员的全部信息,空格分开\n\n");
set_color(0xc);scanf("%s%s%s%s%d%d",p->name,p->number,p->telephone,p->lab,&p->flag,&p->workload);
set_color(0x7);
printf("\n\n\t\t\t\t修改成功!人员详细信息如下:\n\n");
printList(head);
changeTxt(head);
}
}
else if(key==1){
printf("请输入人员的编号:");
set_color(0xc);scanf("%s",number);set_color(0x7);
p=head->next;
while(p&&strcmp(number,p->number)){
p=p->next;
}
if(p==NULL)
printf("\n\n\t\t\t\t删除失败!未找到该人员的信息。\n");
else{
printf("\n\n\t\t\tt查找成功!人员详细信息如下:\n\n");
printf("\t-------------------------------------------------------------------------");
printf("\n\t姓名\t编号\t 电话\t 实验内容\t\t请假\t工作量\t\n");
printf("\t-------------------------------------------------------------------------\n");
printf("\t%\s\t%s\t %s\t%s\t %d\t %d\t\n",p->name,p->number,p->telephone,p->lab,p->flag,p->workload);
printf("\t-------------------------------------------------------------------------\n\n");
printf("请重新输入该人员的全部信息,空格分开\n\n");
set_color(0xc);scanf("%s%s%s%s%d%d",p->name,p->number,p->telephone,p->lab,&p->flag,&p->workload);
set_color(0x7);printf("\n\n\t\t\tt修改成功!人员详细信息如下:\n\n");
printList(head);
changeTxt(head);
}
}
else{
printf("\n\n\t\t\t-----提示:您的输入有误!\n\n");
}
}
worker* selectSort(worker* head){ //链表选择排序
worker*p = head;
worker*q = head;
worker* pre;
worker* max;
worker* max_pre;
worker* p_pre;
worker*temp;
pre=q;
while(q)
{
p=q;
max=q;
p_pre=pre;
while(p)
{
if(max->workload < p->workload)
{
max_pre=p_pre;
max=p;
}
p_pre=p;
p=p->next;
}
if(q!=max)
{
if(q->next==max)// 要交换的两节点是否相邻
{
q->next=max->next;
max->next=q;
}
else
{
temp=q->next;
q->next=max->next;
max_pre->next=q;
max->next=temp;
}
if(q!=head )// 是否为第一个节点
pre->next=max;
else
head=max;
}
pre=max;
q=max->next;
}
return head;
}
void paixv(worker*head)// 根据编号排序
{
head->next=selectSort(head->next);
printf("\n\n\t\t\tt排序成功!人员详细信息如下:\n\n");
printList(head);changeTxt(head);
}
void add(worker*head)// 添加值班信息
{
worker*q;
worker*p=(worker*)malloc(sizeof(worker));
p->next=NULL;
printf("请按提示输入新加入人员的全部信息,空格分开\n\n");
printf("名字 编号 电话 实验名称 是否请假 工作量 \n\n");
set_color(0xc);scanf("%s%s%s%s%d%d",p->name,p->number,p->telephone,p->lab,&p->flag,&p->workload);
set_color(0x7);
q=head;
while(q->next)
{
q=q->next;
}
q->next=p;
printf("\n\n\t\t\tt修改成功!人员详细信息如下:\n\n");
printList(head);
changeTxt(head);
}
void show(worker*head) //显示全部请假人员
{
int i=1;
worker*p=head->next;
printf("\n\n今日全部请假人员如下:\n");
printf("\n\t序号\t姓名\t编号\t 电话\t 实验内容\t \n");
printf("\t-----------------------------------------------------------------\n");
while(p){
if(p->flag){
printf("\t%d\t%s\t%s\t %s\t%s\t\n",i++,p->name,p->number,p->telephone,p->lab);
printf("\t-----------------------------------------------------------------\n");
}
p=p->next;
}
}
int main()
{
int key;
worker* head = (worker*)malloc(sizeof(worker));
head->next=NULL;
printf("\n\t欢迎进入值班信息系统\n\n");
printf("\n\t------------------------全部值班信息------------------------\n");
creat(head);
printList(head);
set_color(0x2);
printf("\n请问您想进行何种操作\n\n");
set_color(0x7);
printf("\t1.值班信息编辑(请假)\n");
printf("\t2.删除人员\n");
printf("\t3.添加人员\n");
printf("\t4.根据工作量排序\n");
printf("\t5.查看全部请假人员\n");
printf("\t6.退出系统\n");
printf("\n\n请输入您的选择: ");
set_color(0xc);
scanf("%d",&key);
set_color(0x7);
while(key!=6){
switch(key){
case 1:
change_member(head);
break;
case 2:
delet_member(head);
break;
case 3:
add(head);
break;
case 4:
paixv(head);
break;
case 5:
show(head);
break;
}
set_color(0x2);
printf("\n请问您想进行何种操作\n\n");
set_color(0x7);
printf("\t1.值班信息编辑(请假)\n\t2.删除人员\n\t3.添加人员\n\t4.根据工作量排序\n\t5.查看全部请假人员\n\t6.退出系统");
printf("\n\n请输入您的选择: ");
set_color(0xc);scanf("%d",&key); set_color(0x7);
}
return 0;
}