In: Computer Science
C programming
A small company provided you three files.
1) emp.txt : this file contains list of employees where each line represents data of an employee. An employee has an id (String max length 20), last name (string max length 100), and salary (int). See the example emp.txt file.
2) dept.txt: This text file contains list of departments of the employees. Each line of the file contains an employee id (string max length 20) and department name for that employee (string max length 100). See the example dept.txt file.
3) query.txt: This file contains a set of queries. There can be two types of queries. Query type 1 represents searching for an employee and Query type 2 presents searching for a department. The first line of the file contains the number of queries n and then next n lines contains the queries. The first number of each line represents the type of query and the second string in the line represents the query key. See the example query.txt file.
Example Query:
A query line "1 Smith" will search for Smith and will display details of Smith including his department name. If Smith does not exist, your program should display "Employee Smith Not Found"
A query line "2 Clinical" will search for Clinical department and will display total salary paid to that department. If the department does not exist, the total salary will be 0.
emp dept relationship: Employee and department is related by the employee id. An employ can have 0 or one department. For example, e5 Martin is not associated to any department. A department can have 0 or more employees.
Requirements:
1. Your program need to have at least two structures. One for employee and one for department. You need to use array of those structures.
2. int readEmp(Employee ArrayOfEmployees[]): You need to implement readEmp function with this function header. This function opens your emp.txt file and load the passed array to this function and returns number of employees in the file.
3. void readDept(Dept ArrayOfDepartments[], int *numOfDept): You need to implement readDept function with this function header. This function opens yourdept.txt file and load the passed array to this function and also update the referenced variable with the number of department in the file.
4. void printAll(Employee Arr[], int totalEmp, Dept ArrayOfDepartments[], int totalDept): This function prints the details of all the employees including their department name available in the passed arrays. See the output format from the sample output shown bellow.
5. void search_employee(Employee Arr[], int totalEmp, Dept ArrayOfDepartments[], int totalDept, char qStr[]): This function processes query type 1 and produces output like the sample output.
6. int totalSal_dept(Employee Arr[], int totalEmp, Dept ArrayOfDepartments[], int totalDept, char qStr[]) : This function processes the query type 2 and produces the output like the sample output.
7. In the main function, declare necessary variables and call the load functions first and then call the printAll function. Next, start reading the query file and process the queries by calling appropriate function(s).
Sample Input/Output: Use the input files. There is no console input involved in this program. The output of the code is based on the provided files
Printing the list of Employees:
=================
ID: e1 LName: Adam Salary: 500 Department: Finance
ID: e2 LName: Smith Salary: 700 Department: Sales
ID: e3 LName: Robin Salary: 450 Department: Marketing
ID: e4 LName: Jack Salary: 800 Department: Sales
ID: e5 LName: Martin Salary: 600 Department: None
ID: e6 LName: Nusair Salary: 700 Department: IT
Query Phase
ID: e2 LName: Smith Salary: 700 Department: Sales
Total Salary of Department Clinical is 0
Employee Nasir Not Found
Total Salary of Department Sales is 1500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXE 50 // For maximum number of employee records
#define MAXD 50 // For maximum number of department records
struct Employee
{
char id[20];
char lastName[100];
int salary;
};
struct Dept
{
char id[20];
char departmentName[100];
};
// Function to read data from file and stores it in array of
Employee object
// Returns number of records read from file
int readEmp(struct Employee ArrayOfEmployees[])
{
// To store number of records
int len = 0;
// Creates a file pointer to open the file emp.txt in read
mode
FILE *readF = fopen("emp.txt", "r");
// Checks if file is unable to open then display error
message
if(readF == NULL)
{
// Display error message
puts("Error: Could not open file: emp.txt");
exit(0);
}// End of if
// Extracts data from file and stores it
while(!feof(readF))
{
// Reads a number from file and stores it at len index
position
fscanf(readF, "%s %s %d", ArrayOfEmployees[len].id,
ArrayOfEmployees[len].lastName,
&ArrayOfEmployees[len].salary);
// Increase the index counter by one
len = len + 1;
}// End of while loop
// Close the file
fclose(readF);
// Returns number of records
return len;
}// End of function
// Function to read department information from file and stores
it in array of Dept object
// Returns number of records using pointer variable
*numOfDept
void readDept(struct Dept ArrayOfDepartments[], int
*numOfDept)
{
// Creates a file pointer to open the file dept.txt in read
mode
FILE *readF = fopen("dept.txt", "r");
// Checks if file is unable to open then display error
message
if(readF == NULL)
{
// Display error message
puts("Error: Could not open file: dept.txt");
exit(0);
}// End of if
// Extracts data from file and stores it
while(!feof(readF))
{
// Reads a number from file and stores it at numOfDept index
position
fscanf(readF, "%s %s", ArrayOfDepartments[*numOfDept].id,
ArrayOfDepartments[*numOfDept].departmentName);
// Increase the index counter by one
*numOfDept = *numOfDept + 1;
}// End of while loop
// Close the file
fclose(readF);
}// End of function
// Function to display all the employee and their corresponding
department name information
void printAll(struct Employee Arr[], int totalEmp, struct Dept
ArrayOfDepartments[], int totalDept)
{
// Loops variable
int c, d;
// Loops till number of employees
for(c = 0; c < totalEmp; c++)
{
// Loops till number of departments
for(d = 0; d < totalDept; d++)
{
// Checks if current employee id is equals to current department
employee id
if(strcmp(Arr[c].id, ArrayOfDepartments[d].id) == 0)
{
// Displays the employee and department information
printf("\n ID: %s LName: %s Salary: %d Department: %s",
Arr[c].id, Arr[c].lastName, Arr[c].salary,
ArrayOfDepartments[d].departmentName);
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
}// End of inner outer loop
}// End of function
// Function to search for an employee name and displays the
information if found
// otherwise displays error message
void search_employee(struct Employee Arr[], int totalEmp, struct
Dept ArrayOfDepartments[],
int totalDept, char qStr[])
{
// Loops variable
int c, d;
// To store found status. Initially -1 for not found
int foundStatus = -1;
// Loops till number of employees
for(c = 0; c < totalEmp; c++)
{
// Checks if current employee last name is equals to parameter qSrt
name
if(strcmp(Arr[c].lastName, qStr) == 0)
{
// Sets the found status to loop variable used for employee
(c)
foundStatus = c;
// Loops till number of departments
for(d = 0; d < totalDept; d++)
{
// Checks if current employee id is equals to current department
employee id
if(strcmp(Arr[c].id, ArrayOfDepartments[d].id) == 0)
{
// Displays the employee and department information
printf("\n ID: %s LName: %s Salary: %d Department: %s",
Arr[c].id, Arr[c].lastName, Arr[c].salary,
ArrayOfDepartments[d].departmentName);
// Come out of the loop
break;
}// End of if condition
}// End of for loop
}// End of outer if condition
}// End of outer for loop
// Checks if found status is -1 then not found
if(foundStatus == -1)
printf("\n Employee %s Not Found", qStr);
}// End of function
// Function to search a department name if found calculate the
total salary of all the employees
// in that department and returns it
// Otherwise returns 0
int totalSal_dept(struct Employee Arr[], int totalEmp, struct Dept
ArrayOfDepartments[],
int totalDept, char qStr[])
{
// Loops variable
int c, d;
// To store total
int total = 0;
// Loops till number of departments
for(c = 0; c < totalDept; c++)
{
// Checks if current department's department name is equals to
parameter qSrt department name
if(strcmp(ArrayOfDepartments[c].departmentName, qStr) == 0)
{
// Loops till number of employees
for(d = 0; d < totalEmp; d++)
{
// Checks if current employee id is equals to current department
employee id
if(strcmp(Arr[d].id, ArrayOfDepartments[c].id) == 0)
{
// Calculates total salary
total += Arr[d].salary;
}// End of if condition
}// End of for loop
}// End of outer if condition
}// End of outer for loop
// Returns the calculate total
return total;
}// End of function
// main function definition
int main()
{
// Creates an array of object of Employee of size MAXE
struct Employee arrayOfEmployees[MAXE];
// Creates an array of object of Dept of size MAXE
struct Dept arrayOfDepartments[MAXD];
// To store number of department records
int numOfDept = 0;
// To store number of employee records
int numOfEmp = 0;
// To store number of query records
int numOfQuery = 0;
// To store query type
int queryType;
// To store query
char query[20];
// Loops variable
int c;
// Calls the function to read employee records
numOfEmp = readEmp(arrayOfEmployees);
// Calls the function to read department records
readDept(arrayOfDepartments, &numOfDept);
printf("\n Printing the list of Employees: ");
printf("\n =================");
// Calls the function to display all employee and department
information
printAll(arrayOfEmployees, numOfEmp, arrayOfDepartments,
numOfDept);
printf("\n\n Query Phase");
// Creates a file pointer to open the file query.txt in read
mode
FILE *readF = fopen("query.txt", "r");
// Checks if file is unable to open then display error
message
if(readF == NULL)
{
// Display error message
puts("Error: Could not open file: query.txt");
exit(0);
}// End of if
// Reads number of queries
fscanf(readF, "%d", &numOfQuery);
// Loops till number of queries
for(c = 0; c < numOfQuery; c++)
{
// Reads query type and query
fscanf(readF, "%d %s", &queryType, query);
// Checks if query type is 1
if(queryType == 1)
// Calls the function to display employee and its department
information
search_employee(arrayOfEmployees, numOfEmp, arrayOfDepartments,
numOfDept, query);
// Otherwise checks if query type is 2
else if(queryType == 2)
// Calls the function to calculate total salary for the
department
printf("\n Total Salary of Department %s is %d", query,
totalSal_dept(arrayOfEmployees, numOfEmp, arrayOfDepartments,
numOfDept, query));
// Otherwise displays error message
else
printf("\n\t Invalid Query Type!!");
}// End of while loop
// Close the file
fclose(readF);
return 0;
}// End of main function
Sample Output:
Printing the list of Employees:
=================
ID: e1 LName: Adam Salary: 500 Department: Finance
ID: e2 LName: Smith Salary: 700 Department: Sales
ID: e3 LName: Robin Salary: 450 Department: Marketing
ID: e4 LName: Jack Salary: 800 Department: Sales
ID: e5 LName: Martin Salary: 600 Department: None
ID: e6 LName: Nusair Salary: 700 Department: IT
Query Phase
ID: e2 LName: Smith Salary: 700 Department: Sales
Total Salary of Department Clinical is 0
Employee Nasir Not Found
Total Salary of Department Sales is 1500
emp.txt file contents
e1 Adam 500
e2 Smith 700
e3 Robin 450
e4 Jack 800
e5 Martin 600
e6 Nusair 700
dept.txt file contents
e1 Finance
e2 Sales
e3 Marketing
e4 Sales
e5 None
e6 IT
query.txt file contents
4
1 Smith
2 Clinical
1 Nasir
2 Sales