Student management system c language coursework (linked list implementation)

student management system

Function:

1. Login interface:

  1. Provide login interface and registration interface
  2. Check the files saved by the system when logging in. If there is no such user name or the password is wrong, you can only try again.
  3. Registration can only be a user permission, and the user name cannot overlap with known users, there is an anonymous input function, and it is required to repeatedly enter the password when registering.

2. User interface:

  1. Provide search by ID and search by name functions, all information is returned if found, and "Not Found" is output if not found

3. Administrator interface:

  1. Provide the interface for adding, deleting, modifying and checking for students and adding, deleting, and modifying user accounts.
  2. For students:
    1. Check: More functions than the user interface to view all student information
    2. Added: You can create student information, such as input in the specified format, if the format is wrong, an error will be reported and re-entered
    3. Delete: first query and delete student information, if there is, list it, then delete it, if not, exit with "Not Found"
    4. Modify: You can modify the student's grade information, enter it as required, and report an error if you enter it incorrectly.
  3. To users:
    1. Added: You can add non-repeated usernames, where you can directly register users as administrator privileges
    2. delete: delete user
    3. Change: You can change the password and administrator rights. Which one to modify as prompted.
    4. lookup: find users

4. Exit the interface:

  1. All interfaces can only be saved by pressing the exit prompt on the interface to save the input data. If the prompt is not exited normally, the operation data will not be written to the file. Print a thank you message when exiting.

document:

User information and student information will be passed in by the system, and an error will be reported if the argc and argv verification fails. Create files database.ext and userinfo.txt

Code:

file.h

The function declarations in file.c, user.c, admin.c, main.c are all in it

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <conio.h>
#include <string.h>

#define MaxSize_name 20
#define MaxScore 100

typedef struct User_s {
	char username[MaxSize_name];
	char password[MaxSize_name];
	bool isAdmin;
	struct User_s* next;
} User;

typedef struct Student_s{
	int id;
	char name[MaxSize_name];
	int chinese;
	int math;
	int english;
	struct Student_s* next;
} Student;

//file.c
void Start_system(int argc, char* argv[],User* user_table, Student* student_table);//Initialize, load table
bool initial_interface(User* user_table);	//login interface
void show_system(bool isAdmin, User* user_table, Student* student_table);//Display interface according to permissions
void close_system(int argc, char* argv[], User* user_table, Student* student_table);//shut down the system
void print_title(void);	//print title
void Insert_password(char password[]); //Anonymous input
void clear_input(void);	//clear the rest of the input
void print_illegal_input(void);//print illegal input
void print_end_info(char* c);//Print the last message of the function, while pausing
void print_thank(void);	//print conclusion

//user.c
void show_user_system(const Student* student_table);//The user account only needs to pass in the student information.
void find_name(const Student* student_table);
void find_id(const Student* student_table);
void print_studentInfo(const Student *s); //output student information

//admin.c
void show_Admin_system(User* user_table, Student* student_table);//The administrator account can delete and modify the account, so pass in the secondary pointer
void find_all(const Student *student_table);
void add_studentInfo(Student* student_table);
void update_studentInfo(Student* student_table);
void delete_student(Student* student_table);
void add_user(User* user_table);
void update_user(User* user_table);
void search_user(const User* user_table);
void delete_user(User* user_table);

main.c

main function

#include "file.h"

int main(int argc, char* argv[]) {
	//Create head node
	User user_table = { "admin","123456",true };//Initialize an administrator account
	Student student_table = { 0 };
	 
	Start_system(argc, argv,&user_table,&student_table);//open table
	bool isAdmin = initial_interface(&user_table);//Login and determine permissions
	show_system(isAdmin, &user_table, &student_table);//Open the interface
	close_system(argc, argv, &user_table, &student_table);//Shut down the system and write the file again

	return 0;
}

file.c

  • Realize the login interface, showing the functions of login, registration and logout
  • Implement anonymous input during login (that is, the input data is displayed as ' * ') function
  • Responsible for file processing, reading both user data and student data into memory
#include "file.h"

//anonymous input function
void Insert_password(char password[]) {
	char c;
	while ((c = _getch()) != '\r') {
		if ('\b' == c) {
			printf("\b \b");
			password = password - 1;
			*password = '\0';
		}
		else {
			putchar('*');
			*password = c;
			password = password + 1;
		}
	}
}

//Login interface to determine whether the account is correct
int sign_in(User* user_table) {
	//Sign in until you enter a pair
	while (true) {
		system("cls"); //clear screen
		puts("Sign in Student Information Management System");
		User* p = user_table;
		char username[MaxSize_name] = { 0 };
		char password[MaxSize_name] = { 0 };
		//Enter username and account
		printf("\nUsername: ");
		gets(username);
again_password:
		printf("Password: ");
		Insert_password(password);
		//Judging whether there is this number, return the permission if there is, return -1 if not
		while (p != NULL) {
			if (!strcmp(p->username, username)) {
				//Found it, start verifying the password
				if (!strcmp(p->password, password)) {
					print_end_info("Log in succeed.");
					return p->isAdmin;
				}
				//wrong password
				else {
					print_end_info("Error: password is wrong.");
					goto again_password;
				}
			}
			p = p->next;
		}
		print_end_info("The account could not be found.");
	}
}

//Registration interface, create a non-identical account, the default is user
void sign_out(User* user_table) {
	char username[MaxSize_name] = { 0 }, password[MaxSize_name] = { 0 }, secondword[MaxSize_name] = { 0 };
	//Loop through usernames
again_name:
	system("cls"); //clear screen
	puts("Sign out of Student Information Management System");
	printf("\nUsername: ");
	gets(username);
	//Determine if there is a repetition
	User* p = user_table;
	while (p != NULL) {
		if (!strcmp(p->username, username)) {
			puts("\nDuplicate name, please enter again.");
			system("pause");
			goto again_name;
		}
		p = p->next;
	}
	//Enter password anonymously
again_word:
	printf("Password: ");
	Insert_password(password);
	printf("\nPassword again: ");
	Insert_password(secondword);
	if (strcmp(password, secondword)) {
		puts("\nThe two entered passwords do not match, enter again.");
		goto again_word;
	}
	//Link to user data
	User* newNode = (User*)malloc(sizeof(User));
	if (newNode == NULL) {
		perror("calloc failed in add_studnetInfo: ");
		return;
	}
	strcpy(newNode->username, username);
	strcpy(newNode->password, password);
	newNode->next = user_table->next;
	user_table->next = newNode;
	//quit
	puts("\nCreat user succesed.press any to continue.");
	system("pause");
}

//Verify the parameters, open the file, and load it into memory
void Start_system(int argc, char* argv[], User* user_table, Student* student_table) {
	if (argc != 3) {
		puts("Error: Invalid Address Entered.\n");
		exit(1);
	}
	//will open the file in read-only mode
	FILE* user_film_p = fopen(argv[1], "ab+");
	if (user_film_p == NULL) {
		perror("userinfo.txt");
		exit(1);
	}
	FILE* student_film_p = fopen(argv[2], "ab+");
	if (student_film_p == NULL) {
		perror("database.txt");
		fclose("Student_film_p");
		exit(1);
	}
	//Create user information table
	User* p = user_table;
	do {
		User* newNode = (User*)calloc(1, sizeof(User));
		if (newNode == NULL) {
			perror("calloc failed in start_system: ");
			return;
		}
		p->next = newNode;
		p = p->next;
	} while (fread(p, sizeof(User), 1, user_film_p) != 0);
	//Create a Student Information Form
	Student* s = student_table;
	do {
		Student* newNode = (Student*)calloc(1, sizeof(Student));
		if (newNode == NULL) {
			perror("calloc failed in start_system: ");
			return;
		}
		s->next = newNode;
		s = s->next;
	} while (fread(s, sizeof(Student), 1, student_film_p) != 0);
	//close file
	fclose(user_film_p);
	fclose(student_film_p);
}

//Login interface, return whether it is an administrator
bool initial_interface(User* user_table) {
	//print interface
	print_title();
	puts("1.sign in");
	puts("2.sign out");
	puts("3.exit");
	//judge input
	while (true) {
		char ch = getchar();
		clear_input();
		switch (ch) {
		case '1':
			return sign_in(user_table);
		case '2':
			sign_out(user_table);
			return false;
		case '3':
			print_thank();
			exit(0);
		default:
			print_illegal_input();
			break;
		}
	}
}

//Show system interface
void show_system(bool isAdmin, User* user_table, Student* student_table) {
	system("cls"); 
	print_title();
	if (isAdmin) {
		show_Admin_system(user_table,student_table);
	}
	else {
		show_user_system(student_table);
	}
}

//Shut down the system, and write the information in the cache to the disk file, close the file
void close_system(int argc, char* argv[], User* user_table, Student* student_table) {
	//will open the file in overwrite mode and update the table in its entirety
	FILE* user_film_p = fopen(argv[1], "wb+");
	if (user_film_p == NULL) {
		perror("userinfo.txt");
		exit(1);
	}
	FILE* student_film_p = fopen(argv[2], "wb+");
	if (student_film_p == NULL) {
		perror("database.txt");
		fclose("Student_film_p");
		exit(1);
	}
	//Write in-memory data to a file
	User* u = user_table;
	Student* s = student_table->next;
	while (u != NULL && strlen(u->username) > 0) {
		fwrite(u, sizeof(User), 1, user_film_p);
		u = u->next;
	}
	while (s != NULL && strlen(s->name) > 0) {
		fwrite(s, sizeof(Student), 1, student_film_p);
		s = s->next;
	}
	fclose(user_film_p);
	fclose(student_film_p);
}

//print header information
void print_title(void) {
	system("cls");
	puts("*******************************************************");
	puts("******** Student Information Management Syetem ********");
	puts("****************** Build by dsy ***********************");
	puts("*******************************************************\n");
}

//Clear redundant input
void clear_input(void) {
	char temp;
	while ((temp = getchar()) != '\n');
}

//print illegal input
void print_illegal_input(void) {
	system("cls");
	puts("Illigal argument. Press any to continue.");
	system("pause");
}

//Output the last message and pause the screen
void print_end_info(char* c) {
	printf("\n%s . press any to continue.\n", c);
	system("pause");
}

//Print thanks at the end of the program
void print_thank(void) {
	system("cls");
	print_title();
	puts("Thanks for using. See you next time.\n");
}

user.c

  • Implement the output of a normal user interface
  • Realize the query of student information by ID and by student number.
#include "file.h"

//Display options
void print_options_user() {
	puts("1. Search by name");
	puts("2. Search by ID");
	puts("3. exit");
}

//Display the user interface
void show_user_system(const Student* student_table) {
	while (true) {
		//print interface
		print_title();
		print_options_user();
		//Processing options
		char c = getchar();
		clear_input();
		switch (c)
		{
		case '1':
			find_name(student_table);
			break;
		case '2':
			find_id(student_table);
			break;
		case '3':
			print_thank();
			return;
		default:
			print_illegal_input();
			break;
		}
	}
}

//lookup name
void find_name(const Student* student_table) {
	Student* p = student_table->next;
	char name[MaxSize_name];
	print_title();
	puts("input student name: ");
	gets(name);
	while (p != NULL) {
		if (!strcmp(p->name, name)) {
			print_studentInfo(p);
			puts("\npress any to continue.\n");
			system("pause");
			return;
		}
		p = p->next;
	}
	print_end_info("Not found");
}

//find id
void find_id(const Student* student_table) {
	Student* p = student_table->next;
	int id;
	print_title();
	puts("input student name: ");
	scanf("%d", &id);
	clear_input();
	while (p != NULL) {
		if (id == p->id) {
			print_studentInfo(p);
			puts("\npress any to continue.\n");
			system("pause");
			return;
		}
		p = p->next;
	}
	printf("%d number is not found.\n", id);
}

void print_studentInfo(const Student* s) {
	printf("\nID   name                 Chinese  Math  English\n");
	printf("%03d  %-20s %-7d  %-4d  %-7d\n\n", s->id, s->name, s->chinese, s->math, s->english);
}

admin.c

  • Implement the output of the administrator account interface
  • Realize the function of administrator: add, delete, modify and search for student information, add, delete, modify and search for user information
#include "file.h"

//print user information
void print_userInfo(const User* u) {
	printf("username             password             isAdmin\n");
	if (u->isAdmin) {
		printf("%-20s %-20s True\n", u->username, u->password);
	}
	else {
		printf("%-20s %-20s False\n\n", u->username, u->password);
	}
}

//Show search list options
void show_search_system(Student* student_table) {
	while (true) {
		//print lookup table
		system("cls");
		print_title();
		puts("1. find all students");
		puts("2. Search by name");
		puts("3. Search by ID");
		puts("4. back to superior menu");
		//option selection
		char c = getchar();
		clear_input();
		switch (c) {
		case '1':
			find_all(student_table);
			break;
		case '2':
			find_name(student_table);
			break;
		case '3':
			find_id(student_table);
			break;
		case '4':
			return;
		default:
			print_illegal_input();
			break;
		}
	}
}

//Display options
void print_options_admin() {
	puts("1. Search student information");
	puts("2. add student information");
	puts("3. update student information");
	puts("4. delete student information");
	puts("5. add user account");
	puts("6. update user account");
	puts("7. delete user account");
	puts("8. search user account");
	puts("9. exit");
}

//main function
void show_Admin_system(User* user_table, Student* student_table) {
	while (true) {
		//print interface
		print_title();
		print_options_admin();
		//Processing options
		char c = getchar();
		clear_input();
		switch (c)
		{
		case '1':
			show_search_system(student_table);
			break;
		case '2':
			add_studentInfo(student_table);
			break;
		case '3':
			update_studentInfo(student_table);
			break;
		case '4':
			delete_student(student_table);
			break;
		case '5':
			add_user(user_table);
			break;
		case '6':
			update_user(user_table);
			break;
		case '7':
			delete_user(user_table);
			break;
		case '8':
			search_user(user_table);
			break;
		case '9':
			print_thank();
			return;
		default:
			print_illegal_input();
			break;
		}
	}
}

//View all students
void find_all(const Student* student_table) {
	print_title();
	Student* s = student_table->next;
	printf("ID   name                 Chinese  Math  English\n");
	while (strlen(s->name) > 0 && s != NULL) {
		printf("%03d  %-20s %-7d  %-4d  %-7d\n", s->id, s->name, s->chinese, s->math, s->english);
		s = s->next;
	}
	system("pause");
}

//increase students
void add_studentInfo(Student* student_table) {
	print_title(); //refresh page
	Student* s = student_table->next;
	Student* newNode = (Student*)malloc(sizeof(Student));
	if (newNode == NULL) {
		perror("calloc failed in add_studnetInfo: ");
		return;
	}
	//Prompt for input format
	puts("please input student infomation which you want to add.");
	puts("fomat is: ID Student_name Chinese_score Math_Score English_score.");
	puts("for example: 1 Tom 99 100 80");
	//Read in student information (error checking)
again:
	memset(newNode, 0, sizeof(Student));
	scanf("%d%s%d%d%d", &newNode->id, newNode->name, &newNode->chinese, &newNode->math, &newNode->english);
	clear_input();
	//Check for typos
	if (newNode->id > 999) {
		puts("ID exceeds Lenth. Try angin.");
		goto again;
	}
	if (strlen(newNode->name) > MaxSize_name) {
		puts("name exceeds length. Try again.");
		goto again;
	}
	if (newNode->chinese > MaxScore || newNode->math > MaxScore || newNode->english > MaxScore) {
		puts("Score is error. Try angin.");
		goto again;
	}
	s = student_table->next;
	//Check duplicates
	while (s != NULL) {
		if (!strcmp(newNode->name, s->name)) {
			puts("This student's information is already exist. Tetry again.");
			goto again;
		}
		s = s->next;
	}
	//no error, the link
	newNode->next = student_table->next;
	student_table->next = newNode;
	print_end_info("Add succeed.");
}

//Update student information (update with name)
void update_studentInfo(Student* student_table) {
	print_title();
	puts("which student's information do you want to update?");
	char name[MaxSize_name] = { 0 };
	gets(name);
	//Find a student
	Student* s = student_table;
	while (s != NULL) {
		//if found
		if (!strcmp(name, s->name)) {
			puts("find succeed.");
			print_studentInfo(s);
			puts("please input new score. Formate is: Chinese Math English.");
			while (true) {
				scanf("%d%d%d", &s->chinese, &s->math, &s->english);
				clear_input();
				if (s->chinese > 100 || s->math > 100 || s->english > 100) {
					puts("Error Score. Try it again.");
					continue;
				}
				else {
					print_end_info("update succeed.");
					return;
				}	
			}
		}
		s = s->next;
	}
	//could not find it
	print_end_info("Not found");
}

//delete student
void delete_student(Student* student_table) {
	print_title();
	puts("which student's information do you want to delete?");
	char name[MaxSize_name] = { 0 };
	gets(name);
	//Find a student
	Student* s = student_table->next;
	Student* pre = student_table;
	while (s != NULL) {
		if (!strcmp(name, s->name)) {
			print_studentInfo(s);
			pre->next = s->next;
			free(s);
			puts("Delete succedd. Press any to continue.");
			system("pause");
			return;
		}
		pre = s;
		s = s->next;
	}
	//could not find it
	print_end_info("Not found.");
}

//Add users, you can set whether to be an administrator
void add_user(User* user_table) {
	print_title(); //refresh page
	char username[MaxSize_name] = { 0 }, password[MaxSize_name] = { 0 }, secondword[MaxSize_name] = { 0 };
	bool isAdmin;
	puts("please input user infomation which you want to add.");
	//Enter your name (heavy)
again_name:
	system("cls"); //clear screen
	puts("Sign out of Student Information Management System");
	printf("\nUsername: ");
	memset(username, 0, sizeof(username));
	gets(username);
	//Determine if there is a repetition
	User* p = user_table;
	while (p != NULL) {
		if (!strcmp(p->username, username)) {
			puts("\nDuplicate name, please enter again.");
			system("pause");
			goto again_name;
		}
		p = p->next;
	}
	//Enter password (wrong)
again_word:
	memset(password, 0, sizeof(password));
	memset(secondword, 0, sizeof(secondword));
	printf("Password: ");
	Insert_password(password);
	printf("\nPassword again: ");
	Insert_password(secondword);
	if (strcmp(password, secondword)) {
		puts("\nThe two entered passwords do not match, enter again.");
		goto again_word;
	}
	//Enter permissions
	printf("\nIs this user administrator ?");
	while (true) {
		puts("Y/N ? ");
		char c = getchar();
		//char backspace = getchar();
		//Implement backspace
		if (c == "\b")	{
			printf("\b \b");
			c = getchar();
			continue;
			}
		if (c  == 'Y') {
			isAdmin = true;
			break;
		}
		if(c == 'N'){
			isAdmin = false;
			break;
		}
		print_end_info("Illegal Input.");
		//clear_input();
	}
	
	//Link to user data
	User* newNode = (User*)malloc(sizeof(User));
	if (newNode == NULL) {
		perror("calloc failed in add_studnetInfo: ");
		return;
	}
	strcpy(newNode->username, username);
	strcpy(newNode->password, password);
	newNode->isAdmin = isAdmin;
	newNode->next = user_table->next;
	user_table->next = newNode;
	//quit
	print_end_info("Creat user succesed.");
}

//Modify user information
void update_user(User* user_table) {
	print_title();
	puts("which user's information do you want to update?");
	char name[MaxSize_name] = { 0 };
	gets(name);
	//Find users
	User* u = user_table;
	while (u != NULL) {
		//if found
		if (!strcmp(name, u->username)) {
			puts("find succeed.");
			print_userInfo(u);
			puts("which do you want to change ? password or isAdmin ?\n");
			while (true) {
				puts("p/i ? ");
				char c = getchar();
				clear_input();
				//Modify administrator information
				if (c == 'i') {
					printf("Is %s administrator ?\n", u->username);
					while (true) {
						puts("Y/N ? ");
						char ch = getchar();
						clear_input();
						if (ch == 'Y') {
							u->isAdmin = true;
							print_end_info("Change succeed.");
							return;
						}
						if (ch == 'N') {
							u->isAdmin = false;
							print_end_info("Change succeed.");
							return;
						}
					}
				}
				//change Password
				if (c == 'p') {
					printf("Input password: ");
					gets(u->password);
					print_end_info("password change succeed");
					return;
				}
			}
		}
		u = u->next;
	}
	//could not find it
	print_end_info("Not found.");
}

//Find users
void search_user(const User* user_table) {
	User* u = user_table;
	char name[MaxSize_name];
	print_title();
	puts("input user name: ");
	gets(name);
	while (u != NULL) {
		if (!strcmp(u->username, name)) {
			print_userInfo(u);
			print_end_info("search succeed.");
			return;
		}
		u = u->next;
	}
	print_end_info("Not found.");
}

//delete users
void delete_user(User* user_table) {
	User* u = user_table;
	User* pre = NULL;
	char name[MaxSize_name];
	print_title();
	puts("input user name you want to delete: ");
	gets(name);
	while (u != NULL) {
		if (!strcmp(u->username, name)) {
			print_userInfo(u);
			//If pre is NULL, it means to delete the first admin account, which is not allowed.
			if (pre == NULL) {
				puts("Error: You can not delete Administrator.");
				return;
			}
			User* temp = u;
			pre->next = temp->next;
			free(temp);
			print_end_info("Delete succeed.");
			return;
		}
		pre = u;
		u = u->next;
	}
	print_end_info("Not found.");
}

Tags: data structure C linked list

Posted by kenmay09 on Mon, 11 Jul 2022 00:22:58 +0930