当前位置:网站首页>C language student management system based on linked list, super detailed
C language student management system based on linked list, super detailed
2022-07-03 08:39:00 【Zoe_ Cpp】
Student management system based on linked list
Student management system based on linked list
Preface
It's almost the end of freshman ,C The language class is over , Final assignment student management system , In the process of writing code, add some innovations on the basis of teachers' lectures , Write code before and after to change bug Almost three or four days , Unexpectedly, it was written , Gained a lot of experience . Two words don't say much, directly on the code , Remember to like collection !
function
- Import the file into the program linked list
- Save to file through program linked list
- The main menu displays
- Enter student list information
- Add student list
- Add individual student information
- Modify a student's information
- Delete a student's information
- Sort students' grades and output
- Sort students' student numbers and output
The whole idea
Use the linked list to establish , add to , Delete to read in and output information
Using the merging and sorting algorithm of linked list ( Fast ) To sort the linked list
Add some fault-tolerant mechanisms ,,,
Manage the project through function modularization
Through the main menu help() Function to include all functions
XD
Function.h
#pragma once
/*************** * Student data * Number id * full name name * fraction score * Next student data ****************/
struct Person
{
int id;
char name[20];
int score;
struct Person* next;
};
// Store the linked list in the file
void saveFile();
// Read data from the file to the linked list
struct Person* loadFile();
// The main menu displays
int help();
// Query a student's information
void searchInfo();
// Modify a student's information
void modifyInfo();
// Merge the two student information lists according to their grades
struct Person* mergeScoreList(struct Person* node1, struct Person* node2);
// Merge the two student information lists in order by student number
struct Person* mergeIdList(struct Person* node1, struct Person* node2);
// Sort student information by grades
struct Person* sortScoreList(struct Person* head);
// Sort student information by student number
struct Person* sortIdList(struct Person* head);
// Delete student information
void deleteInfo();
// Add student information
void addInfo();
// Add student information list
void addInfoList();
// Show the student information list
void displayList();
Function.cpp
#include "Function.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern struct Person* head;
void saveFile()
{
FILE* fp;
struct Person* p1 = head;
if ((fp = fopen("Student.txt", "w")) == NULL)
{
printf("File set failed!\n");
return;
}
while (p1 != NULL)
{
fprintf(fp, "%d\t%s\t%d\n", p1->id, p1->name, p1->score);
p1 = p1->next;
}
fclose(fp);
}
struct Person* loadFile()
{
int i = 0;
char tname[20];
int tid;
int tscore;
FILE* fp;
Person* p1 = NULL;
Person* p2 = NULL;
Person* head = NULL;
if ((fp = fopen("Student.txt", "r")) == NULL)
{
printf("File set failed!\n");
return NULL;
}
while (fscanf(fp, "%d%s%d", &tid, tname, &tscore) != EOF)
{
p1 = (Person*)malloc(sizeof(Person));
p1->next = NULL;
p1->id = tid;
strcpy(p1->name, tname);
p1->score = tscore;
if (i == 0)
{
head = p1;
p2 = p1;
i = 1;
}
else
{
p2->next = p1;
p2 = p1;
}
}
return head;
}
void searchInfo()
{
int tid;
struct Person* p1 = head;
printf(" The student ID you prefer to inquire is ?\n");
scanf("%d", &tid);
while (p1 != NULL)
{
if (p1->id == tid)
{
printf(" Student number \t full name \t achievement \n");
printf("%d\t%s\t%d\n", p1->id, p1->name, p1->score);
break;
}
p1 = p1->next;
}
if (p1 == NULL)
printf(" The student number was not found !\n");
}
void deleteInfo()
{
int tid;
int i;
char tch;
struct Person* p1 = head;
struct Person* tp = head;
printf(" The student ID you want to delete is ?\n");
scanf("%d", &tid);
getchar();
for (i = 0;; i++)
{
if (i == 0 && p1->id == tid)
{
printf(" Confirm to delete the name as %s Information about ?(Y\\N)\n", p1->name);
tch = getchar();
getchar();
if (tch == 'Y')
{
head = head->next;
free(p1);
printf(" Delete successful \n");
break;
}
else
{
printf(" Delete failed \n");
break;
}
}
if (p1 == NULL)
{
printf(" The student number was not found \n");
}
if (p1->id == tid)
{
printf(" Confirm to delete the name as %s Information about ?(Y\\N)\n", p1->name);
tch = getchar();
getchar();
if (tch == 'Y')
{
tp->next = p1->next;
free(p1);
printf(" Delete successful \n");
break;
}
else
{
printf(" Delete failed \n");
break;
}
}
if (i != 0)
{
tp = tp->next;
}
p1 = p1->next;
}
}
void addInfo()
{
char tch;
struct Person* node = (Person*)malloc(sizeof(Person));
struct Person* tp = head;
printf(" Enter the student number of the added student :");
scanf("%d", &node->id);
while (tp != NULL)
{
if (tp->id == node->id)
{
printf(" The student number already exists \n");
return;
}
tp = tp->next;
}
printf(" Enter the name of the added student :");
scanf("%s", node->name);
printf(" Enter the grade of the added student :");
scanf("%d", &node->score);
getchar();
printf(" Undo the operation just now ?(Y\\N)");
tch = getchar();
if (tch == 'Y')
{
printf(" Operation canceled \n");
return;
}
node->next = head;
head = node;
printf(" Add success \n");
}
void addInfoList()
{
char tch;
int tid;
char tname[20];
int tscore;
struct Person* tp = NULL;
struct Person* p1 = head;
printf(" Enter the student list :\n The student number entered is 0 End of input \n");
for (int i = 1;; i++)
{
tp = (struct Person*)malloc(sizeof(Person));
printf(" Enter the first %d Student number of students :", i);
scanf("%d", &tid);
if (tid == 0)
{
free(tp);
tp = NULL;
printf(" End of entering student information , Total entry %d Student information \n", i - 1);
return;
}
while (p1 != NULL)
{
if (p1->id == tid)
{
printf(" The student number already exists \n");
return;
}
p1 = p1->next;
}
printf(" Enter the first %d The names of the students :", i);
scanf("%s", tname);
printf(" Enter the first %d Results of students :", i);
scanf("%d", &tscore);
tp->id = tid;
strcpy(tp->name, tname);
tp->score = tscore;
tp->next = head;
head = tp;
}
}
void modifyInfo()
{
int tid;
struct Person* p1 = head;
printf(" Enter the modified student ID :");
scanf("%d", &tid);
while (p1 != NULL)
{
if (p1->id == tid)
{
printf(" Student information \n Student number \t full name \t achievement \n");
printf("%d\t%s\t%d\n", p1->id, p1->name, p1->score);
printf(" Change the name to :");
scanf("%s", p1->name);
printf(" Modify the score as :");
scanf("%d", &p1->score);
printf(" The information was modified successfully \n");
break;
}
p1 = p1->next;
}
if (p1 == NULL)
printf(" The student number was not found \n");
}
struct Person* mergeIdList(struct Person* node1, struct Person* node2)
{
if (node1 == NULL)
return node2;
if (node2 == NULL)
return node1;
if (node1->id < node2->id)
{
node1->next = mergeIdList(node1->next, node2);
return node1;
}
if (node1->id >= node2->id)
{
node2->next = mergeIdList(node1, node2->next);
return node2;
}
}
struct Person* sortIdList(struct Person* node)
{
struct Person* p1 = node;
struct Person* p2 = node;
struct Person* tp = node;
struct Person* tp2 = NULL;
struct Person* tp1 = NULL;
if (node->next == NULL)
return node;
for (int i = 0; p1 != NULL && p1->next != NULL; i++)
{
p1 = p1->next->next;
p2 = p2->next;
if (i != 0)
tp = tp->next;
}
tp->next = NULL;
tp2 = sortIdList(p2);
tp1 = sortIdList(node);
return mergeIdList(tp1, tp2);
}
struct Person* mergeScoreList(struct Person* node1, struct Person* node2)
{
if (node1 == NULL)
return node2;
if (node2 == NULL)
return node1;
if (node1->score < node2->score)
{
node1->next = mergeScoreList(node1->next, node2);
return node1;
}
if (node1->score >= node2->score)
{
node2->next = mergeScoreList(node1, node2->next);
return node2;
}
}
struct Person* sortScoreList(struct Person* node)
{
struct Person* p1 = node;
struct Person* p2 = node;
struct Person* tp = node;
struct Person* tp2 = NULL;
struct Person* tp1 = NULL;
if (node->next == NULL)
return node;
for (int i = 0; p1 != NULL && p1->next != NULL; i++)
{
p1 = p1->next->next;
p2 = p2->next;
if (i != 0)
tp = tp->next;
}
tp->next = NULL;
tp2 = sortScoreList(p2);
tp1 = sortScoreList(node);
return mergeScoreList(tp1, tp2);
}
void displayList()
{
struct Person* p1 = head;
printf(" Student number \t full name \t achievement \n");
while (p1 != NULL)
{
printf("%d\t%s\t%d\n", p1->id, p1->name, p1->score);
p1 = p1->next;
}
}
int help()
{
char choice;
printf("***************************************************************\n");
printf("* Student management system *\n");
printf("* Output an ascending list of grades Input :A *\n");
printf("* Output the ascending list of student numbers Input :B *\n");
printf("* Add a new student information list Input :C *\n");
printf("* Add new student information Input :D *\n");
printf("* Modify student information Input :E *\n");
printf("* Search for student information Input :F *\n");
printf("* Delete student information Input :G *\n");
printf("* Exit the menu interface and save Input :H *\n");
printf("* *\n");
printf("* *\n");
printf("***************************************************************\n");
scanf("%c", &choice);
system("CLS");
switch (choice)
{
case 'A':
head = sortScoreList(head); displayList(); break;
case 'B':
head = sortIdList(head); displayList(); break;
case 'C':
addInfoList(); break;
case 'D':
addInfo(); break;
case 'E':
modifyInfo(); break;
case 'F':
searchInfo(); break;
case 'G':
deleteInfo(); break;
case 'H':
saveFile(); return 0;
default:
return 1;
}
return 1;
}
Master file StudentManageSystem.cpp
#include <stdio.h>
#include <windows.h>
#include "Function.h"
struct Person* head = NULL;
int main()
{
head = loadFile();
while (help())
{
system("pause");
getchar();
system("CLS");
}
return 0;
}
Detailed description of each function
preservation
adopt fp File pointer opens a file , utilize p1 The pointer traverses from the beginning to the end of the linked list , Each time you traverse an element, the linked list information is saved to a file
void saveFile()
{
FILE* fp;
struct Person* p1 = head;
if ((fp = fopen("Student.txt", "w")) == NULL)
{
printf("File set failed!\n");
return;
}
while (p1 != NULL)
{
fprintf(fp, "%d\t%s\t%d\n", p1->id, p1->name, p1->score);
p1 = p1->next;
}
fclose(fp);
}
Read
Open the file in read-only mode with file pointer ,p1 Open up space from the stacking area , Read the information of the file into p1 Pointer , Loop open linked list , Return the head pointer of the linked list
struct Person* loadFile()
{
int i = 0;
char tname[20];
int tid;
int tscore;
FILE* fp;
struct Person* p1 = NULL;
struct Person* p2 = NULL;
struct Person* head = NULL;
if ((fp = fopen("Student.txt", "r")) == NULL)
{
printf("File set failed!\n");
return NULL;
}
while (fscanf(fp, "%d%s%d", &tid, tname, &tscore) != EOF)
{
p1 = (struct Person*)malloc(sizeof(struct Person));
p1->next = NULL;
p1->id = tid;
strcpy(p1->name, tname);
p1->score = tscore;
if (i == 0)
{
head = p1;
p2 = p1;
i = 1;
}
else
{
p2->next = p1;
p2 = p1;
}
}
return head;
}
lookup
Enter student number with keyboard , The pointer starts from the beginning of the linked list , Output and stop when the target student number is found , If you search to the end and don't find ( The pointer ends with NULL), The student number is not found in the output
void searchInfo()
{
int tid;
struct Person* p1 = head;
printf(" The student ID you prefer to inquire is ?\n");
scanf("%d", &tid);
while (p1 != NULL)
{
if (p1->id == tid)
{
printf(" Student number \t full name \t achievement \n");
printf("%d\t%s\t%d\n", p1->id, p1->name, p1->score);
break;
}
p1 = p1->next;
}
if (p1 == NULL)
printf(" The student number was not found !\n");
}
add to
Apply for a new memory , To save new student information , Finally, use header interpolation to link information to the list
If the student ID is repeated , Then the output student number already exists and exits ( After all, there is only one student number ,,)
If the information you just input is wrong , You can choose to cancel ( Hommization )
void addInfo()
{
char tch;
struct Person* node = (struct Person*)malloc(sizeof(struct Person));
struct Person* tp = head;
printf(" Enter the student number of the added student :");
scanf("%d", &node->id);
while (tp != NULL)
{
if (tp->id == node->id)
{
printf(" The student number already exists \n");
return;
}
tp = tp->next;
}
printf(" Enter the name of the added student :");
scanf("%s", node->name);
printf(" Enter the grade of the added student :");
scanf("%d", &node->score);
getchar();
printf(" Undo the operation just now ?(Y\\N)");
tch = getchar();
if (tch == 'Y')
{
printf(" Operation canceled \n");
return;
}
node->next = head;
head = node;
printf(" Add success \n");
}
Add list
Just like adding functions , It's just that you can add a lot of student information continuously
void addInfoList()
{
char tch;
int tid;
char tname[20];
int tscore;
struct Person* tp = NULL;
struct Person* p1 = head;
printf(" Enter the student list :\n The student number entered is 0 End of input \n");
for (int i = 1;; i++)
{
tp = (struct Person*)malloc(sizeof(struct Person));
printf(" Enter the first %d Student number of students :", i);
scanf("%d", &tid);
if (tid == 0)
{
free(tp);
tp = NULL;
printf(" End of entering student information , Total entry %d Student information \n", i - 1);
return;
}
while (p1 != NULL)
{
if (p1->id == tid)
{
printf(" The student number already exists \n");
return;
}
p1 = p1->next;
}
printf(" Enter the first %d The names of the students :", i);
scanf("%s", tname);
printf(" Enter the first %d Results of students :", i);
scanf("%d", &tscore);
tp->id = tid;
strcpy(tp->name, tname);
tp->score = tscore;
tp->next = head;
head = tp;
}
}
modify
Similar to lookup function , Find information that can be modified
void modifyInfo()
{
int tid;
struct Person* p1 = head;
printf(" Enter the modified student ID :");
scanf("%d", &tid);
while (p1 != NULL)
{
if (p1->id == tid)
{
printf(" Student information \n Student number \t full name \t achievement \n");
printf("%d\t%s\t%d\n", p1->id, p1->name, p1->score);
printf(" Change the name to :");
scanf("%s", p1->name);
printf(" Modify the score as :");
scanf("%d", &p1->score);
printf(" The information was modified successfully \n");
break;
}
p1 = p1->next;
}
if (p1 == NULL)
printf(" The student number was not found \n");
}
Orderly merging of linked lists ( Make a foundation for the linked list sorting function )
Recursion ,,
Recursive : Current list = Nodes with small values Point to The rest of the linked list
struct Person* mergeIdList(struct Person* node1, struct Person* node2)
{
if (node1 == NULL)
return node2;
if (node2 == NULL)
return node1;
if (node1->id < node2->id)
{
node1->next = mergeIdList(node1->next, node2);
return node1;
}
if (node1->id >= node2->id)
{
node2->next = mergeIdList(node1, node2->next);
return node2;
}
}
List sorting
Use fast pointer and slow pointer to find the middle node of the linked list , then ,, Split the list
Continue to recursively split the two linked lists , Until minimum
Merge the two linked lists in order
struct Person* sortIdList(struct Person* node)
{
struct Person* p1 = node;
struct Person* p2 = node;
struct Person* tp = node;
struct Person* tp2 = NULL;
struct Person* tp1 = NULL;
if (node->next == NULL)
return node;
for (int i = 0; p1 != NULL && p1->next != NULL; i++)
{
p1 = p1->next->next;
p2 = p2->next;
if (i != 0)
tp = tp->next;
}
tp->next = NULL;
tp2 = sortIdList(p2);
tp1 = sortIdList(node);
return mergeIdList(tp1, tp2);
}
Output
void displayList()
{
struct Person* p1 = head;
printf(" Student number \t full name \t achievement \n");
while (p1 != NULL)
{
printf("%d\t%s\t%d\n", p1->id, p1->name, p1->score);
p1 = p1->next;
}
}
In the help menu
Output help interface , use switch Modular processing of data , Return after each execution 1, Save and return every time you exit 0
int help()
{
char choice;
printf("***************************************************************\n");
printf("* Student management system *\n");
printf("* Output an ascending list of grades Input :A *\n");
printf("* Output the ascending list of student numbers Input :B *\n");
printf("* Add a new student information list Input :C *\n");
printf("* Add new student information Input :D *\n");
printf("* Modify student information Input :E *\n");
printf("* Search for student information Input :F *\n");
printf("* Delete student information Input :G *\n");
printf("* Exit the menu interface and save Input :H *\n");
printf("* *\n");
printf("* *\n");
printf("***************************************************************\n");
scanf("%c", &choice);
system("CLS");
switch (choice)
{
case 'A':
head = sortScoreList(head); displayList(); break;
case 'B':
head = sortIdList(head); displayList(); break;
case 'C':
addInfoList(); break;
case 'D':
addInfo(); break;
case 'E':
modifyInfo(); break;
case 'F':
searchInfo(); break;
case 'G':
deleteInfo(); break;
case 'H':
saveFile(); return 0;
default:
return 1;
}
return 1;
}
The main function
loop help(),help() return 0 Exit from time
int main()
{
head = loadFile();
while (help())
{
system("pause");
getchar();
system("CLS");
}
return 0;
}
边栏推荐
- Golang string segmentation, substitution and interception
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- 分配异常的servlet
- Graphics_ Games101/202 learning notes
- Servlet的生命周期
- MySQL containerization (1) docker installation MySQL
- [MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
- Sequence of map implementation classes
- Unity editor expansion - controls, layouts
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
猜你喜欢

Advanced OSG collision detection

Vscode, idea, VIM development tool shortcut keys

matlab神经网络所有传递函数(激活函数)公式详解

单调栈-503. 下一个更大元素 II

C course design employee information management system

Jupyter remote server configuration and server startup

十六进制编码简介

Visual Studio (VS) shortcut keys

Data analysis exercises

Simply start with the essence and principle of SOM neural network
随机推荐
[concurrent programming] Table hopping and blocking queue
Exe file running window embedding QT window
Explain sizeof, strlen, pointer, array and other combination questions in detail
LinkedList set
分配异常的servlet
Eating fruit
swagger文档配置
Base64 and base64url
796 · unlock
【音视频】ijkplayer错误码
[updating] wechat applet learning notes_ three
UE4 source code reading_ Bone model and animation system_ Animation process
图像处理8-CNN图像分类
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
[rust notes] 07 structure
Mall management system of database application technology course design
【更新中】微信小程序学习笔记_3
UE4 source code reading_ Bone model and animation system_ Animation node
[rust notes] 02 ownership
Unity Editor Extension - event handling