Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "payfile.txt"
#define MAX 55
#define BUF_SIZE 100
typedef struct Address
{
char street[17];
char city[12];
char state[3];
char zip[6];
}Address;
typedef struct Employee
{
char first[8];
char init[2];
char last[10];
Address ad;
int age;
char sex[1];
int years;
double pay;
}Employee;
void strSub(char s1[], char s2[], int start, int length);
int getEmploy(Employee s[], int max);
void printEmploy(Employee s[], int num);
void printMen(Employee s[], int num);
int topPaywoman(Employee s[], int num);
int lowPayman(Employee s[], int num);
double avgPay(Employee s[], int num);
void printBelowavg(Employee s[], int num);
void printRatiomen(Employee s[], int num);
void printSenior(Employee s[], int num);
void printRaise(Employee s[], int num);
void sortzip(Employee arr[], int num);
int main()
{
int numEmploy, topIndex, lowIndex;
Employee employ[MAX];
numEmploy = getEmploy( employ, MAX );
if( numEmploy == 0 ) return 0;
printEmploy( employ, numEmploy );
printMen( employ, numEmploy );
topIndex = topPaywoman( employ, numEmploy );
printf("\nThe woman with the most pay is %s %s earning $%.2f per week\n",
employ[topIndex].first, employ[topIndex].last, employ[topIndex].pay);
lowIndex = lowPayman( employ, numEmploy );
printf("\nThe man with the least pay is %s %s earning $%.2f per week\n",
employ[lowIndex].first, employ[lowIndex].last, employ[lowIndex].pay);
printf("\nThe average pay is $%.2f per week\n", avgPay(employ, numEmploy));
printBelowavg( employ, numEmploy );
printRatiomen( employ, numEmploy );
printSenior( employ, numEmploy );
printRaise( employ, numEmploy );
/*sort by zip code
sortzip( employ, numEmploy );
printf("\nThe employee data sorted in ascending order by zip code ...");
printEmploy( employ, numEmploy );*/
return 0;
}
void strSub(char s1[], char s2[], int start, int length) {
int i;
for( i = 0; i < length; ++i)
s2[i] = s1[start+i];
s2[i] = 0;
}
int getEmploy(Employee s[], int maxNum) {
int i = 0;
char buf[BUF_SIZE];
char tmpa[32];
char tmp[32];
FILE* fp = fopen( FILE_NAME, "r" );
while( i<maxNum && fgets( buf, BUF_SIZE, fp ) )
{
strSub( buf, s[i].first, 0, 7 );
strSub( buf, s[i].init, 8, 1 );
strSub( buf, s[i].last, 10, 9 );
strSub( buf, s[i].ad.street, 20, 16 );
strSub( buf, s[i].ad.city, 37, 11 );
strSub( buf, s[i].ad.state, 49, 2 );
strSub( buf, s[i].ad.zip, 52, 5 );
strSub( buf, tmp, 58, 2 );
s[i].age = atoi(tmp);
strSub( buf, s[i].sex, 60, 2 );
strSub( buf, tmpa, 62, 2 );
s[i].years = atoi(tmpa);
strSub( buf, tmp, 65, 5 );
s[i].pay = atof(tmp);
++i;
}
fclose( fp );
return i;
}
void printEmploy(Employee s[], int num) {
int i;
printf( "The employees are\n");
for( i = 0; i<num; ++i )
{
printf( "%s %s %s %s %s %s %s %2d %s %1d %.2f\n", s[i].first, s[i].init, s[i].last, s[i].ad.street,
s[i].ad.city, s[i].ad.state, s[i].ad.zip, s[i].age, s[i].sex, s[i].years, s[i].pay );
}
}
void printMen(Employee s[], int num) {
int i;
printf("\nThe men employeed are\n");
for( i = 0; i<num; ++i )
{
if(s[i].sex != 'F') /*need help sorting by men*/
{
printf("%s %s %s %s %s %s %s %2d %s %1d %.2f\n", s[i].first, s[i].init, s[i].last, s[i].ad.street,
s[i].ad.city, s[i].ad.state, s[i].ad.zip, s[i].age, s[i].sex, s[i].years, s[i].pay);}
}}
int topPaywoman(Employee s[], int num)
{
int i, j = 0;
double top = s[0].pay;
for(i = 1; i < num; ++i)
{
if(s[i].pay > top /*&& code to sort by sex still needed*/) { top = s[i].pay; j = i; }
}
return j;
}
int lowPayman(Employee s[], int num){
int i, j = 0;
double low = s[0].pay;
for(i = 1; i < num; ++i)
{
if(s[i].pay < low /*&& code to sort by sex still needed*/) { low = s[i].pay; j = i; }
}
return j;
}
double avgPay(Employee s[], int num)
{
int i;
double sum = 0;
for(i = 0; i < num; ++i)
sum += s[i].pay;
return sum/num;
}
void printBelowavg(Employee s[], int num) {
int i;
double avg = avgPay(s, num);
printf("\nThe women with pay below the average are\n");
for( i = 0; i < num; ++i)
{
if( s[i].pay < avg /*&& code to sort by sex is needed*/)
printf("%s %s %s %.2f\n", s[i].first, s[i].init, s[i].last,
s[i].pay);
}
}
void printRatiomen(Employee s[], int num) {
int i, above = 0, below = 0;
double avg = avgPay(s, num);
for( i = 0; i < num; ++i)
{
above += s[i].pay > avg;
below += s[i].pay < avg;
}/*code to sort by sex is needed*/
printf("\nThe ratio of men above average pay to men below average pay is\n%d : %d\n", above, below);
}
void printSenior(Employee s[], int num) {
int i;
double avg = avgPay(s, num);
printf("\nThe men who make more than $35000 per year and have worked for over 5 years and are over 30 years old are\n");
for( i = 0; i < num; ++i)
{
if( s[i].pay > (35000/52) && s[i].years > 5 && s[i].age > 30/*&& code to sort for sex needed*/)
{printf("%s %s %.2f\n", s[i].first, s[i].last, s[i].pay);}
}
}
void printRaise(Employee s[], int num) {
int i, y = 350;
printf("\nThe employees who get raises are\n");
for(i = 0; i < num; ++i)
{
if( s[i].pay < y){
printf("%s %s\n", s[i].first, s[i].last);
printf("With a raise to %.2f\n", s[i].pay * 1.1);}
}
}
/*void sortzip(Employee arr[], int num)
{
int i, j;
Employee temp;
for(i = 1; i < num; ++i)
{ strcpy(temp.ad.zip, arr[i].ad.zip);
j = i - 1;
while(j >= 0 && temp.ad.zip < arr[j].ad.zip)
{
strcpy(arr[j+1].ad.zip, arr[j].ad.zip);
j--;
}
strcpy(arr[j+1].ad.zip, temp.ad.zip);
}
}
*/