可通过编译,运行的!
综合题目
已知有20个学生记录(包括学号、姓名、成绩)的文件student.dat,所有学生以学号从小到大排序(该文件自行建立)。要求编程序实现查询、排序、插入、删除诸功能。具体要求如下:
a.要求显示如下界面
****************************************
1--------------查询
2--------------排序
3--------------插入
4--------------删除
****************************************
通过选择1-4来确定要做哪一个操作。
B.若选1,则出现如下界面
****************************************
1.1----------按学号查询
1.2----------按姓名查询
1.3----------按成绩查询
****************************************
通过选择1.1-1.3来确定要做哪一个操作,其中:按学号查询用二分法实现;按姓名查询用顺序法实现;按成绩查询实现查询成绩小于m分的学生;找到该生将学生记录输出到屏幕,若查无此人,输出相关信息。
C.若选2,则按成绩从大到小排序,姓名,学号顺序也随之调整。
D.若选3,将一个新学生记录按学号顺序插入,并把结果保存到文件student.dat中。
E.若选4,删除指定学生的记录,并把结果保存到文件student.dat中。
F.以上各个功能均编写成子函数,由主函数调用实现。
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define NULL 0
#define N 5
#define LEN sizeof(struct student)
struct student
{long num;
 char name[10];
 float score;
 struct student *next;
};
int n;
struct student *creat(void)
{struct student *head;
struct student  *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%ld,%f,%s",&p1->num,&p1->score,p1->name);
head=NULL;
while(p1->num)
{n=n+1;
if(n==1) head=p1;
 else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%ld,%f,%s",&p1->num,&p1->score,p1->name);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{struct student *p;
 printf("\n now,these %d records are:\n",n);
 p=head;
 if(head!=NULL)
 do{
    printf("%ld %.1f %s \n",p->num,p->score,p->name);
  p=p->next;
    }while(p!=NULL);
}
void save(struct student *p)
{FILE *fp;
struct student *top;
int i;
top=p;
if((fp=fopen("student.dat","wb"))==NULL)
{printf("cannot open file:\n");
return;
}
fprintf(fp,"these students's message are:");
while(top->num)
{fprintf(fp,"%ld %s %.1f\n",top->num,top->name,top->score);
top=top->next;
}
fclose(fp);
}
void savecaozuo(struct student *p)
{FILE *fp;
struct student *top;
int i;
top=p;
if((fp=fopen("student.dat","ab"))==NULL)
{printf("cannot open file:\n");
return;
}
fprintf(fp,"after deleted(insert),these students's message are:");
while(top->num)
{fprintf(fp,"%ld %s %.1f\n",top->num,top->name,top->score);
top=top->next;
}
fclose(fp);
}
struct student  *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
 p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;}
if(p0->num<=p1->num)
{if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}}
n=n+1;
savecaozuo(head);
return(head);
}
struct student  *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL){printf("\n list null! \n");goto end;}
 p1=head;
 while(num!=p1->num&&p1->next!=NULL)
 {p2=p1;p1=p1->next;}
 if(num==p1->num)
 {if(p1==head)
 head=p1->next;
else p2->next=p1->next;
printf("delete:%d\n",num);
n=n-1;
}
else printf("%ld not been found !\n",num);
  savecaozuo(head);
 end: return(head);
}
void erfenfa(struct student *head)
{int i,top,bott,mid,loca,flag=1,sign=1;
char c;
long znum[N],number;
char zname[N];
float zscore[N];
struct student *p;
p=head;
for(i=0;i<N;i++)
{znum[i]=p->num;
 zname[i]=p->name;
 zscore[i]=p->score;
p=p->next;}
flag=1;
while(flag)
{printf("input the students's number to look for:\n");
  scanf("%ld",&number);
  loca=0;
  top=0;
  bott=N-1;
   if((number<znum[0])||(number>znum[N-1]))
     loca=-1;
    while((sign==1)&&(top<=bott))
    {mid=(bott+top)/2;
     if(number==znum[mid])
       {loca=mid;
         printf("find %ld,its position is%d\n",number,loca+1);
         printf("his message is:\n");
         printf("his num:%ld\n",znum[loca]);
         printf("his name:%s\n",zname[loca]);
         printf("his score:%.1f\n",zscore[loca]);
         sign=0;}
     else {
       if(number<znum[mid])
             bott=mid-1;
           else    top=mid+1;
          }
         }
   if(sign==1||loca==-1)
   printf("%ld is not found \n",number);
   printf("countinue or not (Y\N)?");
   scanf("%c",&c);
     if(c=='N'||c=='n')
     flag=0;
    }
}
void shunxufa(struct student *head)
  {
  struct   student   *p;
  char name[10];
  printf("\n input the name of the student you want to found:\n");
  scanf("%s",name);
  p=head;
  while(p!=NULL&&strcmp(p->name,name)!=0)
  {
  p=p->next;
  };
  if(p==NULL)
  {
  printf("sorry! the student not exist:\n");
  return   ;
  }
  else
  {
  printf("the student's num are:%d",p->num);
  printf("\nthe student's name are:%s",p->name);
  printf("\n the student's score:%3.1f",p->score);
  return;
  }
  }
void chengjifa(struct student *head)
{struct student *p;
 float score;
 p=head;
printf(" below which score you want to get?:\n ");
 scanf("%f",&score);
while(p->num)
{if(p->score<=score)
  {printf("the student's num:%ld \n",p->num);
  printf("the students's name:%s \n",p->name);
   printf("the students's score:%.1f \n",p->score);
  }
 p=p->next;
}
}
void chaxun(struct student *head)
{struct student *p;
 int shu;
 p=head;
printf("\n--------------------------\n");
printf("***************************\n");
printf("    1.1------按学号查询; \n");
printf("    1.2------按姓名查询;  \n");
printf("    1.3------按成绩查询;  \n");
printf("**************************\n");
printf("--------------------------\n\n\n");
printf("input a number (1--3) to make a choose:\n");
scanf("%d",&shu);
switch(shu)
{case 1: erfenfa(p);break;
 case 2:shunxufa(p);break;
 case 3:chengjifa(p);break;
 default :printf("error!\n");
}
}
void paixu(struct student *head)
{struct student *p1,*p2;
 int i,j;
long temp_num;
 char temp_name[10];
 float temp_score;
p1=p2=head;
for(i=0;i<N-1;i++)
{for(j=i+1;j<N;j++)
  {p2=p1;
   p1=p1->next;
    if(p2->score<p1->score)
      {temp_num=p2->num;
       p2->num=p1->num;
       p1->num=temp_num;
       strcpy(temp_name,p2->name);
       strcpy(p2->name,p1->name);
       strcpy(p1->name,temp_name);
       temp_score=p2->score;
       p2->score=p1->score;
       p1->score=temp_score;
      }
 }
p2=head;p1=head;
}
print(head);
}
main()
{
struct student *head,*stu;    
long del_num;
int shu,i;
printf("you must input the %d students's records!!!!!:(num,score,name)\n",N);
head=creat();
print(head);
save(head);
printf("\n-------------------------\n");
printf("**************************\n");
printf("      1------查询;      \n");
printf("      2------排序;       \n");
printf("      3------插入;       \n");
printf("      4------删除;       \n");
printf("*************************\n");
printf("-------------------------\n\n\n");
printf("input a number to made a choose:\n");
scanf("%d",&shu);
switch(shu)
{case 1: chaxun(head);break;
 case 2: paixu(head);break;
 case 3: printf("\n input the insert number;\n");
         stu=(struct student *)malloc(LEN);
          scanf("%ld,%f,%s",&stu->num,&stu->score,stu->name);
           while(stu->num!=0)
            {head=insert(head,stu);
              print(head);
               printf("input the insert records:\n");
               stu=(struct student *)malloc(LEN);
                scanf("%ld,%f,%s",&stu->num,&stu->score,stu->name);
             }
               
               break;
 case 4: printf("\n input the deleted number:\n");
         scanf("%ld",&del_num);
          while(del_num!=0)
           {head=del(head,del_num);
           print(head);
           printf("input the deleted number:\n");
           scanf("%ld",&del_num);
           }
          
          break;
 default :printf("error!\n");
}
}



 
											





 
	    

 
	


 
										
					
	 
											