那位大神帮忙把我的排序算法换为快速排序或者换为直接插入排序算法排序后,使用折半查找算法进行查找
#include<stdio.h>#include<malloc.h>
#include<string.h>
#include<Windows.h>
#include<direct.h>
void read();//读取存档
void save();//保存
void showSqList();//显示员工信息
void showhl();//显示最高、低成绩
void look();//查找与修改
void yghlist();//按员工号或分数排序操作
void level_people();//按等级显示人数
//员工
typedef struct Employee{
int eid;
int age;
char name[30];
int sex;
double score;
int level;
} Employee;
//员工初始化
Employee *loadEmployee(){
Employee *em;
em=(Employee *)malloc(sizeof(Employee));
if(em==NULL){return NULL;}
return em;
}
//员工输入数据
void inputEmplooyeedate(Employee *e){
int eid;
int age;
char name[30];
int sex;
float score;
int level;
printf("\n\n\n\n 请输入员工 姓名,[性别(1为男,2为女)]\n\n 年龄,工号,得分 \n\n 输完每一项请按回车键\n\n");
printf(" ");
scanf("%s",&name);
printf(" ");
scanf("%d",&sex);
printf(" ");
scanf("%d",&age);
printf(" ");
scanf("%d",&eid);
printf(" ");
scanf("%f",&score);
if(score>0&&score<=100&&(sex==1||sex==2)){}
else{
printf("\n--------------输入可能有问题!!!!---------------");
}
e->eid=eid;
e->age=age;
strcpy(e->name,name);
e->sex=sex;
e->score=score;
if(score<60){e->level=68;}
if(score>=60&&score<70){e->level=80;}
if(score>=70&&score<80){e->level=65;}
if(score>=80&&score<90){e->level=69;}
if(score>=90&&score<=100){e->level=79;}
}
//链表
typedef struct SqList{
Employee *em;
SqList *next;
}SqList;
//链表初始化
SqList *loadSqList(){
SqList *s;
s=(SqList *)malloc(sizeof(SqList));
if(s==NULL) return NULL;
s->next=NULL;
return s;
}
//链表添加
void inputSqList(SqList *s,Employee *em){
s->em=em;
s->next=NULL;
}
SqList *p; //声明链表指针
SqList *head;//声明链表头指针
char item;//赋值项
int sqnum=0;//数据个数
int fuzhi;//选择值
//显示所有数据
void showSqList(SqList *s,boolean all){
SqList *sq=s;
if(sq==NULL){printf("\n\n\n -----没有数据-----\n\n\n");}
while(sq!=NULL){
printf("\n -------员工信息-----------\n");
printf("\n 员工姓名:%s",sq->em->name);
if(sq->em->sex==1){
printf("\n 员工性别:男");
}
else if(sq->em->sex==2){
printf("\n 员工性别:女");
}
else
{
printf("\n 不明性别!!!!");
}
printf("\n 员工年龄:%d",sq->em->age);
printf("\n 员工号:%d",sq->em->eid);
printf("\n 员工分数%.2f",sq->em->score);
printf("\n 员工等级%c\n\n",sq->em->level);
printf(" 等级O(outstanding) 等级E(Excellent) 等级A(Acceptable)\n 等级P(Poor) 等级D(Dreadful)\n");
printf("-----------------------------------------------------------\n");
if(all)
sq=sq->next;
else{
break;
}
}
}
//主界面
void zhujiemian(){
printf("\n ----员工成绩管理系统----");
printf("\n\n 1.录入员工信息");
printf("\n\n 2.按等级显示人数");
printf("\n\n 3.显示所有员工信息");
printf("\n\n 4.查询和修改员工信息");
printf("\n\n 5.最高分得主,最低分得主");
printf("\n\n 6.储存");
printf("\n\n 7.对员工信息进行排序并显示");
printf("\n\n 8.导入保存数据");
printf("\n\n 9.退出");
printf("\n -------------------------------\n");
}
//录入数据
SqList * luruemployee(){
SqList *sqlist=loadSqList();
Employee *employee=loadEmployee();
inputEmplooyeedate(employee);
inputSqList(sqlist,employee);
return sqlist;
}
//录入界面
void luruemployeejiemian(){
printf("\n ------录入员工信息-------\n");
printf("\n\n 1.输入");
printf("\n\n 结束输入(请输其他数字)\n\n ");
printf("\n --------------------------\n");
printf(" 请输入:");
}
//排序选项
void list(){
printf("\n ------排序并显示-------\n");
printf("\n 1.按员工号排序");
printf("\n\n other数字.按员工成绩排序");
printf("\n -----------------------\n");
printf("\n\n 请输入:");
}
//按员工号或分数排序操作
void yghlist(SqList *s){
SqList *sq=s;
SqList *sql=s;
while(sq!=NULL&&sq->next!=NULL&&sq->next->next!=NULL){
if(fuzhi==1?sq->next->em->eid>sq->next->next->em->eid:sq->next->em->score>sq->next->next->em->score){
SqList*zjz1=sq->next;
SqList*zjz2=sq->next->next;
sq->next->next=NULL;
sq->next=NULL;
zjz1->next=zjz2->next;
zjz2->next=zjz1;
sq->next=zjz2;
if(sql->next!=NULL)
sql=sql->next;
}
sq=sq->next;
}
}
//按员工号或分数排序实现
void yghlisttrue(SqList *s,int x){
for(int i=0;i<sqnum;i++)
yghlist(s);
}
//修改
void change(SqList *s){
inputEmplooyeedate(s->em);
}
//按等级显示人数
void level_people(SqList *s){
int dnum=0;
int pnum=0;
int anum=0;
int enumm=0;
int onum=0;
if(s->next==NULL){
printf("\n\n\n ---------无数据---------\n\n");
}
while(s->next!=NULL){
if(s->next->em->level==68){
dnum++;
}
if(s->next->em->level==80){
pnum++;
}
if(s->next->em->level==65){
anum++;
}
if(s->next->em->level==69){
enumm++;
}
if(s->next->em->level==79){
onum++;
}
s=s->next;
}
printf("\n ------个等级人数-------\n");
printf("\n 等级为D有:%d",dnum);
printf("\n\n 等级为P有:%d",pnum);
printf("\n\n 等级为A有:%d",anum);
printf("\n\n 等级为E有:%d",enumm);
printf("\n\n 等级为O有:%d",onum);
printf("\n -----------------------\n");
}
//查询和修改
void look(){
printf("\n ------查询与修改-------\n");
printf("\n 1.按员工号查询");
printf("\n\n 2.按员工成绩查询");
printf("\n\n 3.按年龄查询");
printf("\n -----------------------\n");
printf("\n\n 请输入:");
int num_1=0;
int num_2=0;
int num_3=0;
boolean show=true;
SqList *s;
scanf("%d",&num_1);
system("cls");
switch(num_1){
case 1:
s=head;
printf("\n ------按员工号查询-------\n");
printf("\n 请输入员工号:\n\n");
scanf("%d",&num_2);
if(s->next==NULL){printf(" 没有该员工\n\n\n\n");}
while(s->next!=NULL){
if(s->next->em->eid==num_2){
showSqList(s->next,false);
printf(" 是否要对其进行修改(1/是,other数字/否)");
scanf("%d",&num_3);
system("cls");
if(num_3==1){
change(s->next);
printf(" 修改成功\n\n\n");
}
show=false;
}
if(show&&s->next->next==NULL){
printf(" 没有该员工\n\n\n\n");
}
s=s->next;
}
break;
case 2:
show=true;
s=head;
printf("\n ------按员工号查询-------\n");
printf("\n 请输入员工成绩:\n\n");
scanf("%d",&num_2);
if(s->next==NULL){printf(" 没有该员工\n\n\n\n");}
while(s->next!=NULL){
if(s->next->em->score==num_2){
showSqList(s->next,false);
printf(" 是否要对其进行修改(1/是,other数字/否)");
scanf("%d",&num_3);
system("cls");
if(num_3==1){
change(s->next);
printf(" 修改成功\n\n\n");
}
show=false;
}
if(show&&s->next->next==NULL){
printf(" 没有该员工\n\n\n\n");}
s=s->next;
}
break;
case 3:
show=true;
s=head;
printf("\n ------按员工号查询-------\n");
printf("\n 请输入员工年龄:\n\n");
scanf("%d",&num_2);
if(s->next==NULL){printf(" 没有该员工\n\n\n\n");}
while(s->next!=NULL){
if(s->next->em->age==num_2){
showSqList(s->next,false);
printf(" 是否要对其进行修改(1/是,other数字/否)");
scanf("%d",&num_3);
system("cls");
if(num_3==1){
change(s->next);
printf(" 修改成功!!!!!\n\n\n");
}
show=false;
}
if(show&&s->next->next==NULL){
printf(" 没有该员工\n\n\n\n");
}
s=s->next;
}
break;
}
}
//显示最高最低员工信息
void showhl(SqList *h){
SqList *s=h;
if(s->next!=NULL){
yghlisttrue(s,1);
printf("\n 最低分是:");
showSqList(s->next,false);
while(s->next!=NULL){
s=s->next;
}
printf("\n 最高分是:");
showSqList(s,false);}
else
printf("\n\n\n----没有数据-----");
}
//释放空间
int deleteall(SqList *sq){
if(sq->next==NULL){
free(sq->em);
free(sq);
sqnum--;
}
else{
sq=sq->next;
deleteall(sq);
}
return 1;
}
//储存
void save(const char *file,SqList *s,int len){
FILE *fp;
int i;
SqList *sq=s;
fp=fopen(file,"w");
for(i=0;i<len;i++){
fprintf(fp,"%d,%d,%d,%f,%d,%s\n",sq->em->age,sq->em->sex,sq->em->eid,sq->em->score,sq->em->level,sq->em->name);
if(sq->next!=NULL){
sq=sq->next;
}
}
fclose(fp);
printf("\n\n\n 保存成功\n\n\n");
}
//读取数据并显示
int read(const char *file){
int num=0;
FILE *fp;
fp=fopen(file,"rb");
int i=0;
p=head;
if(fp==NULL){
printf("\n\n\n 找不到文件文件!!!!");
return -1;
}
while(!feof(fp)){
int eid;
int age;
char name[30];
int sex;
float score;
int level;
fscanf(fp,"%d,%d,%d,%f,%d,%s\n",&age,&sex,&eid,&score,&level,&name);
if(score>0&&score<=100){
p->next=loadSqList();
Employee *em=loadEmployee();
em->eid=eid;
em->age=age;
strcpy(em->name,name);
em->sex=sex;
em->score=score;
em->level=level;
p->next->em=em;
if(p->next!=NULL){
p=p->next;
}
showSqList(p,false);
num++;
++i;
}
printf(" 共%d项\n\n\n",num);
sqnum=num;
}
fclose(fp);
return i;
}
int main(){
head=loadSqList();
p=head;
char file[30];
if(mkdir("d:\\cjglxt")!=0)
{
strcpy(file,"d:\\cjglxt\\a.csv");
}
else{
if(mkdir("c:\\cjglxt")!=0){
strcpy(file,"c:\\cjglxt\\a.csv");
}
}
while(true){
zhujiemian();
printf(" 请输入:");
scanf("%c",&item);
system("cls");
switch(item){
case '1':
while(true){
luruemployeejiemian();
scanf("%d",&item);
system("cls");
if(item==1){
system("cls");
p->next=luruemployee();
sqnum++;
p=p->next;
}
else{
break;
}
}
break;
case '3':
showSqList(head->next,true);
printf(" 共有%d项!!!!!\n\n",sqnum);
system("pause");
break;
case '4':
look();
system("pause");
system("cls");
break;
case '5':
showhl(head);
system("pause");
break;
case '2':
level_people(head);
system("pause");
break;
case '7':
system("cls");
list();
scanf("%d",&fuzhi);
system("cls");
yghlisttrue(head,NULL);
showSqList(head->next,true);
printf(" 共有%d项!!!!!\n\n",sqnum);
system("pause");
break;
case '8':
read(file);
system("pause");
break;
case '6':
save(file,head->next,sqnum);
system("pause");
break;
case '9':
exit(0);
break;
}
}
return 0;
}