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

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

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

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

其中主要包含的知识点为单链表(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;
}


相关文章
|
机器学习/深度学习 IDE 开发工具
动物分类识别教程+分类释义+界面展示-1
动物分类识别教程+分类释义+界面展示-1
|
编解码 移动开发 前端开发
什么是前端,前端是什么?
什么是前端,前端是什么?
586 0
21:苹果和虫子2
21:苹果和虫子2
268 0
|
监控 关系型数据库 MySQL
使用pt-heartbeat监控主从复制延迟
    MySQL主从复制是MySQL 高可用架构中重要的组成部分,该技术可以用于实现负载均衡,高可用和故障切换,以及提供备份等等。对于主从复制的监控,仅仅依赖于MySQL自身提供的show slave status并不可靠。
1215 0
|
2天前
|
云安全 数据采集 人工智能
古茗联名引爆全网,阿里云三层防护助力对抗黑产
阿里云三层校验+风险识别,为古茗每一杯奶茶保驾护航!
古茗联名引爆全网,阿里云三层防护助力对抗黑产
|
6天前
|
人工智能 中间件 API
AutoGen for .NET - 架构学习指南
《AutoGen for .NET 架构学习指南》系统解析微软多智能体框架,涵盖新旧双架构、核心设计、技术栈与实战路径,助你从入门到精通,构建分布式AI协同系统。
308 142
|
2天前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
411 0

热门文章

最新文章