All pastes #587893 Raw Edit

Miscellany

public text v1 · immutable
#587893 ·published 2007-06-25 05:49 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);
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\n", emp->name);
	printf("Hourly rate: ");
	scanf("%lf\n", &emp->rate);
	printf("Hours worked: ");
	scanf("%lf\n", &emp->hours);
}//end of userInput

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

	if(emp.hours <= 40)  //if less than 40 hours worked, pay is the normal base pay
	{
		totalPay = emp.rate * emp.hours;
		basePay = totalPay;
	}
	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); 
		totalPay = basePay + overtimePay;
	}

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

	return totalPay;
}//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: %.21s\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

/*Function to load one employee from file*/
void LoadEmployee(FILE *input, employee *emp)
{
	if (input != NULL)
		fread(emp, sizeof(struct employee), 1, input);
}

/*Function to save one employee to file*/
void SaveEmployee(FILE *output, employee emp)
{
	if(output !=NULL)
		fwrite(&emp, sizeof(emp), 1, output);
}

/*Function to print list of employees
int empList(FILE *empFile, employee *emp)
{
        int empNum = 0;
		//char name[21] = { };
        printf("Employee List:\n");
        for(i=0;i<NUMBER; i++)
        {   
			sscanf(emp->name, "%s", name);
            printf("%d) %s\n", i, name);
			emp++;
        }
        printf("\n");
        printf("Enter Number: ");
        scanf("%d\n", &empNum);
        return empNum;
	
	char name[21] = { };
	printf("Employee List: \n");
	while(!feof(empFile))
	{
		fread(emp, sizeof(struct employee), 1, empFile);
		sscanf(emp->name, "%s", name);
		for(int i=0; i<NUMBER;i++)
			printf("%d) %s\n", i, name);
	}
	printf("\n");
    printf("Enter Number: ");
    scanf("%d\n", &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;
}

/*Function to print menu choices*/
void EntryPoint()
{
    struct employee employees[NUMBER] = { };
	int num=0, i=0;
	FILE *empFile = fopen("c://employees.txt", "r+");

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

	int choice = 0, empNum = 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\n", choice);
		switch(choice)
		{
			case 1:
				printf("Employee List: \n");
				for (i=0;i<NUMBER;i++)
					printf("%d) %s \n", i, employees[i].name);
				printf("Enter number: ");
				scanf("%d\n", empNum);
				userInput(&employees[empNum]);
				break;
			case 2:
				printf("Employee List: \n");
				for (i=0;i<NUMBER;i++)
					printf("%d) %s \n", i, employees[i].name);
				
				printf("Enter number: ");
				scanf("%d\n", empNum);
				printOut(employees[empNum]);
				break;
			case 3:
				for(int i=0; i<NUMBER; i++)				
					printOut(employees[i]);
				break;
			case 4:
				for(int i=0; i<NUMBER; i++)
					SaveEmployee(empFile, employees[i]);
				break;
			default:
				break;
		}
	}while(choice != 5 && choice != 0);
	fclose(empFile);
	printf("End of run.");
}