Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

libel.c #include \"libel.h\" #include <string.h> #include <stdio.h> #include <st

ID: 3714189 • Letter: L

Question

libel.c

#include "libel.h"

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

//ADD FUNCTION DEFINITIONS FOR LOAD_EL, SAVE_EL, ADD_PERSON, AND SERACH_EL HERE

//ADD FUNCTIONALITY TO THE BELOW FUNCTIONS

void gen_csv_sal(el * emp_list){

char csv_prefix[14] = "csv_list_sal";

char csv_suffix[5] = ".csv";

char * filename = gen_file_name(csv_prefix, 14, csv_suffix, 5);

FILE * fp = fopen(filename, "w");

int search = -1;

double sal;

char ml[5];

while(search == -1) {

printf("Search for salaries (format: 65000.00 less or 100000 more): ");

scanf("%lf %s", &sal, ml);

if(/*if 'ml' is "less"*/) {

//ADD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN

search = 0;

}

else if(/*if 'ml' is "more"*/) {

//AD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN

search = 0;

}

else

printf("Must type more or less ");

}

fclose(fp);

printf("CSV generated: %s ", filename);

return;

}

//ADD FUNCTIONALITY TO THE BELOW FUNCTION

void gen_csv_pos(el * emp_list){

char csv_prefix[14] = "csv_list_pos";

char csv_suffix[5] = ".csv";

char * filename = gen_file_name(csv_prefix, 14, csv_suffix, 5);

FILE * fp = fopen(filename, "w");

  

char pos[25];

printf("Enter company position to search for: ");

scanf("%s", pos);

  

//ADD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN

fclose(fp);

printf("CSV generated: %s ", filename);

return;

}

char * gen_file_name(char * filename, int filename_size, char * suffix, int suffix_size){

char file_num[3];

int max_file_num = 99, i;

char * new_filename = (char *) malloc((filename_size + suffix_size + 2) * sizeof(char));

for(i = 0;i <= max_file_num; i++){

strcpy(new_filename, filename);

sprintf(file_num, "%d", i);

strcat(new_filename, file_num);

strcat(new_filename, suffix);

if (fopen(new_filename, "r") ==NULL) {//if the filename doesnt exsist

//fp = fopen(new_filename, "w");

break;

}

}

// printf("File name generated: %s ", new_filename);

  

return new_filename;

}

------------------------------------

libel.h

#ifndef _LIBCL_H_

#define _LIBCL_H_

#define MAXPPL 500

#define MAXLEN 25

//ADD STRUCTURE(S) HERE

//ADD PROTOTYPES HERE

void gen_csv_sal(el * emp_list);

void gen_csv_pos(el * emp_list);

char * gen_file_name(char * filename, int filename_size, char * suffix, int suffix_size);

#endif

-------------------------------------

employee_list.c

#include <stdio.h>

#include <string.h>

#include "libel.h"

int main () {

const char * filename = "directory.txt";

char * csv_prefix = "csv_list";

char find_name[25];

pi person;

el emp_list;

emp_list.num_people = 0;

load_el(&emp_list, filename);

printf("%d employees loaded. ", emp_list.num_people);

char cont = 'x', //initialize continue to something other than 'Y' or 'N'

   tmp; //

int state = -1,

correct_search_val;

char srch_critera[25] = "init";

while (state != 4){

while(state < 1 || state > 4){

printf("1: Add employee and salary ");

printf("2: Search directory by first name ");

printf("3: Generate CSV ");

printf("4: Save and exit program ");

printf("Enter an option (1-4): ");

scanf("%d", &state);

if(state < 1 || state > 4) {

printf(" Error: number not in range ");

}

}

switch(state) {

//add employee and salary

case 1:

//add one person

printf("Enter a first name: ");

scanf("%s", person.first);

printf("Enter %s's last name: ", person.first);

scanf("%s", person.last);

printf("Enter %s's occupation: ", person.first);

scanf("%s", person.position);

printf("Enter %s's Salary: ", person.first);

scanf("%lf", &person.salary);

scanf("%c", &tmp);

printf("Employee added. ");

add_person(&emp_list, person);

//determine to add more people to the employee list

while(cont != 'N' && emp_list.num_people < MAXPPL) {

cont = 'x';

while(cont != 'Y' && cont != 'N'){

printf("Would you like to enter another name (Y/N): ");

scanf("%c", &cont);

if (cont != 'Y' && cont != 'N') {

printf("Error: User entered '%c'. Must enter either 'Y' or 'N' ", cont);

}

scanf("%c", &tmp);

}

if(cont != 'N') {

printf("Enter a first name: ");

scanf("%s", person.first);

printf("Enter %s's last name: ", person.first);

scanf("%s", person.last);

printf("Enter %s's occupation: ", person.first);

scanf("%s", person.position);

printf("Enter %s's Salary: ", person.first);

scanf("%lf", &person.salary);

scanf("%c", &tmp);

printf("Employee added. ");

add_person(&emp_list, person);

}

}

printf(" Returning to main menu... ");

state = -1;

break;

//search directory by first name

case 2:

cont = 'x'; //reset continue to neither 'Y' nor 'N'

while(cont != 'N'){

cont = 'x';

printf("Enter a person's name to search for: ");

scanf("%s", find_name);

scanf("%c", &tmp);

search_el(emp_list, find_name);

while(cont != 'Y' && cont != 'N'){

printf(" Continue (Y/N)? ");

  

scanf("%c", &cont); fflush(stdout); //, &tmp);

scanf("%c", &tmp);

if (cont != 'Y' && cont != 'N') {

printf("Error: User entered '%c'. Must enter either 'Y' or 'N'. ", cont);

}

}

}

printf(" Returning to main menu... ");

state = -1;

break;

  

//generate CSV file

case 3:

correct_search_val = -1;

while(correct_search_val != 0) {

printf("Generate CSV based on? ("Salary", "Position"): ");

scanf("%s", srch_critera);

if(!strcmp(srch_critera, "Salary")){

printf("Generating CSV based on salary... ");

gen_csv_sal(&emp_list);

correct_search_val = 0;

}

else if(!strcmp(srch_critera, "Position")){

printf("Generating CSV based on position... ");

gen_csv_pos(&emp_list);

correct_search_val = 0;

}

else

printf("Options are: "Salary", "Position" ");

}

printf("Returning to main menu... ");

state = -1;

case 4:

break;

}//end switch

}//end while

//save the employee list

save_el(&emp_list, filename);

printf("%d employees saved. ", emp_list.num_people);

return 0;

}//end main

aRImum allowed grace days: Five Plesse note that this is a purely hypothetical situation. This is not a real event Problem Description: The company XYZ is looking for a system to keep track of each em- ployee's wages. Each employee's name, salary and occupation must be stored in a database. The system must also be able to generate a comma separated value (CSV) file based on a particular position in the company (ie. engineer, secretary, painter, etc.) or all salaries above or below any given number (i.e. a list of people that make over/under s dollars/year). XYZ had complications with their previous intern which led to his termination before he could complete the program. As a result, most of the functionality of the employee-list system is still incomplete. They would like to hire EAS240 interns to complete this system in C programming language The intern completed the main file employee list.c, but, did not have any chanoe to complete most of the library files (the .c and the .h). As a result, the library files for the employee list (the list of all the employees and the required information) have not yet been completed. Each student must write their own implementation of the functions described below to complete the program and make it functional. The function prototypes are given, but must be added to libel.h and the corresponding definitions in libel.e. Main file (employee.list.c): Main function is found in employce.list.c and begins by loading the employees that are already saved in a file named "directory.txt". Loading the employee list is done by calling the function load.el). A state machine is then entered to determine what the user would like to do. Options 1-4 are given: 1 adds a employee, 2 searches all employees by first name, 3 generates a CSV, and 4 saves the changes to the file and exits the program. If any other number is entered, the program remains at this menu. A more detailed description of each option is given below If the user enters 1, the function add.person) is called, and the program enters a loop. The user is prompted to enter 'Y' or 'N'. If 'Y" is entered, the function is called again. The loop only breaks when 'N' is entered. The program then returns to the main menu. If the user enters 2, search el) is called. The user is prompted to enter Y' or 'N', if the user enters 'Y' then search.el() is called again. The searching continues until the user enters 'N'. The program then returns to the main menu.

Explanation / Answer

libel.h

#ifndef _LIBCL_H_

#define _LIBCL_H_

#define MAXPPL 500

#define MAXLEN 25

//ADD STRUCTURE(S) HERE

struct personal_info{

char first[MAXLEN];

char last[MAXLEN];

char position[MAXLEN];

double salary;

};

typedef struct personal_info pi;

//ADD PROTOTYPES HERE

struct employee_list{

pi peoples[MAXPPL];

int num_people;

};

typedef struct employee_list el;


void gen_csv_sal(el *emp_list);

void gen_csv_pos(el *emp_list);

char * gen_file_name(char *filename, int filename_size, char * suffix, int suffix_size);

void load_el(el *emp_list, const char * filename);

void add_person(el *emp_list, pi person);

void search_el(el emp_list, char find_name[]);

void save_el(el *emp_list, const char *filename);

#endif

libel.c

#include "libel.h"

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

//ADD FUNCTION DEFINITIONS FOR LOAD_EL, SAVE_EL, ADD_PERSON, AND SERACH_EL HERE

void load_el(el *emp_list, const char *filename)

{

FILE *fp;

fp = fopen(filename, "r");

if (fp == NULL)

{

printf("Unable to open file ");

exit(1);

}

pi person;

int n, i = 0;

fscanf(fp, "%d", &n);

while (i < n)

{

fscanf(fp, "%s %s %s %lf", person.first, person.last, person.position, &person.salary);

add_person(emp_list, person);

i++;

}

emp_list->num_people = i;

}

void add_person(el *emp_list, pi person)

{

emp_list->peoples[emp_list->num_people] = person;

emp_list->num_people++;

}

void search_el(el emp_list, char find_name[])

{

int found = 0, i = 0;

while (i < emp_list.num_people)

{

if (!strcmp(emp_list.peoples[i].first, find_name))

{

printf("%s %s, %s, %lf ", emp_list.peoples[i].first, emp_list.peoples[i].last, emp_list.peoples[i].position, emp_list.peoples[i].salary);

found = 1;

}

i++;

}

if (found == 0)

{

printf("No entries with that name. ");

}

}

void save_el(el *emp_list, const char *filename)

{

FILE *fp;

fp = fopen(filename, "w");

if (fp == NULL)

{

printf("Unable to open file ");

exit(1);

}

int i = 0;

while (i < emp_list->num_people)

{

fprintf(fp, "%s %s %s %lf ", emp_list->peoples[i].first, emp_list->peoples[i].last, emp_list->peoples[i].position, emp_list->peoples[i].salary);

i++;

}

}

//ADD FUNCTIONALITY TO THE BELOW FUNCTIONS

void gen_csv_sal(el *emp_list)

{

char csv_prefix[14] = "csv_list_sal";

char csv_suffix[5] = ".csv";

char *filename = gen_file_name(csv_prefix, 14, csv_suffix, 5);

FILE *fp = fopen(filename, "w");

int search = -1;

double sal;

char ml[5];

while (search == -1)

{

printf("Search for salaries (format: 65000.00 less or 100000 more): ");

scanf("%lf %s", &sal, ml);

if (!strcmp(ml, "less")) /*if 'ml' is "less"*/

{

//ADD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN

int i = 0;

while (i < emp_list->num_people)

{

if (emp_list->peoples[i].salary < sal)

{

fprintf(fp, "%s %s, %s, %lf ", emp_list->peoples[i].first, emp_list->peoples[i].last, emp_list->peoples[i].position, emp_list->peoples[i].salary);

printf("%s %s, %s, %lf ", emp_list->peoples[i].first, emp_list->peoples[i].last, emp_list->peoples[i].position, emp_list->peoples[i].salary);

search = 0;

}

i++;

}

}

else if (!strcmp(ml, "more")) /*if 'ml' is "more"*/

{

//AD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN

int i = 0;

while (i < emp_list->num_people)

{

if (emp_list->peoples[i].salary > sal)

{

fprintf(fp, "%s %s, %s, %lf ", emp_list->peoples[i].first, emp_list->peoples[i].last, emp_list->peoples[i].position, emp_list->peoples[i].salary);

printf("%s %s, %s, %lf ", emp_list->peoples[i].first, emp_list->peoples[i].last, emp_list->peoples[i].position, emp_list->peoples[i].salary);

search = 0;

}

i++;

}

}

else

printf("Must type more or less ");

}

fclose(fp);

printf("CSV generated: %s ", filename);

return;

}

//ADD FUNCTIONALITY TO THE BELOW FUNCTION

void gen_csv_pos(el *emp_list)

{

char csv_prefix[14] = "csv_list_pos";

char csv_suffix[5] = ".csv";

char *filename = gen_file_name(csv_prefix, 14, csv_suffix, 5);

FILE *fp = fopen(filename, "w");

char pos[25];

printf("Enter company position to search for: ");

scanf("%s", pos);

//ADD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN

int i = 0;

while (i < emp_list->num_people)

{

if (!strcmp(emp_list->peoples[i].position, pos))

{

fprintf(fp, "%s %s, %s, %lf ", emp_list->peoples[i].first, emp_list->peoples[i].last, emp_list->peoples[i].position, emp_list->peoples[i].salary);

printf("%s %s, %s, %lf ", emp_list->peoples[i].first, emp_list->peoples[i].last, emp_list->peoples[i].position, emp_list->peoples[i].salary);

}

i++;

}

fclose(fp);

printf("CSV generated: %s ", filename);

return;

}

char *gen_file_name(char *filename, int filename_size, char *suffix, int suffix_size)

{

char file_num[3];

int max_file_num = 99, i;

char *new_filename = (char *)malloc((filename_size + suffix_size + 2) * sizeof(char));

for (i = 0; i <= max_file_num; i++)

{

strcpy(new_filename, filename);

sprintf(file_num, "%d", i);

strcat(new_filename, file_num);

strcat(new_filename, suffix);

if (fopen(new_filename, "r") == NULL)

{ //if the filename doesnt exsist

//fp = fopen(new_filename, "w");

break;

}

}

// printf("File name generated: %s ", new_filename);

return new_filename;

}


employee_list.c

#include <stdio.h>

#include <string.h>

#include "libel.h"

int main()

{

const char *filename = "directory.txt";

char *csv_prefix = "csv_list";

char find_name[25];

pi person;

el emp_list;

emp_list.num_people = 0;

load_el(&emp_list, filename);

printf("%d employees loaded. ", emp_list.num_people);

char cont = 'x', //initialize continue to something other than 'Y' or 'N'

tmp; //

int state = -1,

correct_search_val;

char srch_critera[25] = "init";

while (state != 4)

{

while (state < 1 || state > 4)

{

printf("1: Add employee and salary ");

printf("2: Search directory by first name ");

printf("3: Generate CSV ");

printf("4: Save and exit program ");

printf("Enter an option (1-4): ");

scanf("%d", &state);

if (state < 1 || state > 4)

{

printf(" Error: number not in range ");

}

}

switch (state)

{

//add employee and salary

case 1:

//add one person

printf("Enter a first name: ");

scanf("%s", person.first);

printf("Enter %s's last name: ", person.first);

scanf("%s", person.last);

printf("Enter %s's occupation: ", person.first);

scanf("%s", person.position);

printf("Enter %s's Salary: ", person.first);

scanf("%lf", &person.salary);

scanf("%c", &tmp);

printf("Employee added. ");

add_person(&emp_list, person);

//determine to add more people to the employee list

while (cont != 'N' && emp_list.num_people < MAXPPL)

{

cont = 'x';

while (cont != 'Y' && cont != 'N')

{

printf("Would you like to enter another name (Y/N): ");

scanf("%c", &cont);

if (cont != 'Y' && cont != 'N')

{

printf("Error: User entered '%c'. Must enter either 'Y' or 'N' ", cont);

}

scanf("%c", &tmp);

}

if (cont != 'N')

{

printf("Enter a first name: ");

scanf("%s", person.first);

printf("Enter %s's last name: ", person.first);

scanf("%s", person.last);

printf("Enter %s's occupation: ", person.first);

scanf("%s", person.position);

printf("Enter %s's Salary: ", person.first);

scanf("%lf", &person.salary);

scanf("%c", &tmp);

printf("Employee added. ");

add_person(&emp_list, person);

}

}

printf(" Returning to main menu... ");

state = -1;

break;

//search directory by first name

case 2:

cont = 'x'; //reset continue to neither 'Y' nor 'N'

while (cont != 'N')

{

cont = 'x';

printf("Enter a person's name to search for: ");

scanf("%s", find_name);

scanf("%c", &tmp);

search_el(emp_list, find_name);

while (cont != 'Y' && cont != 'N')

{

printf(" Continue (Y/N)? ");

scanf("%c", &cont);

fflush(stdout); //, &tmp);

scanf("%c", &tmp);

if (cont != 'Y' && cont != 'N')

{

printf("Error: User entered '%c'. Must enter either 'Y' or 'N'. ", cont);

}

}

}

printf(" Returning to main menu... ");

state = -1;

break;

//generate CSV file

case 3:

correct_search_val = -1;

while (correct_search_val != 0)

{

printf("Generate CSV based on? ("Salary", "Position"): ");

scanf("%s", srch_critera);

if (!strcmp(srch_critera, "Salary"))

{

printf("Generating CSV based on salary... ");

gen_csv_sal(&emp_list);

correct_search_val = 0;

}

else if (!strcmp(srch_critera, "Position"))

{

printf("Generating CSV based on position... ");

gen_csv_pos(&emp_list);

correct_search_val = 0;

}

else

printf("Options are: "Salary", "Position" ");

}

printf("Returning to main menu... ");

state = -1;

case 4:

break;

} //end switch

} //end while

//save the employee list

save_el(&emp_list, filename);

printf("%d employees saved. ", emp_list.num_people);

return 0;

} //end main