Employee information management system

Function description

requirement analysis

  1. Login: the administrator enters the account and password. Only when the account is correct can he enter the system for operation
  2. Add employee information: enter employee information from the keyboard, and the employee information will be inserted into the linked list
  3. Delete employee information: enter the employee student number and delete the employee information from the linked list according to the employee number
  4. Modify employee information: select the option to modify
  5. Input the information of all employees: traverse the linked list and output the information of all employees to the console
  6. Search for designated employee: query the designated employee according to the employee name and employee number, and output the employee information to the console

Header file

Here is also the header file we need to write in first

Insert the code slice here
#Include < stdio. H > / / scanf printf standard I / O
#Include < stdlib. H > / / malloc() free() and other functions
#Include < string. H > / / references to functions such as strcmp() strcpy()
#Include < windows. H > / / used for color change
#Include < conio. H > / / encryption

login interface

Here I write the login directly into the main function
There are only three opportunities to enter the account and password in the system. If you enter the wrong account and password three times, you will exit by yourself;

Insert the code slice here
//Main function
int main(){
	initLinkedList();
	//land

	char UserAccount[100],UserPass[100];
	//The number of login times recorded cannot exceed three
	int LoginSucceed = 0;
	int LoginError = 0;

	while (LoginSucceed!=1){
		printf("Please enter account number:\n");
		scanf("%s",UserAccount);
		//inputpasswd(UserAccount);
		printf("Please input a password: \n");
		inputpasswd(UserPass);
		//scanf("%s",UserPass);
		//verification
		LoginSucceed=login(UserAccount, UserPass);
		//Login succeeded: 1
		//Login failed: - 1
		if(LoginSucceed==1){
			//Successfully entered the system
			operation();
		}
		else if(LoginSucceed==-1){
			printf("Wrong account or password!\n");
			LoginError++;
		}
		//Limit of login times
		if(LoginError>=3){
			printf("\n If the account or password is entered incorrectly for three times, you will be forced to exit the system!\n");
			break;
		}
	}
	return 0;
}

Welcome interface

Insert the code slice here
//Welcome interface
void welcome(){
	printf("\t\t\t**********Welcome to the employee information management system***********\n");
	printf("\t\t\t---------------------------------------------\n");
	printf("\t\t\t---------------------------------------------\n");
	printf("\t\t\t---------------System prompt:--------------------\n");
	printf("\t\t\t-----After entering the system, please enter the number code according to the menu prompt------\n");
	printf("\t\t\t---------------Welcome to use------------------\n");
	printf("\t\t\t---------------------------------------------\n");
}

Structure definition

Store the employee information we need as a member of the structure

Insert the code slice here
typedef struct staff {
	char StaffNumber[100];//Employee number
	char name[20];//full name
	char sex[20];//Gender
	char age[20];//Age
	char EducationBackground[100];//education
	char position[100];//post
	char income[20];//wages
	char address[100];//address
	char phone[20];//Telephone
	struct staff *next;//Used to point to the next node
}staff;

Linked list initialization

The data structure used for the storage of employee information is a single linked list. The structure is used to store employee information, and the next pointer is used to connect multiple employee information in series

Insert the code slice here
staff *head;
//First, the linked list is initialized, and the header pointer is defined to point to NULL;
void initLinkedList(){
	head=(staff *) malloc(sizeof(staff));
	head->next=NULL;
}

home page


Operate according to the options;

Insert the code slice here
//Make menu
void menu(){
	printf("*********************************************\n");
	printf("**********    Employee information management system    ************\n");
	printf("*********************************************\n");
	printf("              1.Insert employee information                   \n");
	printf("              2.Modify employee information                   \n");
	printf("              3.Delete employee information                   \n");
	printf("              4.Find employee information                   \n");
	printf("              5.View all employee information                \n");
	printf("              6.Save employee information                   \n");
	printf("              7.Read employee information                   \n");
	printf("              8.Exit the system                       \n");
	printf("*********************************************\n");
}

increase

Head insertion is also used here

Insert the code slice here
//Insert new employee information (insert: insert)
void insert(){
	//Application space - create node
	staff *NewStaff=(staff *) malloc(sizeof (staff));

	//Obtain employee information
	printf("Please enter employee number: \n");
	scanf("%s",NewStaff->StaffNumber);
	printf("Please enter employee name: \n");
	scanf("%s",NewStaff->name);
	printf("Please enter employee gender: \n");
	scanf("%s",NewStaff->sex);
	printf("Please enter employee age: \n");
	scanf("%s",NewStaff->age);
	printf("Please enter employee education: \n");
	scanf("%s",NewStaff->EducationBackground);
	printf("Please enter employee Title: \n");
	scanf("%s",NewStaff->position);
	printf("Please enter employee salary: \n");
	scanf("%s",NewStaff->income);
	printf("Please enter the employee's home address: \n");
	scanf("%s",NewStaff->address);
	printf("Please enter employee telephone number: \n");
	scanf("%s",NewStaff->phone);
	//Head insertion is used here
	NewStaff->next=head->next;
	head->next=NewStaff;
	printf("Entered successfully!!!\n");

}

Delete

Insert the code slice here
void  del(){
	int op;
	printf("-----1.Search and delete by name   -----\n");
	printf("-----2.Search and delete by employee No -----\n");
	printf("-----3.Find delete by phone   -----\n");
	printf("-----4.Exit this menu          -----\n");

	char StaName[100];
	char StaNum[100];
	char StaPhone[100];
	while(1){
		printf("Please select the submenu number:\n");
		scanf("%d",&op);
		switch(op){
		case 1:
		{
			printf("Please enter the name of the employee to be deleted:\n");
			scanf("%s",StaName);
			staff *p1=head;
			staff *p2=head->next;
			while(p2!=NULL){
				if(strcmp(p2->name,StaName)==0){
					p1->next=p2->next;
					free(p2);
					p2=NULL;//Prevent p2 from becoming a wild pointer
					printf("Deleted successfully!!!\n");
					break;
				}
				p1=p2;
				p2=p2->next;
			}
			break;
		}
		case 2:
		{
			printf("Please enter the employee number to delete:\n");
			scanf("%s",StaNum);
			staff *p1=head;
			staff *p2=head->next;
			while(p2!=NULL){
				if(strcmp(p2->StaffNumber,StaNum)==0){
					p1->next=p2->next;
					free(p2);
					p2=NULL;
				}
				p1=p2;
				p2=p2->next;
			}
			break;
		}
		case 3:
		{
			printf("Please enter the phone number of the employee to be deleted:\n");
			scanf("%s",StaPhone);
			staff *p1=head;
			staff *p2=head->next;
			while(p2!=NULL){
				if(strcmp(p2->phone,StaPhone)==0){
					p1->next=p2->next;
					free(p2);
					p2=NULL;
				}
				p1=p2;
				p2=p2->next;
			}
			break;
		}
		case 4:
			return ;
			default:
				printf("Please at 1~4 Select from!\n");
		}
	}
}

check

Insert the code slice here
//check
void find(){
	printf("\t\t\t***** 1.Query by employee name *****\n");
	printf("\t\t\t***** 2.Query by employee No   *****\n");
	printf("\t\t\t***** 3.Query by employee telephone *****\n");
	printf("\t\t\t***** 4.Exit this menu     *****\n");
	int op;
	while (1){
		printf("Please enter the submenu number:\n");
		scanf("%d",&op);
		switch (op) {
			case 1://Search by employee name
			{
				printf("Please enter the name of the employee information you want to find:\n");
				char StaName[100];
				scanf("%s",StaName);
				staff *p1 = head->next;//Define Pointer use traversal
				while (p1!=NULL){
					if(strcmp(p1->name,StaName)==0){
						printf("Employee number:%s\n Employee name:%s\n Employee gender:%s\n Employee age:%s\n"
							"Employee education:%s\n Employee salary:%s\n Employee position:%s\n Employee's home address:%s\n"
							"Employee telephone:%s\n",p1->StaffNumber,p1->name,p1->sex,p1->age,p1->EducationBackground,
							p1->income,p1->position,p1->address,p1->phone);
						break;
					}
					p1=p1->next;
					printf("The employee was not found!\n");
				}
				break;
			}
		case 2:
		{
			printf("Please enter the employee number of the employee information to be searched:\n");
			char StaNum[100];
			scanf("%s",StaNum);
			staff *p1 = head->next;//Define Pointer use traversal
			while (p1!=NULL){
				if(strcmp(p1->StaffNumber,StaNum)==0){
					printf("Employee number:%s\n Employee name:%s\n Employee gender:%s\n Employee age:%s\n"
						"Employee education:%s\n Employee salary:%s\n Employee position:%s\n Employee's home address:%s\n"
						"Employee telephone:%s\n",p1->StaffNumber,p1->name,p1->sex,p1->age,p1->EducationBackground,
						p1->income,p1->position,p1->address,p1->phone);
					break;
				}
				p1=p1->next;
			}
			break;
		}
		case 3:
		{
			printf("Please enter the telephone number of the employee information you want to find:\n");
			char StaPhone[100];
			scanf("%s",StaPhone);
			staff *p1 = head->next;//Define Pointer use traversal
			while (p1!=NULL){
				if(strcmp(p1->phone,StaPhone)==0){
					printf("Employee number:%s\n Employee name:%s\n Employee gender:%s\n Employee age:%s\n"
						"Employee education:%s\n Employee salary:%s\n Employee position:%s\n Employee's home address:%s\n"
						"Employee telephone:%s\n",p1->StaffNumber,p1->name,p1->sex,p1->age,p1->EducationBackground,
						p1->income,p1->position,p1->address,p1->phone);
					break;
				}
				p1=p1->next;
			}
			break;
		}
		case 4:
			return ;
			default:
				printf("Please at 1~4 Choose between!!!\n");
		}
	}
}

change

Insert the code slice here
//change
void change (){
	staff *flagp=head->next;

	char StaffNum[100];//Used to store employee number
	//Get employee number
	printf("Please enter the employee number to be modified: \n");
	scanf("%s",StaffNum);

//	int flag;// It is used to identify whether the employee is found. The initial value is 0 and 1 when found

	//Traversal list
	while (flagp!=NULL){//Traversal when the next header node is not empty
		if(strcmp(flagp->StaffNumber,StaffNum)==0){//Description target found
			//Then perform the required operations
			printf("***** 1.Modify employee No      *****\n");
			printf("***** 2.Modify employee name    *****\n");
			printf("***** 3.Modify employee gender    *****\n");
			printf("***** 4.Modify employee age    *****\n");
			printf("***** 5.Modify employee education    *****\n");
			printf("***** 6.Modify employee position    *****\n");
			printf("***** 7.Modify employee salary    *****\n");
			printf("***** 8.Modify the employee's home address *****\n");
			printf("***** 9.Modify employee telephone    *****\n");
			printf("***** 10.Exit submenu     *****\n");
			printf("Please select the submenu number: \n");
			int op;
			scanf("%d",&op);
			while (op!=10){
				switch (op) {
				case 1:
					printf("Please confirm the employee number before modification:%s\n",flagp->StaffNumber);
					printf("Please enter the modified employee number: \n");
					scanf("%s",flagp->StaffNumber);
					break;
				case 2:
					printf("Please confirm the name of the employee before modification:%s\n",flagp->name);
					printf("Please enter the modified employee name:\n");
					scanf("%s",flagp->name);
					break;
				case 3:
					printf("Please confirm the gender of the employee before modification:%s\n",flagp->sex);
					printf("Please enter the modified employee gender:\n");
					scanf("%s",flagp->sex);
					break;
				case 4:
					printf("Please confirm the age of the employee before modification:%s\n",flagp->age);
					printf("Please enter the employee's modified age:\n");
					scanf("%s",flagp->age);
					break;
				case 5:
					printf("Please confirm the education background of the employee before modification:%s\n",flagp->EducationBackground);
					printf("Please enter the employee's modified Education:\n");
					scanf("%s",flagp->EducationBackground);
					break;
				case 6:
					printf("Please confirm the position of the employee before modification:%s\n",flagp->position);
					printf("Please enter the modified job title of the employee:\n");
					scanf("%s",flagp->position);
					break;
				case 7:
					printf("Please confirm the salary of the employee before modification:%s\n",flagp->income);
					printf("Please enter the employee's modified salary:\n");
					scanf("%s",flagp->income);
					break;
				case 8:
					printf("Please confirm the home address of the employee before modification:%s\n",flagp->address);
					printf("Please enter the employee's modified home address:\n");
					scanf("%s",flagp->address);
					break;
				case 9:
					printf("Please confirm the telephone number of the employee before modification:%s\n",flagp->phone);
					printf("Please enter the modified telephone number of the employee:\n");
					scanf("%s",flagp->phone);
					break;
					default:
						printf("Please enter 1~9 Number between!\n");
						scanf("%d",&op);
				}
				printf("Please select the submenu number:\n");
				scanf("%d",&op);
			}
			break;
		}
		flagp=flagp->next;
	}
	if(flagp==0)
		printf("The employee does not exist!\n");
}

View all employee information

Insert the code slice here
void all(){
	staff *p = head->next;
//	printf("%-20s %-20s %-5s %-10s %-100s %-20s %-20s %-100 %-20\n",
//		"Employee No.:," Name: "," gender: "," age: "," Education: "," position: "," salary: "," address: "," telephone: \ n ");
	while (p != NULL){

		printf("Employee No.:%-20s full name:%-20s Gender:%-5s Age:%-10s education:%-100s Title:%-20s Salary:%-20s Home address:%-100s Telephone:%-20s\n",
			p->StaffNumber,p->name,p->sex,p->age,p->EducationBackground,p->position,
			p->income,p->address,p->phone);
		p=p->next;
	}
}

preservation

Use the file to save the input employee information

Insert the code slice here
void SaveFile() {  //SaveFile save file
	FILE *file=NULL;
	file=fopen("D:\\staff.txt","wb+");
	if(!file) {
		printf("File open failed\n");
	} else {
		struct staff *p;
		for(p=head->next; p!=NULL; p=p->next) {
			//fwrite(&p->StaffNumber,sizeof(p->StaffNumber),1,file);
			fprintf(file,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
				p->StaffNumber,p->name,p->sex,p->age,p->EducationBackground,
				p->position,p->income,p->address,p->phone);
		}
		fclose(file);
		printf("Storage succeeded!\n");
	}
}

read file

Insert the code slice here
void read() {
	FILE *file = NULL;
	file=fopen("D:\\staff.txt","r");
	if(!file) {
		printf("File open failed\n");
		return ;
	}
	// Request temporary space to hold data
	struct staff *p=(staff*)malloc(sizeof(staff));
	while(!feof(file)) {
		// Read the data from the file into the program and put it in the space pointed to by p
		fscanf(file,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
			&p->StaffNumber,&p->name,&p->sex,&p->age,&p->EducationBackground,
			&p->position,&p->income,&p->address,&p->phone);

		// console output 
		printf("Employee No.:%-20s full name:%-20s Gender:%-5s Age:%-10s education:%-100s Title:%-20s Salary:%-20s Home address:%-100s phone number:%-20s\n",
			p->StaffNumber,p->name,p->sex,p->age,p->EducationBackground,
			p->position,p->income,p->address,p->phone);
	}
	// Close file stream

	fclose(file);

	// Release resources
	free(p);
	p = NULL;
}

Operation function: gather other functions together and put them into the main function

Insert the code slice here
//operation
void operation(){
	system("color f4");
	system("cls");//Clear screen
	welcome();
	Sleep(1800);
	system("cls");
	int op;
	int b;
	menu();
	printf("Please enter your instructions:\n");
//	scanf("%d",&op);
	b=scanf("%d",&op);
	while(b!=1){
		while(getchar()!='\n');
		printf("Warning, only numbers can be entered!\n");
		b=scanf("%d",&op);
	}
	b=0;
	while (op!=8){
		switch (op) {
		case 1:
			printf("****Insert employee information****\n");
			insert();
			break;
		case 2:
			printf("****Modify employee information****\n");
			change();
			break;
		case 3:
			printf("****Delete employee information****\n");
			del();
			break;
		case 4:
			printf("\t\t\t****Find employee information****\n");
			find();
			break;
		case 5:
			printf("****View all employee information****\n");
			all();
			break;
		case 6:
			printf("****Employee information saving****\n");
			SaveFile();
			break;
		case 7:
			printf("****Employee information reading****\n");
			read();
			break;
		default:
				printf("Input command error!!!\n");
				break;
		}
		//Enter a number when required
		//
		system("pause");
		system("cls");
		menu();
		printf("Please enter your instructions:\n");
		int b;
		b=scanf("%d",&op);
		while(b!=1){
			while(getchar()!='\n');
			printf("Warning, only numbers can be entered!\n");
			b=scanf("%d",&op);
		}
		b=0;
//		scanf("%d",&op);
	}
	printf("About to exit the system!\n");
}

Account, password

Insert the code slice here
//Cryptosystem
//Parameter: Account: account
//Parameter: Password: password
// login to the system
int login(char UserAccount[],char UserPass[]){
	char account[100]="1";// Default account 1
	char password[100]="1";//Default password 1
	if(strcmp(UserAccount,account)==0&& strcmp(UserPass,password)==0){
		return 1;
	}
	else
		return -1;
}

Here, only the account number needs to be displayed for security, so the password is encrypted;

Insert the code slice here
//Account password encryption
void inputpasswd (char * pass){
	int n = 0;
	while(1){
		char ch = getch();//getch() reads a character from the console but does not display it on the screen
		if(ch=='\r'){
			pass[n]='\0';//	The last character of an empty string. It is empty by default
			putchar('\r');//  \r enter
			putchar('\n');
			return;
		}//TODO
		if(ch=='\b'){// \b 	 Backspace
			if(n>0){
				putchar('\b');
				putchar(' ');
				putchar('\b');
				n--;//TODO
			}//TODO
		}
		else
		{
			pass[n++] = ch;
			putchar('*');
		}
	}
}

The above is a relatively simple employee information management system. I hope you can communicate with each other!

Keywords: C

Added by cunoodle2 on Wed, 08 Dec 2021 02:23:38 +0200