C++ implementation of simple staff management system
- 2020-05-10 18:31:32
- OfStack
The example of this article for you to share C++ staff management system example code, for your reference, the specific content is as follows
1. Individual employee's header file
staff.h
#ifndef STAFF_H_INCLUDED
#define STAFF_H_INCLUDED
// Structure creation
struct staff
{
char ID[10];
char name[10];
char sex[10];
int pay;
int reward;
int factpay;
};
// Custom structure
typedef struct staff staff;
// Individual employee information creation
staff Createstaff();
// Individual employee information output
void Displaystaff(staff staff);
// Modify employee information
void updatestaff(staff *Staff);
#endif // STAFF_H_INCLUDED
single-worker cpp file
staff.cpp
#include <stdio.h>
#include <stdlib.h>
#include "staff.h"
staff Createstaff()
{
staff staff;
printf("-----------ID-----------\n");
scanf("%s", staff.ID);
printf("-----------name-----------\n");
scanf("%s", staff.name);
printf("-----------sex-----------\n");
scanf("%s", staff.sex);
printf("-----------pay-----------\n");
scanf("%d", &staff.pay);
printf("-----------reward-----------\n");
scanf("%d", &staff.reward);
staff.factpay = staff.pay + staff.reward;
printf("\n");
return staff;
}
void Displaystaff(staff staff)
{
printf("%10s", staff.ID);
printf("%10s", staff.name);
printf("%10s", staff.sex);
printf("%10d", staff.pay);
printf("%10d", staff.reward);
printf("%10d", staff.factpay);
printf("\n");
}
void updatestaff(staff *Staff)
{
printf("----- Please display the data to be modified --------\n");
Displaystaff(*Staff);
printf("------- Please enter the data to modify ---------");
printf("-----------pay-----------\n");
scanf("%d", &Staff->pay);
printf("-----------reward-----------\n");
scanf("%d", &Staff->reward);
Staff->factpay = Staff->pay + Staff->reward;
printf("\n");
}
2. Creation of linked lists
The header file of the linked list
linklist.h
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
#include "staff.h"
// List node creation
struct Node
{
struct staff Staff;
struct Node *next;
};
// Custom node
typedef struct Node node;
typedef struct Node *linklist;
// Create a list
node *Createlinklist();
// Output the data in the linked list
void Displaylinklist(node *head);
// Find the employee by the employee number
node *searchnode(node *head, char ID[]);
// Find the employee by name
void searchnodebyname(node *head, char name[]);
// Remove the worker
void delenode(linklist head, char ID[]);
// Insert the worker
void insertnode(linklist head, staff Staff);
// List to destroy
void distroylinklist(linklist head);
#endif // LINKLIST_H_INCLUDED
List creation of the source
linklist.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "staff.h"
#include "linklist.h"
node *Createlinklist()
{
node *head, *p;
head = (node *)malloc(sizeof(node));
head->next = NULL;
staff a[100] = {{"11111", "mmm", "f", 12000, 2000, 14000},
{"22222", "aaa", "m", 13000, 3000, 16000},
{"33333", "sss", "f", 15000, 3000, 18000},
{"44444", "fff", "m", 17000, 8000, 25000},
{"55555", "ggg", "f", 20000, 5000, 25000}};
for(int i = 0; i<5; i++)
{
p = (node *)malloc(sizeof(node));
p->Staff = a[i];
p->next = head->next;
head->next = p;
}
return head;
}
void Displaylinklist(node *head)
{
linklist p;
p = head->next;
while(p!=NULL)
{
Displaystaff(p->Staff);
p = p->next;
}
}
node *searchnode(node *head, char ID[])
{
linklist p;
p = head;
while(p!=NULL&&strcmp(p->next->Staff.ID, ID)!=0)
{
p = p->next;
}
return p->next;
}
void searchnodebyname(node *head, char name[])
{
linklist p;
p = head;
while((p!=NULL)&&(strcmp((p->next)->Staff.name, name)!=0))
{
p = p->next;
}
printf("-----´ËÈËΪ---------\n");
printf("%s", p->next->Staff.name);
printf("\n");
}
void delenode(linklist head, char ID[])
{
linklist p;
p = head;
while(p->next&&(strcmp(p->next->Staff.ID, ID)!=0))
{
p = p->next;
}
if(p->next)
{
p->next = p->next->next;
}
else
{
printf("=====NO FOUND========\n");
}
}
void insertnode(linklist head, staff Staff)
{
linklist p;
p = (node *)malloc(sizeof(node));
p->Staff = Staff;
p->next = head->next;
head->next = p;
}
void distroylinklist(linklist head)
{
linklist p;
p = head;
while(p!=NULL)
{
p = p->next;
free(p);
}
}
3. File saving
file.h
#ifndef FILE_H_INCLUDED
#define FILE_H_INCLUDED
#include "linklist.h"
#include "staff.h"
// Employee information storage
void saveinformation(linklist head );
// Employee information loading
void loadinformation(linklist head );
#endif // FILE_H_INCLUDED
file.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "file.h"
#include "linklist.h"
#include "staff.h"
void saveinformation(linklist h )
{
FILE *fp;
linklist p;
if ( (fp = fopen("stu.txt","w") ) == NULL)
{
printf("Failure to open stu.txt!\n");
exit(0);
}
for ( p = h->next; p; p=p->next )
{
fwrite( &(p->Staff), sizeof(node), 1, fp);
}
fclose(fp);
}
void loadinformation( linklist h )
{
FILE *fp;
staff nodeBuffer;
if ((fp = fopen("stu.txt","r")) == NULL)
{
printf("\n\t Data file missing or running for first time , The test data will be loaded \n");
return ;
}
while( fread(&nodeBuffer, sizeof(node), 1, fp)!=0 )
{
insertnode(h, nodeBuffer);
}
}
4. The main function
mainmeun.cpp
#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
#include "staff.h"
#include "file.h"
void mainmeun(linklist head);
void searchmenu(linklist head);
int main(void)
{
linklist head=NULL;
//int n;
//printf("------ Please enter the data you want to save ----------\n");
//scanf("%d", &n);
head = Createlinklist();
system("cls");
//Displaylinklist(head);
mainmeun(head);
printf("\n\n");
//loadinformation(head);
//saveinformation(head);
return 0;
}
void mainmeun(linklist head)
{
linklist p;
char ID[10];
//char name[10];
staff Staff;
int selection;
int flag = 1;
do
{
printf("================= Staff management system ===================\n");
printf("==========1. List the output =====2. Data query =====\n");
printf("=======3. To delete data ===4. Data modification =====5. Add data ======\n");
printf("=======6. List to destroy ===7. Inventory information =====8. Give up inventory =====\n");
printf("==================================================\n");
printf("====== Please select function ( 1~8 ) : ");
scanf("%d", &selection);
switch(selection)
{
case 1:
Displaylinklist(head);
break;
case 2:
searchmenu(head);
break;
case 3:
printf("========= Please enter your job number ==========\n");
scanf("%s", ID);
delenode(head, ID);
break;
case 4:
printf("========= Please enter your job number ==========\n");
scanf("%s", ID);
p = searchnode(head, ID);
updatestaff(&(p->Staff));
break;
case 5:
printf("======== Add data =========");
Staff = Createstaff();
insertnode(head, Staff);
break;
case 6:
distroylinklist(head);
break;
case 7:
loadinformation(head);
saveinformation(head);
break;
case 8:
flag = 0;
break;
}
}while(flag == 1);
printf("========BYE=====BYE======");
}
void searchmenu(linklist head)
{
linklist p;
int flag = 1;
char ID[10];
char name[10];
do
{
printf("========= To find the menu ===========\n");
printf("===1.ID======2.name====3. exit ====\n");
printf("=================================\n");
int selection;
printf("== Please select function ( 1~3 ) : ");
scanf("%d", &selection);
switch(selection)
{
case 1:
printf("===== Please enter the ID=======\n");
scanf("%s", ID);
p = searchnode(head, ID);
Displaystaff(p->Staff);
break;
case 2:
printf("===== Please enter the name======\n");
scanf("%s", name);
searchnodebyname(head, name);
break;
case 3:
flag = 0;
break;
}
system("pause");
system("cls");
}while(flag == 1);
}
Recommended articles:
C++ to achieve a simple library management system
C++ implements a simple employee information management system
C++ basic student management system
For more information, please refer to the topic "management system development".