PLEASE HELP C PROGRAM Problem Description : The company XYZ is looking for a sys
ID: 3718348 • Letter: P
Question
PLEASE HELP C PROGRAM
Problem Description: The company XYZ is looking for a system to keep track of each employee’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) ?le based on a particular position in the company (i.e. engineer, secretary, painter, etc.) or all salaries above or below any given number (i.e. a list of people that make over/under x 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 ?le employee list.c, but, did not have any chance to complete most of the library ?les (the .c and the .h). As a result, the library ?les 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 de?nitions in libel.c.
Main ?le (employee list.c): Main function is found in employee list.c and begins by loading the employees that are already saved in a ?le 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 ?rst name, 3 generates a CSV, and 4 saves the changes to the ?le 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.
If the user enters 3, a prompt is given to enter a string. If the string matches “Position” then the function gen_csv_pos() is called; if the string matches “Salary” then gen_csv_sal() is called. If any other string is entered, the user is reminded of the options and prompted again. After one of the functions is called, the program returns to the main menu.
If the user enters 4, the program breaks out of the state machine, saves the employee list (by calling save el()), and exits the program. Note that the employee list is only saved if the user enters 4, NOT if the user presses ctrl+c.
Note: The main function is given in employee list.c. The above is an explanation of main function. The employee list.c ?le should not be edited.
Structures: The system uses two structures. The ?rst structure holds information about each employee. This structure is to be named personal _info. personal_info is to have 3 character arrays (of length MAXLEN de?ned in libel.h) to hold the employee’s ?rst name, last name, position. The structure must also hold a double for the salary. In the line directly below the structure, add the following line:
typedef struct personal info pi;
This line literally renames ‘struct personal_info’ to ‘pi’. This means that create a new instance of this structure, ‘pi newpi;’ may be written instead of ‘struct personal_info newpi;’ (it saves quite a bit of typing).
The second structure is a list of all the employees. It contains an array of personal_info structures, and an integer to store the current number of people in the employee list. Name this structure employee_list. The maximum number of people in the array is MAXPPL, which is also de?ned in libel.h. Use ‘typedef struct employee list el;’ for this structure. Both structures must be declared ONLY in libel.h
Hint: MAXLEN is already de?ned to be 25, and may be found in libel.h. To make an array of length MAXLEN, use: <datatype> array[MAXLEN]; same with MAXPPL.
Functions:
1: Write a function to load a employee list from a ?le. A sample ?le may be seen in the supplemental materials directory.txt. This function must take a pointer to a employee_list, and a const char * string (which is the ?lename). The ?rst element in directory.txt is an integer - the number of people in the list. In this function, create a loop that initializes a employee list to the elements of a ?le (directory.txt). Use the prototype below:
void load el(el * emp list, const char * filename);
After all the employees have been loaded from the ?le, print ''%d employees loaded. '', where %d is the number of employees loaded.
Hint: fscanf(fp, "%s %s %s %lf",...); will read in a users ?rst name, last name, occupation, and salary from the ?le pointer fp. You may want to put this fscanf in a loop to load each employee in the ?le. You must still decide where to store the scanned variables (replace the “...”).
2: Write a function to add an employee personal_info (pi) to the employee list (el) using the prototype below:
void add person(el * emp list, pi person);
Add this prototype to libel.h and the de?nition to libel.c. Read the employee list.c carefully. The employee_list.c prompts the user to enter the information. After the new employee information is entered, the program will call the function add_person() and pass this information to the function. In your function de?nition you need to add this new employee to the employee_list.
3: Write a second function using the prototype below to search the employee list for a ?rst name.
void search el(el emp list, char find name[ ]);
Read the employee_list.c carefully. The employee _list.c prompts the user to enter a name to search for, and then it will pass this name to the function search_el. In your function de?nition you need to search for all the employees that match the searched ?rst name and print full information of each employee that has the same ?st name with the format seen in the provided executable ?le.
Hint: To compare two strings, include the string.h library in your libel.c and use the function strcmp(char *, char *); no linker ?ags are needed for this library. strcmp() returns 0 (or false) if the strings are the same. You can use !strcmp() to return true if they are the same. If there is no employee found, print 'No entries with that name. '.
4: Write a function by using the prototype below to save the employee list in the same manner as directory.txt.
void save el(el * emp list, const char * filename);
This can be done by replacing fscanf with fprintf in the load_el function. Keep in mind each ?eld of the personal info structure must be on its own line. So the fscanf mentioned in the hint above must be modi?ed (replace the spaces with linefeeds where necessary).
5: Complete the function gen_csv sal in libel.c by ?lling in the statements in the while loop. The ?rst if (in the while loop) should check if the variable (string) ml is equal to “less”. If the strings are equal, then generate a Comma Separated Variable (CSV) ?le of all the people in the employee list with a salary LESS than sal.
The second (else if) statement should check if the string ml is equal to “more”. If this is true, generate a CSV of all the people in the employee list with a salary MORE than sal.
Populate the ?le using the ?le pointer fp and by using fprintf. Consider changing the fprintf line from that function to seperate elements by commas instead of linefeeds. Also note that there is a linefeed after each employee.
6: Complete the function gen_csv pos by adding the missing code. This function generates a CSV based on occupation. The code should compare pos to the company position (occupation) of each employee in the list. If the occupation is the occupation we are searching for, write it to the CSV with the ?le pointer fp.
Note: Any function that opens a ?le, should also close that ?le (using fclose(fp)) before the function returns.
Final Steps: Now that the system is implemented, test it by generating a CSV of all the employees (this can be done by using all employees with salary greater than 0), a second CSV for all engineers, and a third CSV for salaries over $100k. Open the CSVs in Microsoft Excel or LibreO?ce Calc (in Linux). Calculate the average wages for all employees and all engineers (compare the these two numbers, are engineers of this company earning more than average?!). Also calculate the standard deviation of wages (for all employees). List and comment on your ?ndings and results In a Word or LibreO?ce Writer ?le. This ?le should be printed to a PDF named “Report.pdf” and submitted in the tarball with the code.
GIVEN FILES:
directory
9
Kelly
Schmitt
Engineer
100000.000000
Kristina
Thompson
Secretary
67500.000000
Bob
Ross
Painter
80000.000000
Shana
Bryd
Engineer
90000.000000
Kelly
Juan
Teacher
70000.000000
Sam
Smith
Singer
4000000.000000
Paul
Leach
CEO
7000000.000000
Geneva
Olivares
Engineer
200000.000000
Jamila
Thong
Engineer
150000.000000
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
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;
}
Explanation / Answer
If you have any doubts, please give me comment...
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.