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;
}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种非常流行的编程语言,它拥有强大的数据结构和操作能力,非常适合用来实现学生成绩管理系统链表是一种常用的数据结构,可以动态地添加、删除和查找数据,非常适合存储学生成绩信息。 首先,我们需要定义一个链表结构体,用来存储学生成绩的信息。结构体包括学生姓名、学生学号和学生成绩等字段。 然后,我们可以定义一些操作链表的函数,例如添加学生成绩、删除学生成绩和查询学生成绩等。 添加学生成绩的函数可以接受用户输入的学生信息,并将其添加到链表中。首先创建一个新的节点,并将用户输入的学生信息存储到节点的字段中。然后将新节点插入到链表的末尾或指定的位置。 删除学生成绩的函数可以接受用户输入的学生学号,并在链表中查找该学生,然后将其删除。首先从链表的头节点开始遍历,找到要删除的节点,然后将其从链表中移除,并释放该节点的内存。 查询学生成绩的函数可以接受用户输入的学生学号,并在链表中查找该学生,并将学生的信息显示出来。首先从链表的头节点开始遍历,找到要查询的节点,然后将节点的字段信息显示给用户。 最后,我们可以编写一个主函数来接受用户的操作指令,并调用相应的函数来实现学生成绩管理系统。用户可以选择添加学生信息、删除学生信息、查询学生信息等操作。 以上就是用C语言链表实现学生成绩管理系统的简要过程。当然,在实际开发中还需要考虑异常情况的处理、数据的持久化存储等问题,希望能对你有所启发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值