当前位置:首页 > 开发教程 > C语言 >

C语言实现新生入学登记系统

时间:2022-06-02 13:33 来源:未知 作者:玩人必被人玩 收藏

这篇文章主要为大家详细介绍了C语言实现新生入学登记系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现新生入学登记系统的具体代码,供大家参考,具体内容如下

项目所用数据结构:链表
算法:对链表数据的增删改查操作,冒泡排序
系统架构图:

C语言实现新生入学登记系统

项目文件结构:

C语言实现新生入学登记系统

(1)system.h

#ifndef SYSTEM_H_INCLUDED
#define SYSTEM_H_INCLUDED
//宏定义学生信息的一种表示形式
#define STUDENT_DATA pMove->studentData.studentId,pMove->studentData.name,pMove->studentData.sex,pMove->studentData.age,pMove->studentData.className,pMove->studentData.major,pMove->studentData.tel,pMove->studentData.score

#define STUDENT_RANKING stuRanking[j].studentId, stuRanking[j].name, stuRanking[j].className, stuRanking[j].score,stuRanking[j].ranking
struct student
{
   char studentId[15]; //学号
   char name[10];
   char sex[4];
   int age;
   char className[20]; //班级
   char major[20]; //专业
   char tel[15];
   int score;  //入学成绩
};

struct Node
{
   struct student studentData;
   struct Node* next;
};

struct studentRanking
{
  char studentId[15];
  char name[10];
  char className[20];
  int score;
  int ranking;

};
extern struct Node* studentList; //链表的头指针
#endif // SYSTEM_H_INCLUDED

(2)main.h

#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include "AddStudent.h"
#include "BeginingAndEnding.h"
#include "DeleteStudent.h"
#include "ModifyStudent.h"
#include "SearchStudent.h"
#include "ShowStudent.h"
#include "ShowStudentRanking.h"
#include "MyList.h"
#include "system.h"
void showMenu();

#endif // MAIN_H_INCLUDED

(3)main.c

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  //getc函数使用的头文件
#include "main.h"
//主函数
int main()
{
int selection;
Int ret;
  Begining();
  showMenu();   //展示界面
  ret = scanf("%d", &selection); //读入用户输入数字
getchar();
While(ret != 1)
{
  printf(“输入错误,请选择(0-6):”);
  ret = scanf("%d", &selection); //读入用户输入数字
}
  while (selection)
  {
    switch (selection)
    {
    case 1:
      AddStudent(); //录入学生信息
      break;
    case 2:
      ShowStudent(); //浏览学生信息
      break;
    case 3:
      SearchStudent(); //查找学生信息
      break;
    case 4:
      DeleteStudent(); //删除学生信息
      break;
    case 5:
      ModifyStudent();//修改学生信息
      break;
    case 6:
      ShowRanking(); //显示学生排名
      break;
    default:
      printf("\t\t请输入正确的数字!\n");
    }
    Ending(); //将链表数据写入文件
    printf("|按任意键返回系统菜单|");
    getch(); //接收用户输入的任意字符
    system("cls");
    showMenu();
    ret = scanf("%d", &selection); //提示用户输入数字
    getchar();
  While(ret != 1)
{
    printf(“输入错误,请选择(0-6):”);
    ret = scanf("%d", &selection); //读入用户输入数字
}

  }
  return 0;
}


void showMenu()
{

  printf("\n\n\n\n\n");
  printf("\t|--------------- 欢迎进入 ----------------|\n");
  printf("\t|       新生入学登记系统      |\n");
  printf("\t|         主菜单         |\n");
  printf("\t|      1. 录入学生信息       |\n");
  printf("\t|      2. 浏览学生信息       |\n");
  printf("\t|      3. 查找学生信息       |\n");
  printf("\t|      4. 删除学生信息       |\n");
  printf("\t|      5. 修改学生信息       |\n");
  printf("\t|      6. 新生入学排名       |\n");
  printf("\t|      0. 退出系统         |\n");
  printf("\t|-----------------------------------------|\n");
  printf("\n");
  printf("\t\t请选择(0-6):");
}

(4)BeginingAndEnding.h

#ifndef BEGININGANDENDING_H_INCLUDED
#define BEGININGANDENDING_H_INCLUDED
//关于启动函数和结束函数
void Begining();
void Ending();
#endif // BEGININGANDENDING_H_INCLUDED

(5)BeginingAndEnding.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include "system.h"
#include "AboutFiles.h"
#include "BeginingAndEnding.h"
#include "MyList.h"
void Begining()
{
   readInfoFromFile("studentList.txt");
}
void Ending()
{
   writeInfoToFile("studentList.txt");
}

(6)MyList.h

#ifndef MYLIST_H_INCLUDED
#define MYLIST_H_INCLUDED
//关于链表的函数声明
//创建节点
struct Node* createNode(struct student data);
//插入节点
void insertNodeByHead(struct student data);
//指定位置删除节点
void deleteAppointNode(char* studentId);
//查找功能
struct Node* searchInfoByData(char* studentId);
//打印链表
void printList();
#endif // MYLIST_H_INCLUDED

(7)MyList.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "system.h"
#include "MyList.h"

struct Node* studentList = NULL; //链表的头指针
//创建节点
struct Node* createNode(struct student studentData)
{
   struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
   if(newNode != NULL)
   {
      newNode->studentData = studentData;
      newNode->next = NULL;
   }
   return newNode;
}
//插入节点
void insertNodeByHead(struct student data)
{
   struct Node* newNode = createNode(data);
   //表头法插入,每次插入都将数据插入到头节点的下一个,先判断头节点是否为空,为空则新节点就是头节点,不为空,则插入在头节点的下一个位置
   if(studentList == NULL)
   {
      studentList = newNode;
   }
   else//不改变头节点
   {
      newNode->next = studentList->next;
      studentList->next = newNode;
   }

}

//指定位置删除节点(知道这个节点的前驱和后续,然后进行删除)
void deleteAppointNode( char* studentId)
{
  //将链表头部设为指定位置,先判断头节点是否为空,为空则链表没有数据,无法删除
  //如果头节点不为空,则设头结点为指定位置,如果头结点是所找的节点,则删除,如果不是,设头结点的下一个为指定节点,头结点为指定节点的前驱节点,一直向下查询
  //指定位置
  struct Node* posNode = studentList;
  //指定位置的前面
  struct Node* posFrontNode = NULL;
  //查找指定节点
  if(posNode == NULL)
  {
   printf("数据为空无法删除!\n");
    return;
  }
  else
  {
     //姓名是字符串,不能直接比较, strcmp(), 相等返回值为0,相同返回值为负数或者正数
    if(strcmp(posNode->studentData.studentId, studentId) ==0)
    {
      studentList = studentList->next;
      free(posNode);
      return;
    }
    else
    {
      posFrontNode = posNode;
      posNode = posNode->next;
      //posFrontNode = posNode; //!!
      while(strcmp(posNode->studentData.studentId, studentId))
      {
        //继续向下一个节点移动
        posFrontNode = posNode;
        posNode = posFrontNode->next;
        if(posNode == NULL) //找到了链表尾部,没有找到数据
        {
          printf("未找到指定位置,无法删除!");
          return;
        }
      }
      //查到到对应数据后,进行删除,将该节点架空,然后释放该节点的存储单元
      posFrontNode->next = posNode->next;
      free(posNode);
      return;
    }
  }
}
//查找功能
struct Node* searchInfoByData(char* studentId)
{
   struct Node* pMove;
   pMove = studentList;
   if(pMove == NULL) //头节点为空,链表为空
   {
      //printf("学生链表为空!\n");
      return pMove;
   }
   else
   {
      // 查找到或者查找到链表最后,则结束循环,返回查询结果,结果为空,有两种可能,一种是链表中没有数据,另一种是没有查询到
      while(pMove != NULL)
      {
         if(strcmp(pMove->studentData.studentId, studentId)==0)//找见
         {
            //printf("已查找到!\n");
            return pMove;
         }
         else
         {
            pMove = pMove->next;
         }

      }
      //printf("未找到!\n");
      return pMove;
   }
}

//打印链表
void printList()
{
   struct Node* pMove = studentList;
   //涉及到展示数据
   //表头
   if(pMove == NULL)
   {
      printf("No student record! Please add.\n");
      return;
   }
   else
   {
      printf("\t-------------------------------------------------------------------------------------\n");
printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","学号","姓名","性别","年龄","班级","专业","电话","入学成绩");
      printf("\t-------------------------------------------------------------------------------------\n");
while(pMove)
 {
   printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA);
         printf("\t-------------------------------------------------------------------------------------\n");
  pMove = pMove->next;
      }
   }
   printf("\n");
}

(8)AddStudent.h

#ifndef ADDSTUDENT_H_INCLUDED
#define ADDSTUDENT_H_INCLUDED
void AddStudent(); //录入学生信息
#endif // ADDSTUDENT_H_INCLUDED

(9)AddStudent.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "AddStudent.h"
#include "system.h"
#include "MyList.h"

//录入学生信息
void AddStudent()
{
  struct student studentData;
  struct Node* isNull = NULL; //接收查询的返回值
  int iFlagExist; //保证不重复输入
  char cFlag;
  int ret; //用来接收scanf的返回值,判断输入数据是否正确

  system("cls");
  printf("===================================【录入学生信息】===============================\n");

  printf("\n请选择是否输入学生信息(y/n):");
  cFlag = getchar();
  getchar();
  while(cFlag != 'n' && cFlag!='y')
  {
     printf("输入有误,请输入‘y'或者‘n'!");
     printf("\n请选择是否输入学生信息(y/n):");
     cFlag = getchar();
     getchar();
  }
  if (cFlag == 'n')
    return;

  //循环输入学生信息可输入一条也可多条输入
  while (cFlag == 'y')
  {
    printf("请输入学生学号:");
    do
    {
      iFlagExist = 0;
      gets(studentData.studentId);
      //对学生编号在链表中进行查询,对查询结果进行判断,如果存在则重新输入,不存在则继续
      isNull = searchInfoByData(studentData.studentId);
      if(isNull!= NULL) //可以查询到
      {
         printf("该学生已经存在请重新输入!\n");
         printf("请输入学生学号:");
         iFlagExist = 1;
      }
    } while (iFlagExist == 1);

    //添加学生信息
    printf("请输入学生姓名:");
    ret = scanf("%s",studentData.name);
    while(ret!=1)
    {
       printf("输入学生姓名有误,请重新输入!\n");
       printf("请输入学生姓名:");
       ret = scanf("%s",studentData.name);
    }
    getchar();
    printf("请输入学生性别(男-M,女-F):"); //这里采用防御式编程,如果不是M,F或者没有输入该项则重新输入
    while (gets(studentData.sex) != NULL)
    {
      if (strcmp(studentData.sex, "F")==0 || strcmp(studentData.sex, "M")==0)
        break;
      printf("错误,只能输入'F'或者'M',请重新输入\n");
      printf("请输入学生性别(男-M,女-F):");
    }

    printf("请输入学生年龄(15-25):");
    ret = scanf("%d", &studentData.age);
    while((ret != 1) || studentData.age<15 || studentData.age>25)
    {
       printf("输入年龄错误,请重新输入学生年龄(15-25):");
       ret = scanf("%d", &studentData.age);
    }

    getchar();
    printf("请输入学生班级(eg: B电子191):");
    gets(studentData.className);
    printf("请输入学生专业:");
    gets(studentData.major);
    printf("请输入学生电话:");
    gets(studentData.tel);

    printf("请输入学生入学成绩(200-750):");
    ret = scanf("%d", &studentData.score);
    while((ret != 1) || studentData.score<200 || studentData.score>750)
    {
       printf("输入成绩信息错误,请重新输入学生入学成绩(200-750):");
       ret = scanf("%d", &studentData.score);
    }
    getchar();

    insertNodeByHead(studentData);
    fflush(stdin);
    printf("继续输入信息吗(y/n):");
    cFlag = getchar();
    getchar();
    while(cFlag != 'n' && cFlag!='y')
    {
      printf("输入有误,请输入‘y'或者‘n'!");
      printf("\n请选择是否输入学生信息(y/n):");
      cFlag = getchar();
      getchar();
    }
  }
  printf("添加学生信息执行完毕!\n");
}

(10)ShowStudent.h

#ifndef SHOWSTUDENT_H_INCLUDED
#define SHOWSTUDENT_H_INCLUDED
void ShowStudent(); //查找学生信息
#endif // SHOWSTUDENT_H_INCLUDED

(11)ShowStudent.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "ShowStudent.h"
#include "system.h"
#include "MyList.h"
//浏览学生信息
void ShowStudent()
{
   system("cls");
   printf("\n");
   printf("\t====================================【浏览学生信息】================================\n");
   printf("\n\n");
   printList();

}

(12)SearchStudent.h

#ifndef SEARCHSTUDENT_H_INCLUDED
#define SEARCHSTUDENT_H_INCLUDED
void SearchStudent(); //查找学生信息
#endif // SEARCHSTUDENT_H_INCLUDED

(13)SearchStudent.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "SearchStudent.h"
#include "system.h"
#include "MyList.h"

//查找学生信息
void SearchStudent()
{
   //查询成功,则返回该学生信息,查询失败则输出提示信息,可重新输入,也可退出
   struct student studentData;
   struct Node* pMove = NULL; //用来接收查询返回的结果
   char cFlag; //接收用户的选择

   system("cls");
   printf("\n");
   printf("\t==================================【查找学生信息】==============================\n");

   printf("\t是否进行学生查询(y/n):");
   cFlag = getchar();
   getchar(); //接收回车键
   while(cFlag != 'n' && cFlag!='y')
   {
      printf("输入有误,请输入‘y'或者‘n'!");
      printf("\n请选择是否查询学生信息(y/n):");
      cFlag = getchar();
      getchar();
   }
   if (cFlag == 'n')
    return;

   while(cFlag == 'y')
   {
    printf("\t请输入需要查找的学生的学号:");
    //这里通过学号进行查询,学号是唯一的,姓名有重名现象
    gets(studentData.studentId);
    pMove = searchInfoByData(studentData.studentId);
    if(pMove) //pMove 为真时,表示查询到
    {
      printf("\t查询成功,以下为该学生信息:\n");
      printf("\t-------------------------------------------------------------------------------------\n");
      printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","学号","姓名","性别","年龄","班级","专业","电话","入学成绩");
      printf("\t-------------------------------------------------------------------------------------\n");
      printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA);
      printf("\t-------------------------------------------------------------------------------------\n");

    }
    else //pMove 为空时,未查询到,这里为空有两种情况,一种为学生名单为空,一种是没有查询到
    {
      printf("\t查询失败,该学生不存在或学生列表为空!\n");
      printf("\t是否重新查询(y/n):");
      cFlag = getchar();
      getchar();
      while(cFlag != 'n' && cFlag!='y')
      {
        printf("输入有误,请输入‘y'或者‘n'!");
        printf("\n是否重新查询学生信息(y/n):");
        cFlag = getchar();
        getchar();
      }
    }
   }
   printf("\t学生信息查询结束!\n");
}

(14)DeleteStudent.h

#ifndef DELETESTUDENT_H_INCLUDED
#define DELETESTUDENT_H_INCLUDED
void DeleteStudent(); //删除学生信息
#endif // DELETESTUDENT_H_INCLUDED

(15)DeleteStudent.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "DeleteStudent.h"
#include "system.h"
#include "MyList.h"
//删除学生信息
void DeleteStudent()
{
   //先根据学号对该学生进行查找,查找到则删除,没有查找到则输出提示信息
   struct student studentData;
   struct Node* pMove = NULL; //用来接收查询返回的结果
   char cFlag;

   system("cls");
   printf("\n");
   printf("\t==================================【删除学生信息】==============================\n");
   printf("\t请输入需要删除的学生的学号:");
   gets(studentData.studentId);
   pMove = searchInfoByData(studentData.studentId);
   if(pMove) //该学生存在,进行删除
   {
    //先对学生信息进行展示
    printf("\t-------------------------------------------------------------------------------------\n");
    printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","学号","姓名","性别","年龄","班级","专业","电话","入学成绩");
    printf("\t-------------------------------------------------------------------------------------\n");
    printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA);
    printf("\t-------------------------------------------------------------------------------------\n");
    printf("\t已查找到该学生信息,是否删除?(y/n)");
    cFlag = getchar();
    getchar(); //吃掉缓冲区的空格

    while(cFlag != 'n' && cFlag!='y')
    {
      printf("输入有误,请输入‘y'或者‘n'!");
      printf("\n请选择是否输入学生信息(y/n):");
      cFlag = getchar();
      getchar();
    }

    if(cFlag == 'n')
      return;
    else if(cFlag == 'y')
    {
       deleteAppointNode(studentData.studentId);
       printf("\t已删除该学生信息!\n");
       printf("\t删除操作执行结束!\n");
    }
   }
   else //找到了链表的末尾,或者链表为空
   {
    printf("\t该学生不存在!无法执行删除操作\n");
   }
}

(16)ModifyStudent.h

#ifndef MODIFYSTUDENT_H_INCLUDED
#define MODIFYSTUDENT_H_INCLUDED
#include "system.h"
void ModifyStudent();//修改学生信息
void ShowModifyMenu();//展示修改选项菜单
void dealSelection(struct student studentData, int selection, struct Node *pMove); //处理用户选择的修改项
#endif // MODIFYSTUDENT_H_INCLUDED

(17)ModifyStudent.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "ModifyStudent.h"
#include "system.h"
#include "MyList.h"

//修改学生信息
void ModifyStudent()
{
   struct student studentData;
   struct Node* pMove = NULL; //对学生信息查询结果进行保存
   int selection; //保存选择信息
   char isContinue = 'n'; //是否继续进行修改

   system("cls");
   printf("\n");
   printf("\t==================================【修改学生信息】==============================\n");

   printf("\t请输入需要修改信息的学生的学号:");
   gets(studentData.studentId);
   pMove = searchInfoByData(studentData.studentId);
   if(pMove == NULL)
   {
      printf("\t该学生信息不存在,无法进行信息修改\n");
      return;
   }
   else //可修改多条学生信息,也可以只修改一条学生信息
   {
      printf("\t-------------------------------------------------------------------------------------\n");
      printf("\t|%-10s |%-7s |%-4s |%-4s |%-12s |%-12s |%-12s |%-5s|\n","学号","姓名","性别","年龄","班级","专业","电话","入学成绩");
      printf("\t-------------------------------------------------------------------------------------\n");
      printf("\t|%-10s |%-7s |%-4s |%-4d |%-12s |%-12s |%-12s |%-8d|\n", STUDENT_DATA);
      printf("\t-------------------------------------------------------------------------------------\n");

      printf("\t是否进行学生信息的修改?(y/n)");
      scanf("%c", &isContinue);
      getchar();
      while(isContinue != 'n' && isContinue !='y')
      {
         printf("\t输入有误,请输入‘y'或者‘n'!");
         printf("\t请选择是否修改学生信息(y/n):");
         isContinue = getchar();
         getchar();
      }
      if(isContinue == 'n')
         return;
      else
      {
         while(isContinue == 'y')
         {
            //system('cls');
            ShowModifyMenu();
            //printf("\t请选择修改项: ");

            scanf("%d", &selection);
            getchar();
            //对用户的操作选择进行处理
            dealSelection(studentData,selection,pMove);
            fflush(stdin);
            printf("\t是否继续修改学生信息(y/n)?");
            isContinue = getchar();
            getchar();
            while(isContinue != 'n' && isContinue!='y')
            {
               printf("\n输入有误,请输入‘y'或者‘n'!");
               printf("\n请选择是否继续修改学生信息(y/n):");
               isContinue = getchar();
               getchar();
            }

         }
         printf("\t学生信息修改完毕!\n");
     }
   }

}

//学生信息修改菜单
void ShowModifyMenu()
{
  printf("\n");
  //printf("\t|       1.学号       |\n");
  printf("\t|       1.姓名       |\n");
  printf("\t|       2.性别       |\n");
  printf("\t|       3.年龄       |\n");
  printf("\t|       4.班级       |\n");
  printf("\t|       5.专业       |\n");
  printf("\t|       6.电话       |\n");
  printf("\t|       7.入学成绩     |\n");
  printf("\n");
  printf("请输入所要修改的信息(键入相应的数字:1-7):");
}

//处理用户选择的修改项
void dealSelection(struct student studentData, int selection, struct Node* pMove)
{
  int ret; //用来接收scanf的返回值
  switch (selection)
  {
  case 1:
    printf("\t请输入输入学生姓名:");
    gets(studentData.name);
    strcpy(pMove->studentData.name, studentData.name);
    break;

  case 2:
    printf("\t请输入学生性别(男-M,女-F):");
    while (gets(studentData.sex) != NULL)
    {
      if (strcmp(studentData.sex, "F") == 0 || strcmp(studentData.sex, "M") == 0)
        break;
      printf("\t错误,只能输入'F'或者'M',请重新输入\n");
      printf("\t请输入学生性别(男-M,女-F):");
    }
    strcpy(pMove->studentData.sex,studentData.sex);
    break;

  case 3:
    printf("\t请输入学生年龄(15-25):");
    ret = scanf("%d", &studentData.age);
    while((ret != 1) || studentData.age<15 || studentData.age>25)
    {
       printf("\t输入年龄错误,请重新输入学生年龄(15-25):");
       ret = scanf("%d", &studentData.age);
    }
    pMove->studentData.age = studentData.age;
    break;

  case 4:
    printf("\t请输入学生班级(eg:B电子191):");
    gets(studentData.className);
    strcpy(pMove->studentData.className, studentData.className);
    break;

  case 5:
    printf("\t请输入学生专业:");
    gets(studentData.major);
    strcpy(pMove->studentData.major, studentData.major);
    break;

  case 6:
    printf("\t请输入学生电话:");
    gets(studentData.tel);
    strcpy(pMove->studentData.tel, studentData.tel);
    break;

  case 7:
    printf("\t请输入学生入学成绩(100-750):");
    ret = scanf("%d", &studentData.score);
    while((ret != 1) || studentData.score<200 || studentData.score>750)
    {
       printf("\t输入成绩信息错误,请重新输入学生入学成绩(200-750):");
       ret = scanf("%d", &studentData.score);
    }
    pMove->studentData.score = studentData.score;
    break;

  default:
    printf("\t\t请输入正确的数字!");
    break;

  }
}

(18)ShowStudentRanking.h

#ifndef SHOWSTUDENTRANKING_H_INCLUDED
#define SHOWSTUDENTRANKING_H_INCLUDED
#include "system.h"
void ShowRanking(); //显示学生排名
void sortByScore(struct studentRanking * stuRanking, int length);
void Ranking(struct studentRanking * stuRanking, int length);
#endif // SHOWSTUDENTRANKING_H_INCLUDED

(19)ShowStudentRanking.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "ShowStudentRanking.h"
#include "system.h"

void ShowRanking()
{
   //*对链表中学生的成绩进行排名,并显示排名结果,排名结果括学号姓名班级专业入学成绩排名
   //*排名是struct studentRanking 的一个结构体成员,在得出排名后,再进行展示
   //*1.对链表中所有学生的成员进行排序,排序结果保存在student.ranking中
   //重新定义一个结构体,保存排名信息并输出
   //定义一个结构体数组
   struct Node *pMove = NULL;
   struct studentRanking *stuRanking;
   int i, j;
   int length = 0; //用来保存链表的长度

   system("cls");
   printf("\n");
   printf("\t==================================【学生排名信息】==============================\n");

   if(studentList == NULL)
   {
     printf("学生登记为空,无法进行成绩排名\n");
     return;
   }
   else //学生链表头指针不为空,代表学生链表中存有学生信息
   {
     pMove = studentList; //pMove指向链表的头
     //通过遍历得到链表有多少个存储单元
     for(i=0; pMove != NULL; i++)
     {
      pMove = pMove->next;
     }
     length = i;
     printf("现有学生总人数为%d\n", length);

     //动态数组
     stuRanking = (struct studentRanking *)malloc(length * sizeof(struct studentRanking));
     if(stuRanking == NULL)
      return;
     //将需要输出的学生信息复制到结构体数组当中
     pMove = studentList;
     for(j=0; j<length; j++)
     {
          strcpy(stuRanking[j].studentId, pMove->studentData.studentId);
          strcpy(stuRanking[j].name , pMove->studentData.name);
          strcpy(stuRanking[j].className , pMove->studentData.className);
          stuRanking[j].score = pMove->studentData.score;
          pMove = pMove->next;
     }
     //复制完成后,根据成绩对学生进行排序
     sortByScore(stuRanking, length);
     //根据排序结果,为每名同学添加排名信息
     Ranking(stuRanking, length);
     //展示排名
     printf("排名结果如下:\n");
     printf("\t-------------------------------------------------------\n");
     printf("\t|%-10s |%-7s |%-12s |%-5s |%-5s|\n","学号","姓名","班级","入学成绩","全级排名");
     printf("\t-------------------------------------------------------\n");

     for(j=0; j<length; j++)
     {
       printf("\t|%-10s |%-7s |%-12s |%-8d |%-8d|\n", STUDENT_RANKING);
       printf("\t-------------------------------------------------------\n");

     }

   }
   printf("输出排名信息完毕!\n");
   system("pause");
}

//通过成绩对链表中的数据进行排序
void sortByScore(struct studentRanking *stuRanking, int length)
{
  //进行冒泡排序,从大到小排序
  int i, j;
  struct studentRanking temp;
  for(i=0; i<length-1; i++)
  {
    for(j=0; j<(length-i-1); j++)
    {
      if(stuRanking[j].score < stuRanking[j+1].score)//后一项比前一项大,则交换两个存储单元中的数据,一轮排序下来,最小项就位,在列表的最末尾
      {
        temp = *(stuRanking+j);
        *(stuRanking+j) = *(stuRanking+j+1);
        *(stuRanking+j+1) =temp;
      }
    }

  }
}
void Ranking(struct studentRanking * stuRanking, int length)
{
   int i;
   for(i=1; i<=length; i++)
   {
      stuRanking[i-1].ranking = i;
   }
}

(20)AboutFiles.h

#ifndef ABOUTFILES_H_INCLUDED
#define ABOUTFILES_H_INCLUDED
//链表的读取--文件读操作
void readInfoFromFile(char* fileName);
//链表的存储--文件写操作
void writeInfoToFile(char* fileName);
#endif // ABOUTFILES_H_INCLUDED

(21)ShowStudentRanking.c

#include <stdio.h>
#include <conio.h>  //getc函数使用的头文件
#include <windows.h> //Sleep函数使用的头文件
#include <string.h>//strcmp函数使用的头文
#include "system.h"
#include "AboutFiles.h"
#include "MyList.h"

//链表的读取--文件读操作
void readInfoFromFile(char* fileName)
{
   //步骤:先将信息读到data里面,再将信息读到文件里面
   //1.打开文件
   FILE *fp;
   struct student data;
   fp = fopen(fileName, "r");

   if(fp == NULL)
   {
       fclose(fp);
       return NULL;

   }
   //2.读文件
   //格式化读取文件,没有读到文件结束标志,则一直读下去,读到的数据插入到链表里面
   else
   {
      while(fscanf(fp, "%s\t%s\t%s\t%d\t%s\t%s\t%s\t%d\n", data.studentId,data.name,data.sex,&data.age,data.className,data.major,data.tel,&data.score) != EOF)
      {
      //将文件中原来的数据插入到链表当中
         insertNodeByHead(data);
      }
     //3.关闭文件,
     fclose(fp);
   }

}


//链表的存储--文件写操作
void writeInfoToFile(char* fileName)
{
   //1.打开文件D:\CodeBlocks\codeblocks C project\StudentSystemDemo02\studentList
   FILE *fp;
   struct Node* pMove = studentList;
   fp = fopen(fileName, "w");
   if(fp == NULL)
   {
      //w+具有创建的功能,建立一个新文件可读可写
      fp = fopen(fileName, "w+");
      //可以给文件写入一个表头信息
   }
   //2.写文件, 按格式写入操作
   while(pMove != NULL)
   {
      fprintf(fp,"%s\t%s\t%s\t%d\t%s\t%s\t%s\t%d\n", STUDENT_DATA);
      pMove = pMove->next;
   }

   //3.关闭文件
   fclose(fp);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持源码搜藏网。


C语言阅读排行

最新文章