C语言 链表(三) 项目实战:学生管理系统单链表和文件操作通用框架

实现了学生管理系统数据的增删改查,数据存储等操作,并方便进行功能拓展。

根据函数名和变量名即可了解具体定义和功能。
有问题欢迎下方评论咨询。
编译器:VS2019

MyStudentsLibrary.cpp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include "MyStudentsLibrary.h"
#include "SystemTask.h"


int main()
{
    
    
	char num;

	Student* student_data = (Student *)malloc(sizeof(Student));

	student_info_library_create(student_data);

	while(1)
	{
    
    
		num = interface_show();
		
		if (parse(num) == task_num())
		{
    
    
			return 0;
		}
		else if (parse(num) >= 0 && parse(num) < task_num())
		{
    
    
			myTask.m_task[parse(num)](student_data);
		}
		else
		{
    
    
			printf("Input Wrong!!!\n");
			Sleep(1000);
		}

		system("cls");
	}
}

void student_info_library_create(Student* head)
{
    
    
	FILE* fRead = fopen("./WkData", "r");
	
	int stNum = 0;

	if (fRead)
	{
    
    
		printf("Welcome to Student Information Library!!!\n\n\n");

		//移动指针到文件尾
		fseek(fRead, 0L, SEEK_END);

		stNum = ftell(fRead) / (sizeof(Student));

		fread(head, sizeof(Student), stNum, fRead);

		fclose(fRead);
		return;
	}
	else
	{
    
    
		Student* student_data = head;
		FILE* fp = fopen("./WkData", "w+");

		record_students_information(student_data);

		if (fp)
		{
    
    
			fwrite(head, sizeof(Student), students_num(student_data), fp);
			fclose(fp);
			printf("\033[42;37m Save Success!\033[0m\n");
			Sleep(1000);
			system("cls");
		}
		else printf("\033[41;37m Save Failed!\033[0m\n");
	}
}

int parse(char num)
{
    
    
	return ((int)(num - '0') - 1);
}

int task_num()
{
    
    
	return (sizeof(task) / sizeof(Task));
}

int students_num(Student* head)
{
    
    
	Student* student_data = head;
	while (student_data->next != NULL)
	{
    
    
		student_data = student_data->next;
	}
	return student_data->num;
}

void record_students_information(Student * head)
{
    
    
	Student* student_data = head;
	int cnt = 0;
	int data = 0;
	system("cls");
	printf("How many student information do you want to input ?\n");
	scanf("%d", &data);
	system("cls");
	while (data--)
	{
    
    
		student_data->next = (Student*)malloc(sizeof(Student));

		student_data = student_data->next;

		printf("please input student name:\n");
		scanf("%s",student_data->name);

		printf("please input student age:\n");
		scanf("%d", &student_data->age);

		student_data->num = ++cnt;
	}
	student_data->next = NULL;

	show_students_information(head);
}

void show_students_information(Student* head)
{
    
    
	Student* student_data = head;
	printf("Name      Age       Num\n");
	while (student_data->next != NULL)
	{
    
    
		printf("%s %10d %10d\n", student_data->next->name, student_data->next->age, student_data->next->num);
		student_data = student_data->next;
	}
	Sleep(1000);
	system("cls");
}

void insert_ahead_students_information(Student* head)
{
    
    
	show_students_information(head);

	Student* student_data = head;
	//新节点
	Student* insert = (Student*)malloc(sizeof(Student));
	//存储旧节点
	Student* temp = (Student*)malloc(sizeof(Student));	
	char tem_name[10];

	printf("please input where you want to insert\n");
	scanf("%s", tem_name);
	system("cls");
	printf("please input the insert name\n");
	scanf("%s", insert->name);
	system("cls");
	printf("please input the insert age\n");
	scanf("%d", &insert->age);
	system("cls");
	while (student_data->next != NULL)
	{
    
    
		if (strcmp(student_data->next->name, tem_name) == 0)
		{
    
    

			temp=student_data->next;

			//新节点与前一个节点连接
			student_data->next = insert;
			
			//新节点与旧节点连接
			insert->next = temp;
			break;
		}
		student_data = student_data->next;
	}

	list_ranking(head);
	show_students_information(head);
}

void list_ranking(Student* head)
{
    
    
	Student* student_data = head;
	int num = 1;
	while (student_data->next != NULL)
	{
    
    
		student_data = student_data->next;
		student_data->num = num++;
	}
}

void insert_follow_students_information(Student* head)
{
    
    
	show_students_information(head);

	Student* student_data = head;
	Student* insert = (Student*)malloc(sizeof(Student));
	Student* temp = (Student*)malloc(sizeof(Student));
	char tem_name[10];
	int tmp_num = 0;
	
	printf("please input where you want to insert\n");
	scanf("%s", tem_name);
	system("cls");
	printf("please input the insert name\n");
	scanf("%s", insert->name);
	system("cls");
	printf("please input the insert age\n");
	scanf("%d", &insert->age);
	system("cls");
	while (student_data->next != NULL)
	{
    
    
		if (strcmp(student_data->next->name, tem_name) == 0)
		{
    
    
			if (student_data->next->next == NULL)
			{
    
    
				student_data->next->next = (Student*)malloc(sizeof(Student));
				student_data->next->next = insert;
				insert->next = NULL;
			}
			else
			{
    
    
				temp = student_data->next->next;
				student_data->next->next = insert;
				insert->next = temp;
			}
			break;
		}

		student_data = student_data->next;
	}

	list_ranking(head);

	show_students_information(head);
}


void search_students_information(Student* head)
{
    
    
	show_students_information(head);

	Student* student_data = head;
	system("cls");
	char tem_name[10];  
	printf("please input which student you want to find\n");
	scanf("%s", tem_name);
	system("cls");   
	while (student_data->next != NULL)
	{
    
    
		if (strcmp(student_data->next->name, tem_name) == 0)
		{
    
    
			printf("student name :%s\n", student_data->next->name);
			printf("student age :%d\n", student_data->next->age);
			printf("student num :%d\n", student_data->next->num);
			Sleep(1000);
		}
		student_data = student_data->next;
	}
}

void delete_students_information(Student* head)
{
    
    
	show_students_information(head);
	Student* student_data = head;
	Student* del_data = NULL;
	system("cls");
	char tem_name[10];
	printf("please input which student you want to delete\n");
	scanf("%s", tem_name);
	system("cls");
	while (student_data->next != NULL)
	{
    
    
		if (strcmp(student_data->next->name, tem_name) == 0)
		{
    
    
			del_data = student_data->next;
			//将要删除结点的前后节点相连
			student_data->next = student_data->next->next;
			free(del_data);
			printf("delete finish\n");
			Sleep(500);
			break;
		}
		student_data = student_data->next;
	}

	list_ranking(head);
	show_students_information(head);
}

void modify_students_information(Student* head)
{
    
    
	Student* student_data = head;
	Student* newValue = (Student*)malloc(sizeof(Student));
	//存放指定结点后一个结点
	Student* tmp1 = (Student*)malloc(sizeof(Student));

	printf("please input which num you want to modify\n");
	scanf("%d", &newValue->num);
	system("cls");
	printf("please input the insert name\n");
	scanf("%s", newValue->name);
	system("cls");
	printf("please input the insert age\n");
	scanf("%d", &newValue->age);
	system("cls");

	while (student_data->next != NULL)
	{
    
    
		if (student_data->next->num == newValue->num)
		{
    
    
			tmp1 = student_data->next;
			student_data->next = newValue;
			student_data->next->next = tmp1->next;
			break;
		}
		student_data = student_data->next;
	}

	list_ranking(head);
	show_students_information(head);
}

char interface_show()
{
    
    
	printf("Please :\n");
	for (size_t i = 0; i < task_num(); i++)
	{
    
    
		printf("%d.", i+1);
		printf("%s\n", myTask.m_InterfaceStr[i]);
	}
	printf("%d.Exit\n", task_num() +1);

	//清空缓存
	rewind(stdin);

	return getchar();
}


MyStudentsLibrary.h

#ifndef MYSTUDENTSLIBRARY_H
#define MYSTUDENTSLIBRARY_H

typedef struct Student {
    
    
	char name[10];
	int age;
	int num;
	Student* next;
}Student;

char interface_show();

void record_students_information( Student* student_data);

void show_students_information(Student* student_data);

void insert_ahead_students_information(Student* student_data);

void insert_follow_students_information(Student* student_data);

void search_students_information(Student* student_data);

void modify_students_information(Student* student_data);

void delete_students_information(Student* student_data);

void list_ranking(Student* student_data);

int students_num(Student* head);

void student_info_library_create(Student* head);

#endif // !1
#pragma once

MyStudentsLibrary.h

#pragma once
#ifndef SYSTEMTASK_H
#define SYSTEMTASK_H
#include "MyStudentsLibrary.h"

#define INTERFACE_SHOW 0

typedef void (*Task)(Student* student_data);

typedef struct MyTask
{
    
    
	Task* m_task;
	char m_InterfaceStr[][100];
}MyTask;

Task task[] =
{
    
    
	record_students_information ,
	show_students_information ,
	insert_ahead_students_information ,
	insert_follow_students_information ,
	modify_students_information,
	search_students_information ,
	delete_students_information
};

extern MyTask myTask = {
    
    
	task,
	{
    
    
	"Student Information input",
	"Show Student Information",
	"Insert student information from the front of the specified position",
	"Insert student information after the specified location",
	"Modify current student information",
	"Search student information",
	"Delete student information",
	}
};

int parse(char num);
int task_num();


#endif // !1
#pragma once

源码下载地址
在这里插入图片描述

承接C语言设计康难度而定只需1北左右。 有意➕微信 a923510073 QQ 923510073 欢迎咨询进行学习上的问题交流。

猜你喜欢

转载自blog.csdn.net/weixin_44291381/article/details/113710172