如何显示正确的总计和平均值? [英] How do I display correct totals and averages?

查看:99
本文介绍了如何显示正确的总计和平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,晚上好!



我的代码问题是我的总数和平均值没有显示正确的值是!到目前为止,我的程序中的其他所有内容,检查标题,QSort,员工数据格式以及显示为其他所有内容输入的正确信息。



您可以看到该程序在这里行动:

第1部分

第2部分

第3部分

第4部分



我改变了信息存储的顺序,因为付费率和小时数意外地相反。在打印检查之前还打印了我的打印总数和平均打印量。



现在没有进一步的麻烦,我的源代码:

 #include< stdio.h> 
#include< stdlib.h>
#include< string.h>
#include< ctype.h>

typedef short BOOLEAN;
typedef char STR15 [15 + 1];
typedef char STR10 [10 + 1];
typedef struct PayRecord
{
STR15 LastName;
STR10 FirstName;
浮动小时,payrate,deferred,gross,fedtax,statetax,ssitax,net;
float reghrs,ovthrs;
float totreg,totovt,totrate,totgross,totfed,totstate,totssi,totdefr,totnet;
} PayRecord;

#define FEDTAXRATE 0.15
#define STATETAXRATE 0.07
#define SSITAXRATE 0.0775
#define MAXEMPLOYEES 5
#define TRUE 1
#define罚款0

#define HEADLINE1员工薪酬报酬总额美联储SSI净额
#define HEADLINE2名称费率Ovt Hrs Pay State Defr Pay \ n
#define HEADLINE3========= ====== ======= ======= ======= ====== ==== === \ n
#define EMPLOYEEREPORT%-16s%5.2f%5.2f%7.2f%6.2f%6.2f%7.2f\\\

#define EMPLOYEEREPORT2%5.2 f%6.2f%6.2f\\\
\ n
#define BREAK============================ ================================== \ n
#define TOTALS1总计%5.2f %6.2f%6.2f%6.2f%6.2f%7.2f \ n
#define TOTALS2%6.2f%5.2f%6.2f\\\
\ n
#define AVERAGES1 平均值%5.2f% 6.2f%5.2f%6.2f%6.2f%7.2f\\\

#define AVERAGES2%6.2f%5.2f%6.2f\\\
\ n

#define BLANK* * \ n
#define BORDER******************************** **************************** \ n
#define HEADER*选中否%3d * \ n
#define HEADER2* Sabre Corporation * \ n
#define HEADER3* 15790 West Henness lane * \ n
#define HEADER4* New Corio,New Mexico 65790 * \ n
#define HEADER5*支付给* \ n的订单
#define HEADER6*%-16s * \ n
#define HEADER7* Sum Nine Hundred Nine-Nine和XX / 100 Dollars * \ n
#define HEADER8*参考:%-16s * \ n

#define STUB*支票号码%3d * \ n
#define STUB2*%-16s Reg Pay Fed Tax * \ n
#define STUB3*地址Ovt支付州税* \ n
#define STUB4*城市,州总薪水SSI税* \ n
#define STUB5* Zip Net Pay Tax Tax * \ n

void PrintHeadings(FILE * reportFile)
{
fprintf(reportFile,HEADLINE1);
fprintf(reportFile,HEADLINE2);
fprintf(reportFile,HEADLINE3);
}
void InitAccumulators(PayRecord EmployeeData [],int maxemployees)
{
EmployeeData [MAXEMPLOYEES - 1] .totreg = 0;
EmployeeData [MAXEMPLOYEES - 1] .totovt = 0;
EmployeeData [MAXEMPLOYEES - 1] .totrate = 0;
EmployeeData [MAXEMPLOYEES - 1] .totgross = 0;
EmployeeData [MAXEMPLOYEES - 1] .totfed = 0;
EmployeeData [MAXEMPLOYEES - 1] .totstate = 0;
EmployeeData [MAXEMPLOYEES - 1] .totssi = 0;
EmployeeData [MAXEMPLOYEES - 1] .totdefr = 0;
EmployeeData [MAXEMPLOYEES - 1] .totnet = 0;
}
void InputEmployeeData(PayRecord EmployeeData [],int count)// 3.3
{
printf(请输入你的名字和姓氏:);
scanf(%s%s,EmployeeData [count] .FirstName,EmployeeData [count] .LastName);

printf(请输入小时,付款和递延金额:);
scanf(%f%f%f,& EmployeeData [count] .hours,& EmployeeData [count] .payrate,& EmployeeData [count] .deferred);
printf(\ n);
}
void QSortEmployee(PayRecord EmployeeData [],int start,int finish)
{
int left = start,
right = finish;
char * pivot = EmployeeData [(start + finish)/ 2] .LastName;

while(left< right)
{
while((strcmp(EmployeeData [left] .LastName,pivot)< 0)&&(left<右))
{
left ++;
}
while((strcmp(EmployeeData [right] .LastName,pivot)> 0)&&(right> left))
{
right-- ;
}
if(left< = right)
{
PayRecord tempEmployee = EmployeeData [left];
EmployeeData [left] = EmployeeData [right];
EmployeeData [right] = tempEmployee;
left ++;
right--;
}
}
if(start< finish)
{
QSortEmployee(EmployeeData,start,right);
}
if(left< finished)
{
QSortEmployee(EmployeeData,left,finish);
}
}
float CalculateGross(PayRecord EmployeeData [],int count)// 3.4
{
return(EmployeeData [count] .hours< = 40) ? EmployeeData [count] .hours * EmployeeData [count] .payrate:
EmployeeData [count] .payrate * 40 +(EmployeeData [count] .hours - 40)* 1.5 * EmployeeData [count] .payrate;
}

void CalculateHours(PayRecord EmployeeData [],int count)
{
if(EmployeeData [count] .hours< = 40)
{
EmployeeData [count] .reghrs = EmployeeData [count] .hours;
EmployeeData [count] .ovthrs = 0;
}
其他
{
EmployeeData [count] .reghrs = 40;
EmployeeData [count] .ovthrs =(EmployeeData [count] .hours - 40);
}
}
float CalculateNet(PayRecord EmployeeData [],int count)
{
返回EmployeeData [count] .gross - (EmployeeData [count] .fedtax + EmployeeData [count] .statetax + EmployeeData [count] .ssitax + EmployeeData [count] .deferred);
}
void AddAccumulators(PayRecord EmployeeData [],int count,int maxemployees)
{
EmployeeData [MAXEMPLOYEES - 1] .totreg = EmployeeData [MAXEMPLOYEES - 1] .totreg + EmployeeData工作[COUNT] .reghrs;
EmployeeData [MAXEMPLOYEES - 1] .totovt = EmployeeData [MAXEMPLOYEES - 1] .totovt + EmployeeData [count] .ovthrs;
EmployeeData [MAXEMPLOYEES - 1] .totrate = EmployeeData [MAXEMPLOYEES - 1] .totrate + EmployeeData [count] .payrate;
EmployeeData [MAXEMPLOYEES - 1] .totgross = EmployeeData [MAXEMPLOYEES - 1] .totgross + EmployeeData [count] .gross;
EmployeeData [MAXEMPLOYEES - 1] .totfed = EmployeeData [MAXEMPLOYEES - 1] .totfed + EmployeeData [count] .fedtax;
EmployeeData [MAXEMPLOYEES - 1] .totstate = EmployeeData [MAXEMPLOYEES - 1] .totstate + EmployeeData [count] .statetax;
EmployeeData [MAXEMPLOYEES - 1] .totssi = EmployeeData [MAXEMPLOYEES - 1] .totssi + EmployeeData [count] .ssitax;
EmployeeData [MAXEMPLOYEES - 1] .totdefr = EmployeeData [MAXEMPLOYEES - 1] .totdefr + EmployeeData [count] .deferred;
EmployeeData [MAXEMPLOYEES - 1] .totnet = EmployeeData [MAXEMPLOYEES - 1] .totnet + EmployeeData [count] .net;
}

void showReport(FILE * reportFile,char * FullName,PayRecord EmployeeData [],int count)//3.8
{
printf(HEADLINE1);
printf(HEADLINE2);
printf(HEADLINE3);
printf(EMPLOYEEREPORT,FullName,EmployeeData [count] .payrate,EmployeeData [count] .reghrs,EmployeeData [count] .gross,EmployeeData [count] .fedtax,EmployeeData [count] .ssitax,EmployeeData [count]。净);
printf(EMPLOYEEREPORT2,EmployeeData [count] .ovthrs,EmployeeData [count] .statetax,EmployeeData [count] .deferred);
fprintf(reportFile,EMPLOYEEREPORT,FullName,EmployeeData [count] .payrate,EmployeeData [count] .reghrs,EmployeeData [count] .gross,EmployeeData [count] .fedtax,EmployeeData [count] .ssitax,EmployeeData [count ]。净);
fprintf(reportFile,EMPLOYEEREPORT2,EmployeeData [count] .ovthrs,EmployeeData [count] .statetax,EmployeeData [count] .deferred);
}
void printTotals(FILE * reportFile,PayRecord EmployeeData [],int maxemployees)
{
printf(BREAK);
printf(TOTALS1,EmployeeData [MAXEMPLOYEES - 1] .totrate,EmployeeData [MAXEMPLOYEES - 1] .totreg,EmployeeData [MAXEMPLOYEES - 1] .totgross,EmployeeData [MAXEMPLOYEES - 1] .totfed,EmployeeData [MAXEMPLOYEES - 1] .totssi,EmployeeData [MAXEMPLOYEES - 1] .totnet);
printf(TOTALS2,EmployeeData [MAXEMPLOYEES - 1] .totovt,EmployeeData [MAXEMPLOYEES - 1] .totstate,EmployeeData [MAXEMPLOYEES - 1] .totdefr);
fprintf(reportFile,BREAK);
fprintf(reportFile,TOTALS1,EmployeeData [MAXEMPLOYEES - 1] .totrate,EmployeeData [MAXEMPLOYEES - 1] .totreg,EmployeeData [MAXEMPLOYEES - 1] .totgross,EmployeeData [MAXEMPLOYEES - 1] .totfed,EmployeeData [MAXEMPLOYEES - 1] .totssi,EmployeeData [MAXEMPLOYEES - 1] .totnet);
fprintf(reportFile,TOTALS2,EmployeeData [MAXEMPLOYEES - 1] .totovt,EmployeeData [MAXEMPLOYEES - 1] .totstate,EmployeeData [MAXEMPLOYEES - 1] .totdefr);
}
void printAverages(FILE * reportFile,int maxemployees,PayRecord EmployeeData [])
{
printf(AVERAGES1,(EmployeeData [MAXEMPLOYEES - 1] .totrate / MAXEMPLOYEES), (EmployeeData [MAXEMPLOYEES - 1] .totreg / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totgross / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totfed / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totssi / MAXEMPLOYEES ),(EmployeeData [MAXEMPLOYEES - 1] .totnet / MAXEMPLOYEES));
printf(AVERAGES2,(EmployeeData [MAXEMPLOYEES - 1] .totovt / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totstate / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totdefr / MAXEMPLOYEES));
fprintf(reportFile,AVERAGES1,(EmployeeData [MAXEMPLOYEES - 1] .totrate / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totreg / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totgross / MAXEMPLOYEES),( EmployeeData [MAXEMPLOYEES - 1] .totfed / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totssi / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totnet / MAXEMPLOYEES));
fprintf(reportFile,AVERAGES2,(EmployeeData [MAXEMPLOYEES - 1] .totovt / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totstate / MAXEMPLOYEES),(EmployeeData [MAXEMPLOYEES - 1] .totdefr / MAXEMPLOYEES));
}
float CalcFedtax(PayRecord EmployeeData [],int count)// 3.5.1
{
return(EmployeeData [count] .gross - EmployeeData [count] .deferred) * FEDTAXRATE;
}
float CalcStatetax(PayRecord EmployeeData [],int count)// 3.5.2
{
返回EmployeeData [count] .fedtax * STATETAXRATE;
}
float CalcSSItax(PayRecord EmployeeData [],int count)// 3.5.3
{
return(EmployeeData [count] .gross - EmployeeData [count] .deferred) * SSITAXRATE;
}
void CalculateTaxes(PayRecord EmployeeData [],int count)//3.5
{
EmployeeData [count] .fedtax = CalcFedtax(EmployeeData,count); //调用3.5.1
EmployeeData [count] .statetax = CalcStatetax(EmployeeData,count); //调用3.5.2
EmployeeData [count] .ssitax = CalcSSItax(EmployeeData,count); //调用3.5.3
}
// ====================

void printCheckHeader(PayRecord EmployeeData [],char * CheckName,int chkNo,char * refCode,int count)
{
printf(BORDER);
printf(HEADER,chkNo);
printf(BLANK);
printf(HEADER2);
printf(HEADER3);
printf(HEADER4);
printf(BLANK);
printf(BLANK);
printf(HEADER5);
printf(BLANK);
printf(HEADER6,CheckName);
printf(BLANK);
printf(HEADER7);
printf(BLANK);
printf(BLANK);
printf(HEADER8,refCode);
printf(BLANK);
printf(BLANK);
printf(BORDER);
}
void printCheckStub(PayRecord EmployeeData [],char * CheckName,int chkNo,int count)
{
printf(STUB,chkNo);
printf(BLANK);
printf(STUB2,CheckName);
printf(STUB3);
printf(STUB4);
printf(STUB5);
printf(BLANK);
printf(BLANK);
printf(BORDER);
printf(\ n);
}
BOOLEAN isVowel(char aChar)
{
switch(toupper(aChar))//< ;ctype.h>
{
案例'A':
案例'E':
案例'我':
案例'O':
案例'U':返回TRUE;打破;
默认值:返回FALSE;
}
}
char * generateRefCode(PayRecord EmployeeData [],int chkNo,int count)
{

static char tempStr [15 + 10 + 1];
char buffer [10 + 1];
int i,j = 1;

//这个循环用于清除字符串每次generateRefCode被称为
for(i = 0; i< 26; i ++)
{
tempStr [ i] = 0;
}
// tempStr [0] =姓氏[0]; //需要将第一个字母放入临时字符串中。
tempStr [0] = toupper(EmployeeData [count] .LastName [0]);
//这应该是保存姓氏第一个字母的第一个地址。
// LOOP
//需要遍历姓氏中的所有字符。
//检查这封信是否是一个呜呜声。如果是,则添加到临时字符串,否则省略它。
// ENDLOOP
for(i = 1; i< strlen(EmployeeData [count] .LastName); i ++)
{
if(!isVowel(EmployeeData [count] .LastName [i]))
{
tempStr [j ++] = toupper(EmployeeData [count] .LastName [i]);
}
}

sprintf(缓冲区,%d,chkNo);
// sprintf用于使用char缓冲区将int chkNo转换为字符串
strcat(tempStr,buffer);
tempStr [j + 3] = 0;
//添加字符串终止符,添加当前下标+ 3,因为我们在字符串的末尾添加
// 3个数字,以便它将字符串终结符
//添加到结尾的字符串。
返回tempStr;
}

int main(void)
{
PayRecord EmployeeData [MAXEMPLOYEES];
char FullName [15 + 10 + 1];
char CheckName [15 + 10 + 1];
char * refCode;
int count;
int chkNo = 100;
FILE * reportFile; //读为reportFile是指向文件的指针

reportFile = fopen(./ report.txt,wt); //打开文件进行写入模式访问

if(reportFile == NULL)//测试文件DID是否未打开,退出。
{
printf(报告文件打开失败...... \ n);
fflush(stdin);
退出(-30); // reqs< stdlib.h>
}

PrintHeadings(reportFile); //调用3.1
InitAccumulators(EmployeeData,(MAXEMPLOYEES - 1)); //调用3.2
for(count = 0 ; count< MAXEMPLOYEES; count ++)
{
InputEmployeeData(EmployeeData,count);
}

QSortEmployee(EmployeeData,0,(MAXEMPLOYEES - 1));

for(count = 0; count< MAXEMPLOYEES; count ++)
{
strcpy(FullName,EmployeeData [count] .LastName);
strcat(FullName,,);
strcat(FullName,EmployeeData [count] .FirstName);
EmployeeData [count] .gross = CalculateGross(EmployeeData,count); //调用3.4
// CalculateHours()实际上不计算任何小时数
//它将小时分成正常时间和加班时间
CalculateHours(EmployeeData,count); //调用3.4.1
CalculateTaxes(EmployeeData,count); //调用3.5
EmployeeData [count] .net = CalculateNet(EmployeeData,count); //调用3.6
AddAccumulators(EmployeeData,count,(MAXEMPLOYEES - 1)); //调用3.7
showReport(reportFile,FullName,EmployeeData,count); //调用3.8
}

printTotals(reportFile,EmployeeData,(MAXEMPLOYEES - 1)); //调用3.8.1
printAverages(reportFile,(MAXEMPLOYEES - 1),EmployeeData); //调用3.8.2

for(count = 0; count< MAXEMPLOYEES; count ++)
{
refCode = generateRefCode(EmployeeData,chkNo,count);
strcpy(CheckName,EmployeeData [count] .FirstName);
strcat(CheckName,);
strcat(CheckName,EmployeeData [count] .LastName);
printCheckHeader(EmployeeData,CheckName,chkNo,refCode,count);
printCheckStub(EmployeeData,CheckName,chkNo,count);
chkNo ++;
}

fclose(reportFile); //关闭文件
fflush(stdin);
getchar();
返回0;
}





感谢您的帮助!

解决方案

< blockquote>对不起,我嘲笑你的费用,你做了所有这些工作而错过了基本的工作。



一个关于上帝的爱的评论为总数做了一个单独的记录它并不难

 EmployeeData TotalsRecord; 



这将阻止你做你做过的愚蠢的事情,即使用最后一个员工记录来保存总数,然后再将其排除,这样它就不再是最后一次了:-)



查看问题



InitAccumulators =>零记录(MAXEMPLOYEES - 1)

AddAccumulators =>您将所有总计添加到(MAXEMPLOYEES - 1)

QSortEmployee(EmployeeData,0,(MAXEMPLOYEES - 1)); =>然后你Qsort数据,包括你的总数记录:-)



负数是因为你没有把任何其他字段归零,所以他们只是有垃圾它们

Hello guys and good evening!

The problem that I have been having with my code is that my totals and averages do not show the correct values that they should be! Everything else in my program works so far, the check headers, QSort, formatting for employee data, and displaying correct information entered for everything else.

You can see the program in action here:
Part 1
Part 2
Part 3
Part 4

I changed the order of which the information is stored, since payrate and hours were accidentally the opposite way. Also bumped up my print totals and print averages before the print checks.

Now without further ado, my source code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef short BOOLEAN;
typedef char STR15[15 + 1];
typedef char STR10[10 + 1];
typedef struct PayRecord
{
	STR15 LastName;
	STR10 FirstName;
	float hours, payrate, deferred, gross, fedtax, statetax, ssitax, net;
	float reghrs, ovthrs;
	float totreg, totovt, totrate, totgross, totfed, totstate, totssi, totdefr, totnet;
} PayRecord;

#define FEDTAXRATE 0.15
#define STATETAXRATE 0.07
#define SSITAXRATE 0.0775
#define MAXEMPLOYEES 5
#define TRUE 1
#define FALSE 0

#define HEADLINE1       "Employee         Pay    Reg Hrs  Gross    Fed    SSI    Net\n"
#define HEADLINE2       "Name             Rate   Ovt Hrs  Pay     State   Defr   Pay\n"
#define HEADLINE3       "=========        ====== ======= ======= ======= ====== =======\n"
#define EMPLOYEEREPORT  "%-16s %5.2f    %5.2f %7.2f  %6.2f %6.2f %7.2f\n"
#define EMPLOYEEREPORT2 "                          %5.2f          %6.2f %6.2f\n\n"
#define BREAK           "==============================================================\n"
#define TOTALS1         "Totals           %5.2f   %6.2f %6.2f  %6.2f %6.2f %7.2f\n"
#define TOTALS2         "                         %6.2f           %5.2f %6.2f\n\n"
#define AVERAGES1       "Averages         %5.2f   %6.2f  %5.2f  %6.2f %6.2f %7.2f\n"
#define AVERAGES2       "                         %6.2f           %5.2f %6.2f\n\n"

#define BLANK	"*							   *\n"
#define BORDER  "************************************************************\n"
#define HEADER  "*				Check No %3d		   *\n"
#define HEADER2 "*   Sabre Corporation					   *\n"
#define HEADER3 "*   15790 West Henness lane				   *\n"
#define HEADER4 "*   New Corio, New Mexico 65790				   *\n"
#define HEADER5 "*   Pay to the Order of					   *\n"
#define HEADER6 "*   %-16s					   *\n"
#define HEADER7 "*   The Sum Nine Hundred Ninety-Nine and XX/100 Dollars    *\n"
#define HEADER8 "*   Reference: %-16s				   *\n"

#define STUB 	"*   Check No %3d					   *\n"
#define STUB2	"*   %-16s	   Reg Pay		Fed Tax	   *\n"
#define STUB3	"*   Address		   Ovt Pay		State Tax  *\n"
#define STUB4	"*   City, State		   Gross Pay		SSI Tax    *\n"
#define STUB5	"*   Zip			   Net Pay		Tax Total  *\n"

void PrintHeadings(FILE *reportFile)
{
	fprintf(reportFile, HEADLINE1);
	fprintf(reportFile, HEADLINE2);
	fprintf(reportFile, HEADLINE3);
}
void InitAccumulators(PayRecord EmployeeData[], int maxemployees)
{
	EmployeeData[MAXEMPLOYEES - 1].totreg = 0;
	EmployeeData[MAXEMPLOYEES - 1].totovt = 0;
	EmployeeData[MAXEMPLOYEES - 1].totrate = 0;
	EmployeeData[MAXEMPLOYEES - 1].totgross = 0;
	EmployeeData[MAXEMPLOYEES - 1].totfed = 0;
	EmployeeData[MAXEMPLOYEES - 1].totstate = 0;
	EmployeeData[MAXEMPLOYEES - 1].totssi = 0;
	EmployeeData[MAXEMPLOYEES - 1].totdefr = 0;
	EmployeeData[MAXEMPLOYEES - 1].totnet = 0;
}
void InputEmployeeData(PayRecord EmployeeData[], int count)// 3.3
{
	printf("Please enter your first and last name: ");
	scanf("%s%s", EmployeeData[count].FirstName, EmployeeData[count].LastName);

	printf("Please enter hours, payrate, and deferred amount: ");
	scanf("%f%f%f", &EmployeeData[count].hours, &EmployeeData[count].payrate, &EmployeeData[count].deferred);
	printf("\n");
}
void QSortEmployee(PayRecord EmployeeData[], int start, int finish)
{
	int left = start,
		right = finish;
	char * pivot = EmployeeData[(start + finish) / 2].LastName;

	while (left < right)
	{
		while ((strcmp(EmployeeData[left].LastName, pivot) < 0) && (left < right))
		{
			left++;
		}
		while ((strcmp(EmployeeData[right].LastName, pivot) > 0) && (right > left))
		{
			right--;
		}
		if (left <= right)
		{
			PayRecord tempEmployee = EmployeeData[left];
			EmployeeData[left] = EmployeeData[right];
			EmployeeData[right] = tempEmployee;
			left++;
			right--;
		}
	}
	if (start < finish)
	{
		QSortEmployee(EmployeeData, start, right);
	}
	if (left < finish)
	{
		QSortEmployee(EmployeeData, left, finish);
	}
}
float CalculateGross(PayRecord EmployeeData[], int count) // 3.4
{
	return (EmployeeData[count].hours <= 40) ? EmployeeData[count].hours * EmployeeData[count].payrate :
		EmployeeData[count].payrate * 40 + (EmployeeData[count].hours - 40) * 1.5 * EmployeeData[count].payrate;
}

void CalculateHours(PayRecord EmployeeData[], int count)
{
	if (EmployeeData[count].hours <= 40)
	{
		EmployeeData[count].reghrs = EmployeeData[count].hours;
		EmployeeData[count].ovthrs = 0;
	}
	else
	{
		EmployeeData[count].reghrs = 40;
		EmployeeData[count].ovthrs = (EmployeeData[count].hours - 40);
	}
}
float CalculateNet(PayRecord EmployeeData[], int count)
{
	return EmployeeData[count].gross - (EmployeeData[count].fedtax + EmployeeData[count].statetax + EmployeeData[count].ssitax + EmployeeData[count].deferred);
}
void AddAccumulators(PayRecord EmployeeData[], int count, int maxemployees)
{
	EmployeeData[MAXEMPLOYEES - 1].totreg = EmployeeData[MAXEMPLOYEES - 1].totreg + EmployeeData[count].reghrs;
	EmployeeData[MAXEMPLOYEES - 1].totovt = EmployeeData[MAXEMPLOYEES - 1].totovt + EmployeeData[count].ovthrs;
	EmployeeData[MAXEMPLOYEES - 1].totrate = EmployeeData[MAXEMPLOYEES - 1].totrate + EmployeeData[count].payrate;
	EmployeeData[MAXEMPLOYEES - 1].totgross = EmployeeData[MAXEMPLOYEES - 1].totgross + EmployeeData[count].gross;
	EmployeeData[MAXEMPLOYEES - 1].totfed = EmployeeData[MAXEMPLOYEES - 1].totfed + EmployeeData[count].fedtax;
	EmployeeData[MAXEMPLOYEES - 1].totstate = EmployeeData[MAXEMPLOYEES - 1].totstate + EmployeeData[count].statetax;
	EmployeeData[MAXEMPLOYEES - 1].totssi = EmployeeData[MAXEMPLOYEES - 1].totssi + EmployeeData[count].ssitax;
	EmployeeData[MAXEMPLOYEES - 1].totdefr = EmployeeData[MAXEMPLOYEES - 1].totdefr + EmployeeData[count].deferred;
	EmployeeData[MAXEMPLOYEES - 1].totnet = EmployeeData[MAXEMPLOYEES - 1].totnet + EmployeeData[count].net;
}

void showReport(FILE *reportFile, char *FullName, PayRecord EmployeeData[], int count) //3.8
{
	printf(HEADLINE1);
	printf(HEADLINE2);
	printf(HEADLINE3);
	printf(EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
	printf(EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
	fprintf(reportFile, EMPLOYEEREPORT, FullName, EmployeeData[count].payrate, EmployeeData[count].reghrs, EmployeeData[count].gross, EmployeeData[count].fedtax, EmployeeData[count].ssitax, EmployeeData[count].net);
	fprintf(reportFile, EMPLOYEEREPORT2, EmployeeData[count].ovthrs, EmployeeData[count].statetax, EmployeeData[count].deferred);
}
void printTotals(FILE *reportFile, PayRecord EmployeeData[], int maxemployees)
{
	printf(BREAK);
	printf(TOTALS1, EmployeeData[MAXEMPLOYEES - 1].totrate, EmployeeData[MAXEMPLOYEES - 1].totreg, EmployeeData[MAXEMPLOYEES - 1].totgross, EmployeeData[MAXEMPLOYEES - 1].totfed, EmployeeData[MAXEMPLOYEES - 1].totssi, EmployeeData[MAXEMPLOYEES - 1].totnet);
	printf(TOTALS2, EmployeeData[MAXEMPLOYEES - 1].totovt, EmployeeData[MAXEMPLOYEES - 1].totstate, EmployeeData[MAXEMPLOYEES - 1].totdefr);
	fprintf(reportFile, BREAK);
	fprintf(reportFile, TOTALS1, EmployeeData[MAXEMPLOYEES - 1].totrate, EmployeeData[MAXEMPLOYEES - 1].totreg, EmployeeData[MAXEMPLOYEES - 1].totgross, EmployeeData[MAXEMPLOYEES - 1].totfed, EmployeeData[MAXEMPLOYEES - 1].totssi, EmployeeData[MAXEMPLOYEES - 1].totnet);
	fprintf(reportFile, TOTALS2, EmployeeData[MAXEMPLOYEES - 1].totovt, EmployeeData[MAXEMPLOYEES - 1].totstate, EmployeeData[MAXEMPLOYEES - 1].totdefr);
}
void printAverages(FILE *reportFile, int maxemployees, PayRecord EmployeeData[])
{
	printf(AVERAGES1, (EmployeeData[MAXEMPLOYEES - 1].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totnet / MAXEMPLOYEES));
	printf(AVERAGES2, (EmployeeData[MAXEMPLOYEES - 1].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totdefr / MAXEMPLOYEES));
	fprintf(reportFile, AVERAGES1, (EmployeeData[MAXEMPLOYEES - 1].totrate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totreg / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totgross / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totfed / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totssi / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totnet / MAXEMPLOYEES));
	fprintf(reportFile, AVERAGES2, (EmployeeData[MAXEMPLOYEES - 1].totovt / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totstate / MAXEMPLOYEES), (EmployeeData[MAXEMPLOYEES - 1].totdefr / MAXEMPLOYEES));
}
float CalcFedtax(PayRecord EmployeeData[], int count) // 3.5.1
{
	return (EmployeeData[count].gross - EmployeeData[count].deferred)*FEDTAXRATE;
}
float CalcStatetax(PayRecord EmployeeData[], int count) // 3.5.2
{
	return EmployeeData[count].fedtax * STATETAXRATE;
}
float CalcSSItax(PayRecord EmployeeData[], int count) // 3.5.3
{
	return (EmployeeData[count].gross - EmployeeData[count].deferred)*SSITAXRATE;
}
void CalculateTaxes(PayRecord EmployeeData[], int count) //3.5
{
	EmployeeData[count].fedtax = CalcFedtax(EmployeeData, count); // call 3.5.1
	EmployeeData[count].statetax = CalcStatetax(EmployeeData, count); // call 3.5.2
	EmployeeData[count].ssitax = CalcSSItax(EmployeeData, count); // call 3.5.3
}
//====================

void printCheckHeader(PayRecord EmployeeData[], char *CheckName, int chkNo, char *refCode, int count)
{
	printf(BORDER);
	printf(HEADER, chkNo);
	printf(BLANK);
	printf(HEADER2);
	printf(HEADER3);
	printf(HEADER4);
	printf(BLANK);
	printf(BLANK);
	printf(HEADER5);
	printf(BLANK);
	printf(HEADER6, CheckName);
	printf(BLANK);
	printf(HEADER7);
	printf(BLANK);
	printf(BLANK);
	printf(HEADER8, refCode);
	printf(BLANK);
	printf(BLANK);
	printf(BORDER);
}
void printCheckStub(PayRecord EmployeeData[], char *CheckName, int chkNo, int count)
{
	printf(STUB, chkNo);
	printf(BLANK);
	printf(STUB2, CheckName);
	printf(STUB3);
	printf(STUB4);
	printf(STUB5);
	printf(BLANK);
	printf(BLANK);
	printf(BORDER);
	printf("\n");
}
BOOLEAN isVowel(char aChar)
{
	switch (toupper(aChar)) //<ctype.h>
	{
	case 'A':
	case 'E':
	case 'I':
	case 'O':
	case 'U': return TRUE; break;
	default: return FALSE;
	}
}
char *generateRefCode(PayRecord EmployeeData[], int chkNo, int count)
{

	static char tempStr[15 + 10 + 1];
	char buffer[10 + 1];
	int i, j = 1;

	//This loop is used to clear the string everytime generateRefCode is called
	for (i = 0; i < 26; i++)
	{
		tempStr[i] = 0;
	}
	//tempStr[0] = lastname[0]; // Needs to put the first letter into the temporary string.
	tempStr[0] = toupper(EmployeeData[count].LastName[0]);
	// This should be the first address that holds the first letter of the last name.
	// LOOP
	// Need to get through all the characters in the lastname.
	// Check if the letter is a vowl. If it is, add to the temporary string, else omit it.
	// ENDLOOP
	for (i = 1; i < strlen(EmployeeData[count].LastName); i++)
	{
		if (!isVowel(EmployeeData[count].LastName[i]))
		{
			tempStr[j++] = toupper(EmployeeData[count].LastName[i]);
		}
	}

	sprintf(buffer, "%d", chkNo);
	//sprintf is used to make int chkNo into a string using a char buffer
	strcat(tempStr, buffer);
	tempStr[j + 3] = 0;
	// add string terminator, adds the the current subscript+3 because we add
	// 3 numbers to the end of the string so that it adds the string terminator
	// to the end of the string.
	return tempStr;
}

int main(void)
{
	PayRecord EmployeeData[MAXEMPLOYEES];
	char FullName[15 + 10 + 1];
	char CheckName[15 + 10 + 1];
	char *refCode;
	int count;
	int chkNo = 100;
	FILE *reportFile; // Read as "reportFile is a pointer to a file"

	reportFile = fopen("./report.txt", "wt");// open the file for "write-mode" access

	if (reportFile == NULL)// Test if file DID NOT open, exit.
	{
		printf(" report file open failed ...\n");
		fflush(stdin);
		exit(-30); //reqs <stdlib.h>
	}

	PrintHeadings(reportFile);//Call 3.1
	InitAccumulators(EmployeeData, (MAXEMPLOYEES - 1));//call 3.2
	for (count = 0; count < MAXEMPLOYEES; count++)
	{
		InputEmployeeData(EmployeeData, count);
	}

	QSortEmployee(EmployeeData, 0, (MAXEMPLOYEES - 1));

	for (count = 0; count < MAXEMPLOYEES; count++)
	{
		strcpy(FullName, EmployeeData[count].LastName);
		strcat(FullName, ", ");
		strcat(FullName, EmployeeData[count].FirstName);
		EmployeeData[count].gross = CalculateGross(EmployeeData, count);// Call 3.4
		//CalculateHours() does not actually calculate any hours
		//It seperates the hours into regular hours and overtime hours
		CalculateHours(EmployeeData, count);// Call 3.4.1
		CalculateTaxes(EmployeeData, count); // call 3.5
		EmployeeData[count].net = CalculateNet(EmployeeData, count);//call 3.6
		AddAccumulators(EmployeeData, count, (MAXEMPLOYEES - 1));//call 3.7
		showReport(reportFile, FullName, EmployeeData, count);//call 3.8
	}

	printTotals(reportFile, EmployeeData, (MAXEMPLOYEES - 1));//call 3.8.1
	printAverages(reportFile, (MAXEMPLOYEES - 1), EmployeeData);//call 3.8.2

	for (count = 0; count < MAXEMPLOYEES; count++)
	{
		refCode = generateRefCode(EmployeeData, chkNo, count);
		strcpy(CheckName, EmployeeData[count].FirstName);
		strcat(CheckName, " ");
		strcat(CheckName, EmployeeData[count].LastName);
		printCheckHeader(EmployeeData, CheckName, chkNo, refCode, count);
		printCheckStub(EmployeeData, CheckName, chkNo, count);
		chkNo++;
	}

	fclose(reportFile);// Close file
	fflush(stdin);
	getchar();
	return 0;
}



Thanks for the help in advance!

解决方案

Sorry I had laugh at your expense, you do all this work and miss the basic.

One comment for the love of god make a separate record for the totals it's not hard

EmployeeData TotalsRecord; 


It will the stop you doing the stupid thing you have done which is to use the last employee record to hold the totals in and then Qsort it later so it's not last anymore :-)

See the problem

InitAccumulators => zeros the last record (MAXEMPLOYEES - 1)
AddAccumulators => You add all the totals to (MAXEMPLOYEES - 1)
QSortEmployee(EmployeeData, 0, (MAXEMPLOYEES - 1)); => You then Qsort the data including your totals record :-)

The negative numbers are because you don't zero any of the other fields and so they just have rubbish in them


这篇关于如何显示正确的总计和平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆