Code:
/************************
* CIS 15AG AM - Lab VII
*
* A program to help a business keep
* statistics of its employee salaries
* for one year.
*
* Author: Michael Baird
*
* Begin date: Nov 28, 2005
* Last modified: Dec 6, 2005
**/
#include <stdio.h>
#include <ctype.h>
char PromptUser();
int CountEmployees();
void HighestAnnualSalary();
float CalculateHighestSalary( int * emp );
void AverageMonthly();
float CalculateAverageSalary();
void FormatReport();
int InputToOutput();
int main()
{
char choice;
do
{
choice = PromptUser();
switch( tolower( choice ) )
{
case 'c':
{
printf( "There are entries for %d employees.\n\n", CountEmployees() );
} break;
case 'h':
{
HighestAnnualSalary();
} break;
case 'a':
{
AverageMonthly();
} break;
case 'f':
{
FormatReport();
} break;
}
} while( tolower( choice ) != 'q' );
system( "PAUSE" );
return 0;
}
char PromptUser()
{
char res;
printf( "Choose an option from the following menu:\n" );
printf( "\tc: calculate total employees\n" );
printf( "\th: report highest annual salary\n" );
printf( "\ta: calculate average monthly salary\n" );
printf( "\tf: format the report to output file\n" );
printf( "\tq: quit this application\n\n" );
printf( "Enter your choice: " );
scanf( " %c", &res );
return res;
}
int CountEmployees()
{
FILE * input;
int count = 0;
char holder;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 1;
}
while( (fscanf( input, "%c", &holder ) != EOF) )
{
if( holder == '\n' )
count++;
}
fclose( input );
return count;
}
void HighestAnnualSalary()
{
int employee = 0;
float highest = CalculateHighestSalary( &employee );
printf( "Employee %d has the highest annual salary of %.2f.\n\n", employee, highest );
return;
}
float CalculateHighestSalary( int * emp )
{
FILE * input;
int i, j; //for use with loop methods
int employeeID, monthsWorked;
float highestSalary = 0, iSalary /*individual monthly salary*/ = 0, iTotalSalary = 0 /*individual total salary*/;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 1;
}
for( j = 0; j < CountEmployees(); j++ )
{
while( fscanf( input, "%d,", &employeeID ) != EOF )
{
fscanf( input, "%d,", &monthsWorked );
for( i = 0; i < monthsWorked; i++ )
{
fscanf( input, "%f", &iSalary );
iTotalSalary += iSalary;
}
if( iTotalSalary > highestSalary )
{
highestSalary = iTotalSalary;
*emp = employeeID;
}
//reset for each employee
iSalary = 0;
iTotalSalary = 0;
}
}
fclose( input );
return highestSalary;
}
void AverageMonthly()
{
printf( "The average monthly salary for all employees is: %.2f.\n\n", CalculateAverageSalary() );
return;
}
float CalculateAverageSalary()
{
FILE * input;
int i, j; //for use with loop methods
int employeeID; //holder for this entry
int totalMonths = 0, iMonthsWorked /*individual months worked*/;
float totalAverage = 0, iSalary /*individual monthly salary*/;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening the input file.\n" );
return 1;
}
for( j = 0; j < CountEmployees(); j++ )
{
while( fscanf( input, "%d,", &employeeID ) != EOF )
{
fscanf( input, "%d,", &iMonthsWorked );
totalMonths += iMonthsWorked;
for( i = 0; i < iMonthsWorked; i++ )
{
fscanf( input, "%f", &iSalary );
totalAverage += iSalary; //store in totalAverage simply to save mem space
}
}
}
return (totalAverage / totalMonths);
}
void FormatReport()
{
if( InputToOutput() )
{
printf( "The input file has been successfully written to the output file, \'out.txt\'.\n\n" );
}
else
printf( "Formatting failed.\n\n" );
return;
}
int InputToOutput()
{
FILE * input;
FILE * output;
int i, j; //for use with loops
int employeeID, monthsWorked;
float jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec;
if( (input = fopen( "lab7_input.txt", "r" )) == NULL )
{
printf( "Error opening input file.\n" );
return 0;
}
else if( (output = fopen( "out.txt", "w" )) == NULL )
{
printf( "Error creating output file.\n" );
return 0;
}
for( j = 0; j < CountEmployees(); j++ )
{
while( fscanf( input, "%d,", &employeeID ) != EOF )
{
fscanf( input, "%d,", &monthsWorked );
fprintf( output, "%d\t%d", employeeID, monthsWorked );
}
}
}
The two functions that are related to this question are the last two at the bottom there.
Code:
1234,12,1200 2050 3010 4000 5560 6005 6780 5340 4000.50 3040 2005 1100
2245,5,980 525 1120 405.50 650.50
3496,3, 1020.50 2085 1050.50
8849,10,500.50 305.50 810 925 1060 1256.50 800.50 810.50 1087 578
2390,6,875.50 1020 510.50 790 250.50 1000.50
As I said above, the first two