c语言链表-学生成绩管理系统

大学时候用链表写的成绩管理系统,可实现增删查改的功能,代码如下


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#define L sizeof( struct student )


struct student
{
long xuehao;
char name[20]; 
float shudian;
float english;
float physics;
float cyuyan;
struct student *next;
};




struct student *creat()

struct student *p1,*p2,*head;
char str[90];
int n = 0;
FILE *fp = fopen( "file2.txt","r" );

if( fp == NULL )
{
printf( "cannot open the file" );
 exit( 0 );
}

puts( fgets( str,90,fp ) );
head = p1 = p2 = ( struct student* )malloc(L);
  fscanf( fp,"%ld %s %f %f %f %f",&p1->xuehao,p1->name,&p1->shudian,
          &p1->english,&p1->physics,&p1->cyuyan );
  
while( !feof( fp ) )
{
  p1 = ( struct student* )malloc( L );
    fscanf(fp,"%ld %s %f %f %f %f\n",&p1->xuehao,p1->name,&p1->shudian,
      &p1->english,&p1->physics,&p1->cyuyan);
      p2->next = p1;
    p2 = p1;
}
p2->next = NULL;
fclose( fp );
return( head );
}




void search( struct student *head,long num )
{
struct student *p;
printf( "search record: " );
scanf( "%ld",&num );
p = head;
while( p )
{
if( p->xuehao == num )
{
printf("xuehao:%ld\nname:%s\nshudian:%6.1f\nenglish:%6.1f\nphysics:%6.1f\ncyuyan:%6.1f\n",
p->xuehao,p->name,p->shudian,
p->english,p->physics,p->cyuyan);
break;
}
p = p->next;
 }
}




void print( struct student *head )
{
struct student *p;
p = head;
if( head != NULL )
while( p != NULL )

printf("%ld    %s  %6.1f  %6.1f  %6.1f   %6.1f\n",p->xuehao,p->name,p->shudian,
p->english,p->physics,p->cyuyan);
p = p->next;
}
}




struct student *sort(struct student *head)
{
struct student *endpt,*p,*p1,*p2;
p1 = (struct student *)malloc(L);
p1->next = head; 
head = p1;
for (endpt = NULL; endpt != head; endpt = p) 
{
for (p=p1 = head; p1->next->next != endpt; p1 = p1->next)
{
    if ((p1->next->shudian+p1->next->english+p1->next->physics+
     p1->next->cyuyan)>(p1->next->next->shudian+ p1->next->next->english+
     p1->next->next->physics+ p1->next->next->cyuyan)) 
    {
    p2 = p1->next->next; 
    p1->next->next = p2->next; 
    p2->next = p1->next; 
    p1->next = p2; 
    //p = p1->next->next; 
    }
}
}
p1 = head; 
head = head->next; 
return head;


}






void save(struct student *head)
{
FILE *fp;int i;
char s[80] = "学号    姓名  数电  英语  物理  c语言 总分 平均分" ;
struct student *p;
head = sort(head);
p = head;

if( (fp = fopen("file3.txt","w")) == NULL )
{
printf( "cannot open the file!\n" );
 exit( 0 );
}

fprintf( fp,"%s\n", s );

while( p != NULL )

fprintf(fp,"%ld %s %6.1f %6.1f %6.1f %6.1f %6.1f %6.1f\n",
 p->xuehao,p->name,p->shudian,p->english,p->physics,p->cyuyan,
 p->shudian+p->english+p->physics+p->cyuyan,
    (p->shudian+p->english+p->physics+p->cyuyan)/4);
    p = p->next;
}

fclose( fp );
}




struct student *del( struct student *head,long xuehao )
{
struct student *p1,*p2;
if( head == NULL )
{
printf( "\nlist null\n" );
}
p1=head;
  int flag = 0;
while( p1)

if( xuehao == p1->xuehao )
 {
  flag = 1;
if( p1 == head )
 {
head = p1->next;
 }
 else
 {   
p2->next = p1->next;
}
  printf("delete xuehao : %ld\n",xuehao);
 }
 p2 = p1;
p1 = p1->next;
}
if( flag == 0 )
{
printf("%ld not been found!\n",xuehao);
}
return( head );
}




struct student *insert( struct student *head,struct student *stu )
{
struct student *p0,*p1,*p2;
p0 = stu;
p1 = head;

if( head == NULL )
{
head = p0;
 p0->next = NULL;
}
else
{
while( ( p0->xuehao > p1->xuehao )
      &&( p1->next != NULL ))
{
p2 = p1;
p1 = p1->next;
}

if( p0->xuehao <= p1->xuehao )
{
if( head == p1 )

head = p0;
}
else
 {
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}

}
  return( head );
}






int main( )
{
struct student *head,stu;
long a,b;
printf( "input records:\n" );
head = creat();
print( head );
search( head , b );
printf( "\ninput the deleted xuehao:" );
scanf( "%ld",&a );
head = del( head , a );
print(head);
printf( "\ninput the inserted record:" );
scanf( "%ld%s%f%f%f%f",&stu.xuehao,stu.name,&stu.shudian,
     &stu.english,&stu.physics,&stu.cyuyan );
  head = insert( head,&stu );
  print( head );
  save( head );
return 0;
}

你可能感兴趣的:(c语言链表-学生成绩管理系统)