学生成绩管理系统-单链表形式

简介: 学生成绩管理系统-单链表形式

(创作不易,感谢有你,你的支持,就是我前行的最大动力,如果看完对你有帮助,请留下您的足迹)

今天我们来试着用链表创建一个学生成绩管理系统

其中主要包含的知识点为单链表(http://t.csdn.cn/gsAdH)和C语言文件操作(http://t.csdn.cn/j3kwe) 相关知识点的链接我已经给出

总体代码就是这样,如果大家发现bug或者有更好的方法 ,欢迎大家一起来讨论呀,一起加油

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
//学生信息
typedef struct student
{
  char stuNum[20];
  char name[20];
  int score;
}Student;
//节点信息
typedef struct node
{
  Student student;
  struct node* next;
}node;
void welcome()
{
  printf("****  链式学生管理系统  *******\n");
  printf("*******************************\n");
  printf("****0.退出功能 1.录入功能******\n");
  printf("****2.打印功能 3.统计功能******\n");
  printf("****4.查找功能 5.修改功能******\n");
  printf("****6.删除功能 7.排序功能******\n");
  printf("*******************************\n");
}
//保存学生信息
void savestudent(node* head)
{
  //打开文件
  FILE* file = fopen("C:\\Users\\周星辰\\Desktop\\学生成绩管理系统.txt", "w");
  if (file == NULL)
  {
    printf("打开文件失败\n");
    return;
  }
  node* move = head->next;
  while (move != NULL)
  {
    //写入文件
    if (fwrite(&move->student, sizeof(student), 1, file) != 1)
    {
      printf("保存%s出现错误\n", move->student.name);
      return;
    }
    move = move->next;
  }
  //关闭文件
  fclose(file);
}
//录入学生信息
void inputstudent(node*head)
{
  node* fresh = (node*)malloc(sizeof(node));
  fresh->next = NULL;
  printf("请输入学生学号\n");
  scanf("%s", fresh->student.stuNum);
  printf("请输入学生姓名\n");
  scanf("%s", fresh->student.name);
  printf("请输入学生成绩\n");
  scanf("%d", &fresh->student.score);
  node* move = head;
  while (move->next != NULL)
  {
    move = move->next;
  }
  //将学生插入到尾部
  move->next = fresh;
  savestudent(head);
  system("cls");//清屏
  printf("学生信息录入成功\n");
  system("pause");//暂停程序
  system("cls");//清屏
}
//打印学生信息
void printStudent(node* head)
{
  node* move = head->next;
  printf("%10s\t%10s\t%10s\n","学号","姓名","成绩");
  while (move != NULL)
  {
    /*printf("学号:%s 姓名:%s 成绩:%d\n",
      move->student.stuNum,
      move->student.name,
      move->student.score);*/
    printf("%10s\t%10s\t%10d\n",
      move->student.stuNum,
      move->student.name,
      move->student.score);
    move = move->next;
  }
  system("pause");//暂停程序
  system("cls");//清屏
}
//统计总人数
void countstudent(node* head)
{
  node* move = head->next;
  int count = 0;
  while (move != NULL)
  {
    count++;
    move = move->next;
  }
  printf("学生总人数为:%d\n", count);
  system("pause");//暂停程序
  system("cls");//清屏
}
//查找学生信息
void findstudent(node* head)
{
  printf("请输入学生的学号:\n");
  char a[20];
  scanf("%s", a);
  node* move = head->next;
  while (move != NULL)
  {
    if (strcmp(move->student.stuNum, a) == 0)
    {
      printf("%10s\t%10s\t%10s\n", "学号", "姓名", "成绩");
      printf("%10s\t%10s\t%10d\n",
        move->student.stuNum,
        move->student.name,
        move->student.score);
      system("pause");//暂停程序
      system("cls");//清屏
      return;
    }
    move = move->next;
  }
  printf("未找到学生信息\n");
  system("pause");//暂停程序
  system("cls");//清屏
}
//读取学生信息
void loadstudent(node* head)
{
  //打开文件
  FILE* file = fopen("C:\\Users\\周星辰\\Desktop\\学生成绩管理系统.txt", "r");
  if (!file)
  {
    printf("未找到学生文件\n");
    return;
  }
  //创建一个节点
  node* fresh = (node*)malloc(sizeof(node));
  fresh->next = NULL;
  node* move = head;
  while (fread(&fresh->student, sizeof(student), 1, file) == 1)
  {
    move->next = fresh;
    move = fresh;
    fresh = (node*)malloc(sizeof(node));
    fresh->next = NULL;
  }
  //最后多定义一个fresh,要将它释放掉
  free(fresh);
  //关闭文件
  fclose(file);
}
//修改学生信息
void modifystudent(node* head)
{
  printf("请输入要修改的学生的学号:");
  char a[20];
  scanf("%s", &a);
  node* move = head->next;
  while (move != NULL)
  {
    if (strcmp(move->student.stuNum, a) == 0)
    {
      printf("请输入学生学号\n");
      scanf("%s", move->student.stuNum);
      printf("请输入学生姓名\n");
      scanf("%s", move->student.name);
      printf("请输入学生成绩\n");
      scanf("%d", &move->student.score);
      savestudent(head);
      printf("修改成功\n");
      system("pause");//暂停程序
      system("cls");//清屏
      return;
    }
    move = move->next;
  }
  printf("未找到学生信息\n");
  system("pause");//暂停程序
  system("cls");//清屏
}
//删除学生信息
void deletestudent(node* head)
{
  printf("输入要删除学生学号:\n");
  char a[20];
  scanf("%s", &a);
  node* move = head;
  while (move->next != NULL)
  {
    if (strcmp(move->next->student.stuNum, a) == 0)
    {
      node* tmp = move->next;
      move->next = move->next->next;
      free(tmp);
      tmp = NULL;
      savestudent(head);
      printf("删除成功\n");
      system("pause");//暂停程序
      system("cls");//清屏
      return;
    }
    move = move->next;
  }
  printf("未找到学生信息\n");
  system("pause");//暂停程序
  system("cls");//清屏
}
//排序
void sortstudent(node* head)
{
  for (node* turn = head->next; turn->next != NULL; turn = turn->next)
  {
    for (node* move = head->next; move->next != NULL; move = move->next)
    {
      if (move->student.score < move->next->student.score)
      {
        student temp = move->student;
        move->student = move->next->student;
        move->next->student = temp;
      }
    }
  }
  printStudent(head);
}
int main()
{
  //创建头节点
  node* head = (node*)malloc(sizeof(node));
  head->next = NULL;
  loadstudent(head);
  while (1)
  {
    welcome();
    char c = _getch();
    switch (c)
    {
    case '1':
      inputstudent(head);
      break;
    case '2':
      printStudent(head);
      break;
    case '3':
      countstudent(head);
      break;
    case '4':
      findstudent(head);
      break;
    case '5':
      modifystudent(head);
      break;
    case '6':
      deletestudent(head);
      break;
    case '7':
      sortstudent(head);
      break;
    case '0':
      printf("退出程序\n");
      exit(0);
      break;
    default:
      printf("输入错误\n");
      break;
    }
  }
  return 0;
}


相关文章
|
3月前
|
存储 C语言
深入解析C语言的动态数据类型单项链表技术
深入解析C语言的动态数据类型单项链表技术
34 0
|
数据库 C语言
C语言加链表实现学生信息管理系统
用C语言加链表的知识实现简单的学生信息管理系统。(可以自己完善添加数据库,文件流等操作)
87 0
|
8月前
|
存储 Java C++
数据结构单链表之C 中的通用链表 | 第十六套
数据结构单链表之C 中的通用链表 | 第十六套
45 1
|
8月前
|
存储 C++
数据结构单链表之将链表表示的两个数字相加 | 第十四套
数据结构单链表之将链表表示的两个数字相加 | 第十四套
33 0
|
11月前
|
C语言
C语言操作excel表格-链表实现
C语言操作excel表格-链表实现
100 0
|
Java
数组、集合、链表实现学生成绩管理系统
数组、集合、链表实现学生成绩管理系统
55 0
|
索引
基于链表实现的学生管理系统
基于链表实现的学生管理系统
65 0
|
存储 算法
我爱啃书--链表的特殊形式和总结(大话数据结构)
我爱啃书--链表的特殊形式和总结(大话数据结构)
77 0
|
数据可视化 C语言 C++
数据结构实验报告—顺序表的基本操作—学生管理系统
数据结构实验报告—顺序表的基本操作—学生管理系统
299 0