I have to create a program that \"must-reads\" from already existing data file w
ID: 3550262 • Letter: I
Question
I have to create a program that "must-reads" from already existing data file which contains the unknown number of records.
`The record should consist of the following fields for each employee:
`First Name
`Last Name
`Social Security Number
`Gender (Male or Female)
`Hours Worked
`Rate
The salary should be calculated on the weekly basis of 40 hours. Any time over 40 hours worked, is considered to be overtime. When working overtime, employees get paid 1.5 of their normal salary. (For example: If an employee worked an hour overtime, and he was normally paid $20.00 per hour, his overtime would be calculated as $20.00 x 1.5 = $30.00)
`The menu should contain the following choices:
1. List all employees to the screen with their salary.
2. Add an employee to the payroll.
3. Delete an employee from the payroll.
4. List all the female employees.
5. List all employees with overtime.
6. Alphabetize the list of employees.
7. Who gets the highest salary.
8. Quit and Save.
`Please note: If an employee is deleted from the database, he/she should no longer exist in the file or database, when the `program is rerun.
`Please comment your program adequately and name it PayRoll.c(Points will be deducted if there are no comments, or the `program is incorrectly named).
Please NOTE: It is C Programming and please run the program and check the error. And I am putting some codes that is for ssimilar type of program.
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
struct xrec
{
char name[20];
char g;
double gpa;
char ssn[12];
};
typedef struct xrec Student;
void printStudent(Student x);
Student getData(void);
void getDData(Student * x);
int main()
{
Student st1,st2;
Student * ptr;
Student ** pptr;
Student a[2];
char * q;
st1=getData();
/*getDData(&st1);*/
ptr=&st1;
/*
ptr->gpa=2.5;*/
(*ptr).gpa=2.5;
pptr=&ptr;
printf("=====>>>%f ",(**pptr).gpa);
printStudent(st1);
/*
q="Lisa";
st1.g='F';
strcpy(st1.name,q);
st1.gpa=4.5;
a[0]=st1;st2=st1;strcpy(st2.name,"Jack");st2.gpa=2.4;
a[1]=st2;
printStudent(a[0]);
printStudent(a[1]);
*/
return 0;
}
void getDData(Student * x)
{
printf("Name:");scanf("%s",x->name);fflush(stdin);
printf("Gender:");scanf("%c",&(*x).g);
printf("Gpa:"); scanf("%lf",&(*x).gpa);
printf("SSN:"); scanf("%s",(*x).ssn);
}
Student getData(void)
{
Student x;
printf("Name:");
scanf("%s",x.name);fflush(stdin);
printf("Gender:");scanf("%c",&x.g);
printf("Gpa:"); scanf("%lf",&x.gpa);
printf("SSN:"); scanf("%s",x.ssn);
return x;
}
void printStudent(Student x)
{
printf("Name:%s Gender:%c Gpa:%4.2f "
,x.name,x.g,x.gpa);
x.gpa=2.5;
}
!!!!!!!!!! I DONT WANT COPY AND PASTE PLEASE RUN THE PROGRAM TO CHECK ERRORS.... Thank You!!!!!!!!!!!!!
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int flag1 = 0;//flag1 is for checking tha list has been empty of not
struct node
{
char first[15],last[15],gender[10];
long long int ssn; //ssn is social security number
int hours,rate,salary;
struct node *next;
};
//This code is for sorting
struct node *getTail(struct node *cur)
{
while (cur != NULL && cur->next != NULL)
cur = cur->next;
return cur;
}
// Partitions the list taking the last element as the pivot
struct node *partition(struct node *head, struct node *end,
struct node **newHead, struct node **newEnd)
{
struct node *pivot = end;
struct node *prev = NULL, *cur = head, *tail = pivot;
// During partition, both the head and end of the list might change
// which is updated in the newHead and newEnd variables
while (cur != pivot)
{
if (strcmp(cur->first,pivot->first) == -1)
{
// First node that has a value less than the pivot - becomes
// the new head
if ((*newHead) == NULL)
(*newHead) = cur;
prev = cur;
cur = cur->next;
}
else // If cur node is greater than pivot
{
// Move cur node to next of tail, and change tail
if (prev)
prev->next = cur->next;
struct node *tmp = cur->next;
cur->next = NULL;
tail->next = cur;
tail = cur;
cur = tmp;
}
}
// If the pivot data is the smallest element in the current list,
// pivot becomes the head
if ((*newHead) == NULL)
(*newHead) = pivot;
// Update newEnd to the current last node
(*newEnd) = tail;
// Return the pivot node
return pivot;
}
//here the sorting happens exclusive of the end node
struct node *quickSortRecur(struct node *head, struct node *end)
{
// base condition
if (!head || head == end)
return head;
struct node *newHead = NULL, *newEnd = NULL;
// Partition the list, newHead and newEnd will be updated
// by the partition function
struct node *pivot = partition(head, end, &newHead, &newEnd);
// If pivot is the smallest element - no need to recur for
// the left part.
if (newHead != pivot)
{
// Set the node before the pivot node as NULL
struct node *tmp = newHead;
while (tmp->next != pivot)
tmp = tmp->next;
tmp->next = NULL;
// Recur for the list before pivot
newHead = quickSortRecur(newHead, tmp);
// Change next of last node of the left half to pivot
tmp = getTail(newHead);
tmp->next = pivot;
}
// Recur for the list after the pivot element
pivot->next = quickSortRecur(pivot->next, newEnd);
return newHead;
}
// The main function for quick sort. This is a wrapper over recursive
// function quickSortRecur()
void quickSort(struct node **headRef)
{
(*headRef) = quickSortRecur(*headRef, getTail(*headRef));
return;
}
//Sorting code ended
void print(struct node **head_ref)
{
//List all employees to the screen with their salary.
struct node *head = *head_ref;
while(head)
{
printf("%s %s Salary is %d ",head->first,head->last,head->salary);
head = head->next;
}
}
void add(struct node** head_ref,char first[],char last[],char gender[],long long int ssn,int hours,int rate)
{
//Add an employee to the payroll.
int salary;
/* allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));
/* put in the data */
strcpy(new_node->first,first);
strcpy(new_node->last,last);
strcpy(new_node->gender,gender);
new_node->ssn = ssn;
new_node->hours = hours;
new_node->rate = rate;
if(hours > 40)
{
salary = 40 * (rate);
salary = salary + 1.5 * (hours - 40) * (rate);
}
else
salary = (hours) * (rate);
new_node->salary = salary;
new_node->next = *head_ref;
*head_ref = new_node;
}
void del(struct node **head_ref,long long int ssn)
{
//Delete an employee from the payroll.
struct node *head = *head_ref,*temp;
int a = 0 ;
if(head->ssn == ssn)
{
if(head->next)
{
temp = head->next;
strcpy(head->first,temp->first);
strcpy(head->last,temp->last);
strcpy(head->gender,temp->gender);
head->ssn = temp->ssn;
head->hours = temp->hours;
head->rate = temp->rate;
head->salary = temp->salary;
head->next = temp->next;
free(temp);
}
else
{
flag1 = 1;
}
a = 1;
}
else
{
while(head->next && head->next->ssn != ssn)
{
head = head->next;
}
if(head->next)
{
temp = head->next;
head->next = head->next->next;
a = 1;
free(temp);
}
}
if(a == 0)
printf(" Employee not found in database ");
else
printf(" Employee deleted ");
head = *head_ref;
}
void female(struct node **head_ref)
{
//List all the female employees.
struct node *head = *head_ref;
while(head)
{
if(strcmp(head->gender,"Female") == 0)
printf("%s %s ",head->first,head->last);
head = head->next;
}
}
void overtime(struct node **head_ref)
{
//List all employees with overtime.
struct node *head = *head_ref;
while(head)
{
if(head->hours > 40)
printf("%s %s ",head->first,head->last);
head = head->next;
}
}
void sorted(struct node **head_ref)
{
//Alphabetize the list of employees.
quickSort(head_ref);
}
void high(struct node **head_ref)
{
//Who gets the highest salary.
struct node *head = *head_ref;
int maximum = 0;
char first[15],last[15];
while(head)
{
if(head->salary > maximum)
{
maximum = head->salary;
strcpy(first,head->first);
strcpy(last,head->last);
//printf("%s %s %d ",head->first,head->last,head->salary);
}
head = head->next;
}
printf("%s %s ",first,last);
}
void save(struct node **head_ref,char *filename)
{
//Quit and Save.
FILE *fp;
struct node *head = *head_ref;
fp=fopen(filename,"w");
if( fp == NULL )
{
printf("Error while writing to the file. ");
exit(EXIT_FAILURE);
}
while(head)
{
fprintf(fp,"%s %s %lld %s %d %d ",head->first,head->last,head->ssn,head->gender,head->hours,head->rate);
head = head->next;
}
fclose (fp);
}
void menu()
{
printf("1. List all employees to the screen with their salary. ");
printf("2. Add an employee to the payroll. ");
printf("3. Delete an employee from the payroll. ");
printf("4. List all the female employees. ");
printf("5. List all employees with overtime. ");
printf("6. Alphabetize the list of employees. ");
printf("7. Who gets the highest salary. ");
printf("8. Quit and Save. ");
}
void store(struct node** head_ref,char *filename)
{
FILE* fp1;
fp1=fopen (filename,"r");
if( fp1 == NULL )
{
printf("Error while opening the file. ");
exit(EXIT_FAILURE);
}
char first[15],last[15],gen[10];
int hours,rate;
long long int ssn;
while(fscanf(fp1,"%s %s %lld %s %d %d",first,last,&ssn,gen,&hours,&rate)!=EOF)
{
//printf("%s %s %d %s %d %d ",first,last,ssn,gen,hours,rate);
add(head_ref,first,last,gen,ssn,hours,rate);
}
fclose (fp1);
}
int main()
{
int choice,hours,rate;
long long int ssn;
char first[15],last[15],gender[10];
struct node *head = NULL;
char filename[15];
printf("Enter name of input file ");
scanf("%s",filename);
store(&head,filename);
while(1)
{
printf(" Enter your choice ");
menu();
printf(" ");
scanf("%d",&choice);
if(choice == 1)
{
printf(" All employees with salary are ");
print(&head);
}
else if(choice == 2)
{
printf("Enter first name of new employee ");
scanf("%s",first);
printf("Enter last name of new empoyee ");
scanf("%s",last);
printf("Enter gender of new empoyee ");
scanf("%s",gender);
printf("Enter Social security number of new employee ");
scanf("%lld",&ssn);
printf("Enter Hours worked of new employee ");
scanf("%d",&hours);
printf("Enter rate of new employee ");
scanf("%d",&rate);
add(&head,first,last,gender,ssn,hours,rate);
printf(" %s %s added as employee ",first,last);
}
else if(choice == 3)
{
printf(" Enter social security number of employ to be deleted ");
scanf("%lld",&ssn);
del(&head,ssn);
if(flag1)
{
head = NULL;
printf("List is empty now ");
flag1 = 0;
}
}
else if(choice == 4)
{
printf(" All female employees are ");
female(&head);
}
else if(choice == 5)
{
printf(" All employees with overtime are ");
overtime(&head);
}
else if(choice == 6)
{
sorted(&head);
}
else if(choice == 7)
{
printf(" Employee with highest salary is ");
high(&head);
}
else if(choice ==8)
{
save(&head,filename);
break;
}
}
system("pause");
return 0;
}