#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<malloc.h>
using namespace std;
// Function result status code
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status; //Status is the type of function, and its value is the function result status code, such as OK.
//——————- dynamic allocation sequential storage structure of linear table ---//
#Define list? Init? Size 20 / / initial allocation of linear table storage space
#Define list add Size 10 / / allocation increment of linear table storage space
typedef struct
{
char id[10]; /* Staff number */
char name[20]; /* Employee name */
char department[20]; /* Department in charge */
float salary; /* Wages of staff and workers */
char postion[20]; /* position */
}ElemType;
typedef struct
{
ElemType *elem; //Storage base address
int length; //Current table length (number of elements in current table)
int listsize; //Currently allocated storage capacity in sizeof(ElemType)
}SqList;
Status SqList_Init(SqList &L) //Initialize, construct a new linear table L
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem) exit(OVERFLOW); //Storage memory allocation failed
L.length = 0; //Empty table length is 0
L.listsize = LIST_INIT_SIZE; //Initialize storage capacity
return OK;
}
int ListLength(SqList L) //Return the number of data elements in L
{
return L.length;
}
//Returns the value of the ith element in the table
ElemType GetElem(SqList L, int i)
{
if (i<1 || i>L.length) exit(ERROR); //Illegal i value
return L.elem[i - 1];
}
//Insert a new element at the end of the table
Status SqList_Insert(SqList &L, ElemType e)
{
ElemType *p, *q, *newbase = NULL;
if (L.length >= L.listsize) //Current memory is full, increase memory, reallocate
{
newbase = (ElemType *)realloc(L.elem, (L.listsize + LIST_ADD_SIZE) * sizeof(ElemType));
if (!newbase) exit(OVERFLOW); //Storage memory allocation failed
L.elem = newbase; //New base address
L.listsize += LIST_ADD_SIZE; //Storage capacity increase
}
int i = L.length + 1;
q = &(L.elem[i - 1]); //q is the insertion position
for (p = &(L.elem[L.length - 1]); p >= q; --p) *(p + 1) = *p;
//Insert position and elements after move right
*q = e; //Insert e into the position of the ith element
++L.length; //Table length plus one
return OK;
}
//Delete the ith element in the sequential linear table
Status SqList_Delete(SqList &L, int i)
{
//The legal value of i is 1 < = i < = L.Length
ElemType *p, *q;
if (i < 1 || i > L.length) return ERROR; //Illegal i value
p = &L.elem[i - 1]; //p is the location of the deleted element
q = L.elem + L.length - 1; //The position of the tail element
for (; p <= q; ++p) *(p - 1) = *p; //Move element left after deleted element
--L.length;
return OK;
}
// Compare whether it is the same person (whether id numbers are equal)
Status Equal(ElemType a, ElemType b)
{
if (!strcmp(a.id,b.id)) return TRUE;
else return FALSE;
}
//Find the element whose first value satisfies the compare relationship with e in L, and return its subscript in the table
int SqList_LocateElem(SqList L, ElemType e,
Status(*compare)(ElemType, ElemType)) //Parameter compare() is a function pointer
{
int i = 1; //The initial value of i is the bit order of the first element
ElemType *p; //The initial value of p is the storage location of the first element
p = L.elem;
while (i <= L.length && !(*compare)(*p++, e)) ++i;
if (i <= L.length) return i; //If it is found, its bit order in L will be returned; otherwise, 0 will be returned.
else return 0;
}
// Get the entered employee information
ElemType GetEmpInfomation()
{
ElemType e;
printf("Please enter employee number:");
scanf("%s",&(e.id));
printf("Please enter the name of the employee:");
scanf("%s", &(e.name));
printf("Please enter position:");
scanf("%s", &(e.postion));
printf("Please enter Department:");
scanf("%s", &(e.department));
printf("Please enter salary:");
scanf("%f", &(e.salary));
printf("\n");
return e;
}
// Insert new employee information
void AddEmpToList(SqList &L)
{
ElemType e = GetEmpInfomation();
int res = SqList_LocateElem(L, e, Equal);
if(res == 0)//This employee information does not exist
{
SqList_Insert(L, e);
printf("Added successfully! Do you want to continue adding? One:yes;0:no\n");
int c = -1;
scanf("%d",&c);
if(c == 1){
AddEmpToList(L);
}else if(c == 0){
printf("\n");
return;
}else{
printf("Illegal input, please operate again!\n\n");
return;
}
}else{//This employee information already exists
printf("Employee information of this number already exists, please do not repeat!\n\n");
return;
}
}
// Show employee information
void showEmpInfo(SqList L)
{
printf("\n Please enter the employee number to view:");
char id[10];
scanf("%s",id);
ElemType e;
strcpy(e.id,id);
int res = SqList_LocateElem(L, e, Equal);
if(res != 0){
e = GetElem(L, res);
printf("Staff number\t Full name\t department\t position\t salary\n");
printf("%s\t%s\t%s\t%s\t%.2f\n\n", e.id, e.name, e.department, e.postion, e.salary);
}else{
printf("This employee does not exist!\n\n");
}
}
// Delete an employee's information
void DeleteEmp(SqList &L)
{
if(L.length == 0){
printf("Currently, there is no employee information. Please select add employee information first!\n");
return;
}
printf("Please enter the employee number to delete:");
char id[10];
scanf("%s",id);
ElemType e;
strcpy(e.id,id);
int res = SqList_LocateElem(L, e, Equal);
if(res != 0){
printf("Are you sure you want to delete? Determine:1; give up:0\n");
int c;
scanf("%d",&c);
if(c == 1){
SqList_Delete(L, res + 1);//The following table of the i+1 = elements is i
printf("Delete successfully!\n\n");
}else{
printf("This operation has been cancelled!\n\n");
}
}else{
printf("This employee does not exist!\n\n");
}
}
//Print employee information form
void Print(SqList L)
{
printf("\n Staff number\t Full name\t department\t position\t salary\n");
ElemType p;
for (int i = 0; i < L.length; i++)
{
p = L.elem[i];
printf("%s\t%s\t%s\t%s\t%.2f\n", p.id, p.name, p.department, p.postion, p.salary);
}
printf("\n\n");
}
// Main menu
void menu(SqList &L)
{
bool flag = true;
while(flag)
{
flag = true;
printf("======================================================\n");
printf("\t\t Welcome to the company's employee information management system\n");
printf("======================================================\n");
printf("\t\t1.Increase employee information\n");
printf("\t\t2.Find employee information\n");
printf("\t\t3.Display all employee information\n");
printf("\t\t4.Delete employee information\n");
printf("\t\t5.Sign out\n");
printf("======================================================\n");
printf("Please select 1-5: ");
int c;
scanf("%d",&c);
switch(c)
{
case 1: AddEmpToList(L);break;
case 2: showEmpInfo(L);break;
case 3: Print(L);break;
case 4: DeleteEmp(L);break;
case 5: flag = false; printf("\n You have quit!\n");break;
default: printf("Illegal input!\n\n");break;
}
}
}
int main()
{
SqList L; //Define SqList structure L
SqList_Init(L); //Initialize L
menu(L);
system("pause");
}