/*极限俱乐部会员信息系统*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define Len sizeof (struct staff) /*自动求取staff结构的字节长度*/
struct staff
{
char unit[60];
char name[20];
char sex[20];
int age;
int height;
int weight;
struct staff *next;
} ;
int Interface(char (*x)[20],int n); /*操作界面*/
struct staff *Add(struct staff*); /*增加会员数*/
struct staff *InsertAge(struct staff*,struct staff*); /*按年龄由大到小插入链表*/
struct staff *InsertHeight(struct staff*,struct staff*); /*按身高由高到矮插入链表*/
struct staff *InsertWeight(struct staff*,struct staff*); /*按体重由重到轻插入链表*/
struct staff *SortAge(struct staff *); /*按年龄大小排序*/
struct staff *SortHeight(struct staff*); /*按身高排序*/
struct staff *SortWeight(struct staff*); /*按体重排序*/
void Display(struct staff*); /*显示已存在或者经操作后改变的数据*/
void Search (struct staff*); /*信息查找*/
void Modify(struct staff*); /*信息修改*/
struct staff *Delete(struct staff*); /*信息删除*/
void SaveFile(struct staff*); /*信息存盘*/
struct staff *LoadFile(struct staff*); /*加载文件*/
int n=0;
char password[8]="7654321"; /*设置系统管理员密码*/
void main()
{
struct staff *head=NULL; /*设置链首为空*/
int s; /*设置变量,以用于用户输入信息的接收,并将其作为转向函数的参数*/
char openfile[10];
char choice;
char menu[][20]={ "添加","显示","按年龄排序","按身高排序",
"按体重排序","检索","修改","删除","存盘","返回"};
/**********************系统密码管理********************/

char pass[8];
int flag = 0;
int w = 3;
do{
printf("请输入管理员密码:\n");
scanf("%s",pass);
if(!strcmp(pass,password))
{
printf("PASS\n\n\n");
flag = 1;
break;
}
else
{
printf("密码错误,请重新输入:\n");
w--;
}
}while(w>0);
if(!flag)
{
printf("你已连续三次输入错误,请确认后再使用本系统,谢谢!\n");
exit(0);
}
/**********************打开已有文件********************/
printf ("\n 打开已有的文件\"staff_4.txt\"?(y/n)");
scanf("%s",openfile);
if((strcmp(openfile,"y")==NULL))
{
head=LoadFile(head); /*输入y,则调用LoadFile()函数*/
}
else /*输入n,则系统自动创建一个新文件*/
printf("系统自动创建一个新文件\"staff_4.txt\".\n");

system("cls"); /*用system调用dos命令,清屏*/
printf("~~~~~~~~~~~~~\t\t\t\t\t~~~~~~~~~~~~~\n");
printf("~~~~~~~~~~~~~\t\t\t\t\t~~~~~~~~~~~~~\n");
printf("\t\t欢迎进入极限俱乐部会员信息系统\n");
printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%\t\t\t\t\t%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
printf("-------------------------------");
printf("\n\n\n\n");

printf("单位:北京极限俱乐部\n");
printf("地址:北京西城区西四\n");
printf("会员热线:010-66886688");
printf("\n\n\n\n");
printf("==============\t\t==============\n");
printf("本系统由“天地人和软件有限公司”开发\n");
printf("地址:北京市海淀区学院路\n");
printf(“作者:天天\n”);
printf("==============\t\t==============\n");
printf("\n\n");
printf("\t\t请任意输入一个值并回车进入系统\n");

scanf("%s",&choice);
if(choice=='n'||choice=='N')
{
exit(1);
}
system("cls"); /*用system调用dos命令,清屏*/

do
{
s=Interface(menu,10); /*调用Interface()函数*/
switch (s) /*转向语句,选择以实现不同的功能*/
{
case 0,head=Add(head);break;
case 1,Display(head);break;
case 2,head=SortAge(head);break;
case 3,head=SortHeight(head);break;
case 4,head=SortWeight(head);break;
case 5,Search(head);break;
case 6,Modify(head);break;
case 7,Delete(head);break;
case 8,SaveFile(head);
}
}
while(s>=0&&s<=8);
}
/**********************************************************\
操作界面函数
\**********************************************************/
int Interface(char (*x)[20],int n)
{
int i;
int j;
printf("\n×××××××极限俱乐部会员信息系统×××××××\n");
do
{
for (j=0;j<n;j++)
printf("%2d.%s\n",j+1,x[j]);
printf("请从上述选项中选择:\n");
scanf("%d",&i); /*接收键盘输入的字符*/

}
while (i<0||i>n+1);
return (int)i-1;
}
struct staff *LoadFile(struct staff *head)
{
FILE *fp;
struct staff *sta;
if((fp=fopen("d:\\staff_4.txt","r"))==NULL) /*判断能否以只读方式打开文件*/
{
printf("不能打开文件!\n");
exit(0);
}
while (feof(fp)==0)
{
sta = (struct staff *)malloc(Len);
fscanf(fp,"%s%s%s%d%d%d\n",sta->unit,sta->name,
sta->sex,&sta->age,&sta->height,&sta->weight);
head = InsertAge(head,sta); /*调用InsertAge()函数*/
n++;
}
printf("本链表装载%d个结点.\n",n); /*显示载入的结点数,即有几个人的信息*/
fclose(fp);
return head;

}
/**********************************************************\
添加信息函数
\**********************************************************/
struct staff *Add(struct staff *head)
{
struct staff *p;
while(1)
{
p=(struct staff *)malloc(Len);
printf("\n×××××××××输入会员信息××××××××(按e退出)");
printf("\n [%d]请输入会员信息(按e键退出):\n",n+1);
printf("单位:");
scanf("%s",p->unit);
if (strcmp(p->unit,"e")==0) /*若要退出输入,则按e*/
{
free(p);
break;
}
printf("姓名:");
scanf("%s",p->name);
printf("性别(Male/Female):");
scanf("%s",p->sex);
printf("年龄:");
scanf("%d",&p->age);
printf("身高(cm):");
scanf("%d",&p->height);
printf("体重(kg):");
scanf("%d",&p->weight);
head=InsertAge(head,p);
}
return head;
}
/**********************************************************\
显示信息函数
\**********************************************************/
void Display(struct staff *p)
{
printf("\n%20s%10s%10s","单位","姓名","性别"); /*显示标题*/
printf("%10s%10s%10s","年龄","身高","体重"); /*显示数据信息*/
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
while(p!=0)
{
printf("%20s%10s%10s",p->unit,p->name,p->sex);
printf("%10d%10d%10d",p->age,p->height,p->weight);
printf("\n");
p=p->next; /*将下一结点的指针值赋予当前指针,从而不断向前推进*/
}
}
/**********************************************************\
按姓名排序函数
\**********************************************************/
struct staff *SortAge(struct staff *head)
{
struct staff *header=NULL,*p,*q;
while(head!=0)
{
q=(struct staff *)malloc(Len);
(*q)=(*head);
header=InsertAge(header,q); /*将InsertAge()的返回值赋予指针header*/
p=head;
head=head->next;
free(p); /*释放p所占空间,实现动态功能*/
}
printf("\n 完成年龄排序!\n");
return header;
}
/**********************************************************\
选择法按身高排序函数
\**********************************************************/
struct staff *SortHeight(struct staff *head)
{
struct staff *header=NULL,*p,*q;
while(head!=0)
{
q=(struct staff *)malloc(Len);
(*q)=(*head);
header=InsertHeight(header,q); /*将InsertHeight()的返回值赋予指针header*/
p=head;
head=head->next;
free(p);
}
printf("\n 完成身高排序!\n");
return header;
}
/**********************************************************\
选择法按体重排序函数
\**********************************************************/
struct staff *SortWeight(struct staff *head)
{
struct staff *header=NULL,*p,*q;
while(head!=0)
{
q=(struct staff *)malloc(Len);
(*q)=(*head);
header=InsertWeight(header,q); /*将InsertWeight()的返回值赋予指针header*/
p=head;
head=head->next;
free(p);
}
printf("\n 完成体重排序!\n");
return header;

}
/**********************************************************\
修改信息函数
\**********************************************************/
void Modify(struct staff *head)
{
struct staff *p1,*p2;
char name[20];
p1=p2=head;
printf("请输入会员姓名:");
scanf("%s",name);
while (strcmp(name,p1->name)!=0 && p2->next!=0) /*逐个比较,查找信息*/
{
p2=p1;
p1=p1->next;
}
if (strcmp(name,p1->name)==0) /*找到后修改信息*/
{
printf("请输入新信息:\n");
printf("单位:");
scanf("%s",p1->unit);
printf("姓名:");
scanf("%s",p1->name);
printf("性别:");
scanf("%s",p1->sex);
printf("年龄:");
scanf("%d",&p1->age);
printf("身高(cm):");
scanf("%d",&p1->height);
printf("体重(kg):");
scanf("%d",&p1->weight);
printf("\n修改已完成!\n");
}
}
/**********************************************************\
删除信息函数
\**********************************************************/
struct staff *Delete(struct staff *head)
{
struct staff *p1,*p2;
char name[20];
printf("\n 请输入会员姓名:");
scanf("%s",name);
p1=p2=head;
while (strcmp(name,p1->name)!=0 && p1->next!=0) /*逐个比较,查找信息*/
{
p2=p1;
p1=p1->next;
}

if(strcmp(name,p1->name)==0)
{
if (p1==head) /*删除链首*/
{
head=head->next;
}

else
{
p2->next=p1->next; /*删除p1所指结点*/
}
free(p1); /*释放p1所占内存*/
n=n-1;
printf("\n %s 已删除!\n",name);

}
else
printf("%s 没有找到,请再次确认输入信息!",name);
return head;
}
/**********************************************************\
按姓名检索函数
\**********************************************************/
void Search(struct staff *head)
{
struct staff *p1,*p2;
char name[20];
p1=p2=head;
printf("\n 请输入会员姓名:");
scanf("%s",name);
while (strcmp(name,p1->name)!=0 && p1->next!=0)
{
p2=p1;
p1=p1->next;
}
if(strcmp(name,p1->name)==0) /*逐个比较,查找信息*/
{
printf("\n %20s %10s %10s","单位","姓名","性别");
printf(" %10s %10s %10s","年龄","身高","体重");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("%20s %10s %10s",p1->unit,p1->name,p1->sex);
printf("%10d %10d %10d",p1->age,p1->height,p1->weight);
printf("\n");

}
else
printf("%s 没有找到,请再次确认输入信息!",name);
}
/**********************************************************\
保存文件函数
\**********************************************************/
void SaveFile(struct staff *head)
{
FILE *fp1;
struct staff *p=head;
if((fp1=fopen("d:\\staff_4_new.txt","w"))==NULL)
{ /*以文本方式生成并"写打开"文件*/
printf("不能打开文件!\n");
exit(1);
}
while(p!=NULL)
{
fprintf(fp1,"%10s %10s %10s %10d %10d %10d \n",p->unit,p->name,
p->sex,p->age,p->height,p->weight);
p=p->next;
}
printf("\n 文件已保存!\n");
fclose(fp1);
}
/**********************************************************\
插入姓名文件函数
\**********************************************************/
struct staff *InsertAge(struct staff *head,struct staff *p)/*按年龄由大到小插入链表*/
{
struct staff *p1,*p2;
p1=p2=head;
if (head==0)
{
head=p;
p->next=0;
return head;
}
else
while((p->age<p1->age) && (p1->next!=0))
{
p2=p1;
p1=p1->next;
}
if(p->age>=p1->age)
{
if(p1==head)
{
head=p;
p->next=p1;
}
else
{
p2->next=p;
p->next=p1;
}
}
else
{
p1->next=p;
p->next=0;
}
return head;

}
/**********************************************************\
插入身高函数
\**********************************************************/
struct staff *InsertHeight(struct staff*head,struct staff *p )
{
struct staff *p1,*p2; /*按身高由高到矮插入链表*/
p1=p2=head;
if (head==0)
{
head=p;
p->next=0;
return head;
}
else
while((p->height<p1->height) && (p1->next!=0))
{
p2=p1;
p1=p1->next;
}
if(p->height>=p1->height)
{
if(p1==head)
{
head=p;
p->next=p1;
}
else
{
p2->next=p;
p->next=p1;
}
}
else
{
p1->next=p;
p->next=0;
}
return head;
}
/**********************************************************\
插入体重函数
\**********************************************************/
struct staff *InsertWeight(struct staff* head,struct staff* p)
{
struct staff *p1,*p2; /*按体重由重到轻插入链表*/
p1=p2=head;
if (head==0)
{
head=p;
p->next=0;
return head;
}
else
while((p->weight<p1->weight) && (p1->next!=0))
{
p2=p1;
p1=p1->next;
}
if(p->weight>=p1->weight)
{
if(p1==head)
{
head=p;
p->next=p1;
}
else
{
p2->next=p;
p->next=p1;
}
}
else
{
p1->next=p;
p->next=0;
}
return head;
}