a. First Name (firstName)
b. Last Name (lastName)
c. Employee ID (id)
d. Start Year (startYear)
e. Starting Salary (startSalary)
f. Current Salary (currentSalary)
g. next
Create a library of functions that operate on this data structure. The source code for the functions should be in lastname_employeerec.c and the function prototypes should be included in lastname_employeerec.h. The following functions should be in the library:
Add the following functions to your program’s library of functions:
1 – add a new employee record
2 – delete an employee record
3 – sort employee records by last name
4 – print all employee records
5 – write all employee records to a file
6 – read employee records from a file
7 – search employee records (last & first names)
8 – search employee records (employee id)
9 – update employee record (employee id)
10 – update employee record (current salary)
0 – exit
In C language please
#include <stdio.h>
typedef struct EMPLOYEE {
char* firstName;
char* lastName;
unsigned int id ;
int startYear;
double startSalary;
double currentSalary;
struct EMPLOYEE* next;
} sEMPLOYEE;
int showMenu();
void deleteRecord(sEMPLOYEE* rec );
void deleteList(sEMPLOYEE* employeeList );
sEMPLOYEE* create_sEMPLOYEE_record();
sEMPLOYEE* add_sEMPLOYEE_records(sEMPLOYEE* sEMPLOYEEList,
sEMPLOYEE* rec);
sEMPLOYEE* delete_sEMPLOYEE_record(sEMPLOYEE* sEMPLOYEEList
,unsigned int ID );
void print_sEMPLOYEE_record(sEMPLOYEE *sEMPLOYEE);
sEMPLOYEE* sort_sEMPLOYEE_records(sEMPLOYEE *sEMPLOYEEList);
int write_sEMPLOYEE_record(char* filename, sEMPLOYEE*
sEMPLOYEEList);
sEMPLOYEE* read_employee_record(char* filename, sEMPLOYEE*
employeeList );
############# c file ###########
#include "lastname_employee.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int showMenu()
{
printf(" Enter your choice from following : \n");
printf(" 1. add a new employee record.\n");
printf(" 2.delete an employee record.\n");
printf(" 3. sort employee record by last name\n");
printf(" 4. print all employee records.\n");
printf(" 5. Write all employee records to a file.\n");
printf(" 6. read employee records from a file.\n");
printf(" 7. exit .\n");
int ip ;
char c1 ;
scanf("%d%c", &ip , &c1 );
//printf(" GOt - %d \n",ip);
return ip ;
}
sEMPLOYEE* create_sEMPLOYEE_record()
{
char line[1024] ;
char * buf = line ;
size_t len = 50;
size_t x = 0;
sEMPLOYEE* nRec = (sEMPLOYEE *) malloc( sizeof(sEMPLOYEE));
nRec->next = NULL;
printf("Enter LastName : " );
x = getline( &buf, &len , stdin);
buf[x-1] = 0;
//printf(" Got line %s , len %x , %d \n",buf,len, x);
nRec->lastName = (char* ) malloc( sizeof(char) * x );
strncpy(nRec->lastName,buf,x);
printf("Enter first Name : " );
x = getline( &buf, &len , stdin);
buf[x-1] = 0;
//printf(" Got line %s , len %x , %d \n",buf,len, x);
nRec->firstName = (char* ) malloc( sizeof(char) * x );
strncpy(nRec->firstName,buf,x);
printf("Enter Employee ID : " );
x = getline( &buf, &len , stdin);
buf[x-1] = 0;
//printf(" Got line %s , len %x , %d \n",buf,len, x);
nRec->id = atoi(buf);
printf("Enter Start Year : " );
x = getline( &buf, &len , stdin);
buf[x-1] = 0;
//printf(" Got line %s , len %x , %d \n",buf,len, x);
nRec->startYear = atoi(buf);
printf("Enter Starting Salary : " );
x = getline( &buf, &len , stdin);
buf[x-1] = 0;
//printf(" Got line %s , len %x , %d \n",buf,len, x);
nRec->startSalary = atof(buf);
printf("Enter current Salary : " );
x = getline( &buf, &len , stdin);
buf[x-1] = 0;
//printf(" Got line %s , len %x , %d \n",buf,len, x);
nRec->currentSalary = atof(buf);
return nRec;
}
sEMPLOYEE* add_sEMPLOYEE_records(sEMPLOYEE* employeeList,
sEMPLOYEE* rec)
{
if ( employeeList != NULL )
rec->next = employeeList;
return rec;
}
sEMPLOYEE* delete_sEMPLOYEE_record(sEMPLOYEE* employeeList
,unsigned int ID )
{
sEMPLOYEE* tmpNode = employeeList;
sEMPLOYEE* pNode = NULL;
sEMPLOYEE* head = employeeList;
while ( tmpNode != NULL )
{
if ( tmpNode->id == ID )
break;
pNode = tmpNode;
tmpNode = tmpNode->next;
}
// if found tmpNode
if ( tmpNode != NULL )
{
if ( pNode != NULL )
{
pNode->next = tmpNode->next ;
} else
{
// tmpNode is head
head = tmpNode->next ;
}
tmpNode->next = NULL;
}
deleteRecord(tmpNode);
return head;
}
void print_sEMPLOYEE_record(sEMPLOYEE * rec)
{
printf(" Employee ID : %d \n" , rec->id);
printf(" Employee last Name : %s \n" , rec->lastName);
printf(" Employee first Name : %s \n" , rec->firstName);
printf(" Employee Start year - %4d \n" , rec->startYear);
printf(" Employee start Salary : %.2f \n" ,
rec->startSalary);
printf(" Employee current Salary : %.2f \n" ,
rec->currentSalary);
return;
}
sEMPLOYEE* sort_sEMPLOYEE_records(sEMPLOYEE *
employeeList)
{
sEMPLOYEE* tmpNode = NULL;
sEMPLOYEE* head = employeeList;
sEMPLOYEE* pNode = NULL;
sEMPLOYEE* tmp1Node = NULL;
bool flag = false ;
sEMPLOYEE* sortedList = NULL;
while ( head != NULL )
{
tmpNode = head ;
head = tmpNode->next ;
tmpNode->next = NULL;
tmp1Node = sortedList ;
pNode = NULL;
while ( tmp1Node != NULL )
{
if ( strcmp( tmpNode->lastName, tmp1Node->lastName) < 0
)
break;
pNode = tmp1Node;
tmp1Node = tmp1Node->next;
}
if ( tmp1Node != NULL )
{
tmpNode->next = tmp1Node ;
}
if ( pNode != NULL )
{
pNode->next = tmpNode ;
} else {
sortedList = tmpNode ;
}
}
return sortedList;
}
int write_sEMPLOYEE_record(char* filename, sEMPLOYEE*
employeeList)
{
FILE * fp = fopen(filename,"w");
if ( fp == NULL ) {
fprintf(stderr," Cannot open file %s \n" , filename );
return -1;
}
sEMPLOYEE* tmpNode = employeeList;
while (tmpNode != NULL )
{
fprintf(fp,"%s,%s,%d,%4d,%.2f,%.2f\n",
tmpNode->lastName,tmpNode->firstName,
tmpNode->id,tmpNode->startYear, tmpNode->startSalary,
tmpNode->currentSalary);
tmpNode = tmpNode->next;
}
return 0;
}
sEMPLOYEE* read_employee_record(char* filename, sEMPLOYEE*
employeeList )
{
FILE * fp = fopen(filename,"r");
if ( fp == NULL ) {
fprintf(stderr," Cannot open file %s \n" , filename );
return employeeList;
}
char line[1024];
char *buf = line ;
size_t len = 150;
int x = 0 ;
char *tok ;
char s[2] = ",";
sEMPLOYEE* nNode = NULL;
sEMPLOYEE* nList = employeeList;
while ( getline(&buf , &len, fp) > 0 ) {
//printf(" read line - %s \n", buf);
nNode = (sEMPLOYEE*) malloc( sizeof(sEMPLOYEE) );
tok = strtok(buf,s );
//printf("Got tok %s \n",tok);
if (tok != 0 )
{
x = strlen(tok);
nNode->lastName = (char* ) malloc( sizeof(char) * x );
strncpy(nNode->lastName,tok,x);
}
tok = strtok(NULL, s);
if (tok != 0 )
{
x = strlen(tok);
nNode->firstName = (char* ) malloc( sizeof(char) * x );
strncpy(nNode->firstName,tok,x);
}
tok = strtok(NULL, s);
if (tok != 0 )
nNode->id = atoi(tok);
tok = strtok(NULL, s);
if (tok != 0 )
nNode->startYear = atoi(tok);
tok = strtok(NULL, s);
if (tok != 0 )
nNode->startSalary = atof(tok);
tok = strtok(NULL, s);
if (tok != 0 )
nNode->currentSalary = atof(tok);
//print_sEMPLOYEE_record(nNode);
nList = add_sEMPLOYEE_records(nList , nNode);
}
return nList;
}
void deleteRecord(sEMPLOYEE* rec )
{
if ( rec != NULL )
{
if ( rec->lastName != NULL)
{
free( rec->lastName );
//printf("Deleting last");
}
if ( rec->firstName != NULL)
{
free( rec->firstName );
//printf("Deleting fist ");
}
//printf("Deleting rec\n");
free(rec);
}
}
void deleteList(sEMPLOYEE* employeeList )
{
sEMPLOYEE* tNode = employeeList;
sEMPLOYEE* pNode = NULL;
while (tNode != NULL )
{
pNode = tNode ;
tNode = tNode->next ;
deleteRecord(pNode);
}
}
int main()
{
sEMPLOYEE* xRec = NULL;
sEMPLOYEE* recordList = NULL;
int input = 0 ;
char filename[25] ;
int id ;
sEMPLOYEE* tmpNode = recordList;
while ( input != 7 )
{
input = showMenu();
switch( input) {
case 1 :
xRec = create_sEMPLOYEE_record();
recordList = add_sEMPLOYEE_records(recordList,xRec);
break;
case 2 :
printf(" Enter the Employee ID to search :");
scanf("%d" ,& id );
recordList = delete_sEMPLOYEE_record(recordList, id);
break;
case 3:
recordList = sort_sEMPLOYEE_records(recordList);
case 4 :
tmpNode = recordList;
while(tmpNode != NULL)
{
print_sEMPLOYEE_record(tmpNode);
tmpNode = tmpNode->next;
}
break;
case 5 :
printf(" Enter the File to Write :");
scanf("%s" , filename );
write_sEMPLOYEE_record(filename, recordList);
break;
case 6 :
printf(" Enter the File to Read :");
scanf("%s" , filename );
recordList = read_employee_record(filename, recordList);
break;
case 7 :
break;
}
}
deleteList(recordList);
}
===========================================================================
Note: Could you please consider my effort on this work and give me a UPVOTE. Thank you :)
Get Answers For Free
Most questions answered within 1 hours.