*** WRITE CODE IN C LANGUAGE PLEASE *** Problem: Mother Goose Department Store h
ID: 3563019 • Letter: #
Question
*** WRITE CODE IN C LANGUAGE PLEASE ***
Problem: Mother Goose Department Store has decided to hire your software company to build a database for their organization. You have been selected to work on the database project. At the last team meeting, your team lead divided the final design tasks. Your assignment is to read in Employee data and provide methods for querying this information.
Objectives: The objective of this assignment is for students to demonstrate a working knowledge of :
- structs
- linked list operations
In your program you are required to:
1. Create an employeeData struct to represent employees. Your struct should have the following fields:
- An integer EMP_ID,
- name which stores the candidate
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct employeeData{
int EMP_ID;
char name[20];
int dep;
int rank;
double salary;
}emp;
typedef struct node{
struct employeeData emp;
struct node* next;
}node;
node* head=NULL;
node* initializelist(){
emp E;
FILE *fp;
fp = fopen("empinfo.txt","r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file. ");
exit(EXIT_FAILURE);
}
int id;
char* nam;
nam=(char*)malloc(20*sizeof(char));
int a,b;
double c;
fscanf(fp, "%d %s %d %d %lf", &id, nam, &a, &b,&c);
E.EMP_ID=id;
strcpy(E.name, nam);
E.dep=a;
E.rank=b;
E.salary=c;
head=(node*)malloc(sizeof(node));
head->emp=E;
head->next=NULL;
node* p=head;
node* temp;
while(fscanf(fp, "%d %s %d %d %lf", &id,nam, &a, &b,&c) != EOF) {
temp=(node*)malloc(sizeof(node));
E.EMP_ID=id;
strcpy(E.name, nam);
E.dep=a;
E.rank=b;
E.salary=c;
temp->emp=E;
p->next=temp;
temp->next=NULL;
p=temp;
}
fclose(fp);
return head;
}
node* add(){
int a,b,c;
double d;
char nam[20];
scanf("%d %s %d %d %lf",&a,nam,&b,&c,&d);
emp e;
e.EMP_ID=a;
strcpy(e.name, nam);
e.dep=b;
e.rank=c;
e.salary=d;
if(head==NULL){
head=(node*)malloc(sizeof(node));
head->emp=e;
head->next=NULL;
printf("Addition complete ");
return head;
}
node* p=head,*q=NULL;
while(p->emp.EMP_ID<a){
q=p;
p=p->next;
}
node* temp=(node*)malloc(sizeof(node));
if(p==head){
temp->emp=e;
temp->next=head;
head=temp;
printf("Addition complete ");
return head;
}
temp->next=p;
q->next=temp;
printf("Addition complete ");
return head;
}
node* deleteemp(int id){
node* p=head,*q;
while(p->emp.EMP_ID!=id){
q=p;
p=p->next;
}
if(p==head){
head=p->next;
free(p);
printf("deletion completed ");
return head;
}
q->next=p->next;
free(p);
printf("deletion completed ");
return head;
}
node* modify(int id,double s){
node* p=head,*q;
while(p->emp.EMP_ID!=id){
p=p->next;
}
if(p!=NULL){
p->emp.salary=s;
}
printf("modification completed ");
return head;
}
void query(int r){
node* p=head;
while(p!=NULL){
if(p->emp.rank==r){
printf("names of employee with rank %d ",r);
printf("%s ",p->emp.name);
}
p=p->next;
}
}
void print(){
node* p=head;
printf("DETAILS OF ALL EMPLOYEES ");
while(p!=NULL){
printf("%d %s %d %d %lf ",p->emp.EMP_ID,p->emp.name,p->emp.dep,p->emp.rank,p->emp.salary);
p=p->next;
}
}
int main(){
head=initializelist();
int choice,id,r;
double s;
while(1){
printf("******menu***** ");
printf("1. ADD EMPLOYEE ");
printf("2. DELETE EMPLOYEE 3. MODIFY SALARY 4.QUERY 5.PRINT DETAILS ");
printf("ENTER your choice ");
scanf("%d",&choice);
if(choice==1){
head=add();
}
if(choice==2){
printf("Enter emp id to delete ");
scanf("%d",id);
head=deleteemp(id);
}
if(choice==3){
printf("Enter emp id and modified salary ");
scanf("%d %lf",&id,&s);
head=modify(id,s);
}
if(choice==4){
printf("Enter rank ");
scanf("%d",&r);
query(r);
}
if(choice==5){
print();
}
if(choice==0)
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.