All pastes #586911 Raw Edit

Someone

public text v1 · immutable
#586911 ·published 2007-06-24 03:20 UTC
rendered paste body
#include <stdio.h>
#include <string.h>

/*number of employees*/
#define NUMBER 5

/*structure for each employee*/
struct Employee
{
	char name[21];
	double rate;
	double hours;
};

typedef struct Employee employee;

/*function prototypes*/
void userInput(employee *emp);
double calcPay(employee emp);
double taxes(double pay);
void printOut(employee emp);
void LoadEmployee(FILE *input, employee *emp);
void SaveEmployee(FILE *output, employee emp);
int empList(employee *emp, int total);
char inputMode();
void EntryPoint();


/*function main begins program execution*/
int main (void)
{
	EntryPoint();
	return 0;
}//end of main function

/*function to prompt user for name, rate, and hours worked*/
void userInput(employee *emp)
{
	printf("Enter name: ");
	scanf("%s", emp->name);
	printf("Hourly rate: ");
	scanf("%lf", &emp->rate);
	printf("Hours worked: ");
	scanf("%lf", &emp->hours);
}//end of userInput

/*function to calculate the gross, base, and overtime pay*/
double calcPay(employee emp)
{
	double total = 0.0, basePay = 0.0, overtimePay = 0.0;

	if(emp.hours <= 40)  //if less than 40 hours worked, pay is the normal base pay
	{
		total = emp.rate * emp.hours;
		basePay = total;
	}
	else if (emp.hours > 40)  //if over 40 hours worked, pay includes overtime
	{
		basePay = 40*emp.rate;   
		overtimePay = 1.5*emp.rate*(emp.hours - 40); 
		total = basePay + overtimePay;
	}

	printf("Gross pay:		$%7.2f\n", total);  
	printf("Base pay:		$%7.2f\n", basePay); 
	printf("Overtime pay:		$%7.2f\n", overtimePay); 

	return total;
}//end of calcPay

/*function to calculate taxes owed*/
double taxes(double pay)
{
	return 0.20*pay;
}//end of taxes

/*function to print out paycheck details*/
void printOut(employee emp)
{	
	double amount = 0.0;

	printf("Name: %s\n", emp.name); 
	printf("Hours:		 %.2f\n", emp.hours); 
	printf("Rate:		$%7.2f\n", emp.rate);  

	amount = calcPay(emp);

	printf("Taxes paid:		$%7.2f\n", taxes(amount));    
	printf("Net pay:		$%7.2f\n", amount-taxes(amount));
	printf("\n");
}//end of printOut

void LoadEmployee(FILE *input, employee *emp)
{
	if (input != NULL)
		printf("bytes=%d\n", fread(emp, sizeof(employee), 1, input));
}

void SaveEmployee(FILE *output, employee emp)
{
	if(output !=NULL)
		fwrite(&emp, sizeof(emp), 1, output);
}

int empList(employee *emp, int total)
{
	int empNum = 0;
	int i;
	printf("Employee List:\n");
	for(i=0; i<total; i++)
	{	
		printf("%d) %s ", i, emp[i].name);
	}
	printf("\n");
	printf("Enter Number: ");
	scanf("%d", &empNum); 
	return empNum;
}

/*Function to prompt user for input mode*/
char InputMode()
{
	char input;
	printf("Please choose a data source: \n");
	printf("F) File\nK) Keyboard\nQ) Quit\n");
	scanf("%c", &input);
    return input;
}

void EntryPoint()
{
    employee employees[NUMBER];
	int num = 0, i, total; 
	FILE *empFile = fopen("employees.txt", "r");

    switch (InputMode())
    {
    case 'f':
    case 'F':
		for(i=0; i<NUMBER; i++)
		{
			LoadEmployee(empFile, &employees[i]);
			if(feof(empFile))
			{
				total = i;
				break;
			}
		}
        break;
    case 'k':
    case 'K':
		printf("Number of Employees (less than %d): ", NUMBER);
		scanf("%d\n", &num);
		if(num > NUMBER)
			num = NUMBER;
		for(i=0; i<num; i++)
		{
	        userInput(&employees[i]);
			if(employees[i].rate == -1 || employees[i].hours == -1)
				break;
		}
        break;
    default:
        break;
    }

    int choice = 0;
	do	
	{
		printf("Choose from the following: \n");
		printf("1) Update Data\n");
		printf("2) Print One Employee Data\n");
		printf("3) Print All Employee Data\n");
		printf("4) Save to File\n");
		printf("5) Quit\n");
		printf("Enter number: ");
		scanf("%d", &choice);
	
		int empNum = 0;
		switch(choice)
		{
			case 1:
				empNum = empList(employees, total);
				userInput(&employees[empNum]);
				break;
			case 2:
				empNum = empList(employees, total);
				printOut(employees[empNum]);
				break;
			case 3:
				for(i=0; i<total; i++)
			    {
					printOut(employees[i]);
			    }
				break;
			case 4:
				for(i=0; i<NUMBER; i++)
					SaveEmployee(empFile, employees[i]);
				break;
			default:
				break;
		}
	}while(choice != 5 && choice != 0);

    // update employee record
    // show employee record
	for(i=0; i<NUMBER; i++)
		SaveEmployee(empFile, employees[i]);
	fclose(empFile);
	printf("End of run. Employee data saved to c://employee.txt");
}