gpt4 book ai didi

c - 将 C 程序分成 void 函数

转载 作者:行者123 更新时间:2023-11-30 15:55:53 25 4
gpt4 key购买 nike

我是编程新手。我正在尝试将这个程序分成更小的部分。首先,我正在看的部分是第 3.2 项:InitializeAccumulators。程序正在运行,但数学运算不断创建超长数字。其次,我正在看第 3.7 部分:AddDetailToAccumulators。这个程序不会运行。它的工资率变量存在问题。有什么想法吗?谢谢一百万!

#include <stdio.h>
#include <stdlib.h>
#include "TAXRATES.h"
#include "EmployeeRecord.h"
#include "CalcTaxes.o"
#define ADDR(var) &var
#define REPORTCOLUMNHEADINGS1 "Employee Pay Reg Hrs Gross Fed SSI Net\n"
#define REPORTCOLUMNHEADINGS2 "Name Rate OVT Hrs Pay State Defr Pay\n"
#define BARS "======== ===== ======= ======= ====== ====== =======\n\n"
#define REPORTCOLUMN1 "%s, %s\t%8.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n"
#define REPORTCOLUMN2 "%32.2f%18.2f%8.2f\n\n"

void PrintReportHeadings(FILE *ReportFile);//3.1
void InitializeAccumulators(float *totpr,float *totreg,float *totovt,float *totgp,float *totfed,
float *totstt,float *totsst,float *totdef,float *totnp,int *empCount);//3.2
extern void CalculateTaxes(float gross, float defr, float *ft, float *st, float *ssit);//3.5
void AddDetailToAccumulators(int *empCount,float *totpr,float *totfed,float *totreg,float *totovt,
float *totgp,float *totstt,float *totsst,float *totnp,float *totdef,float *r.payrate);//3.7


int main()
{
EmployeeRecord r;

float reghours,ovthours;
float ft,st,ssit;
float totpr,totreg,totovt,totgp,totfed,totstt,totsst,totdef,totnp;
float avgpr,avgreg,avgovt,avggp,avgfedt,avgstt,avgsst,avgdef,avgnp;
char answer;
int empCount;
FILE *ReportFile;

void PrintReportHeadings(FILE *ReportFile);//3.1
void InitializeAccumulators(float *totpr,float *totreg,float *totovt,float *totgp,float *totfed,
float *totstt,float *totsst,float *totdef,float *totnp,int *empCount);//3.2
do
{

printf("Enter employee's name: "); // input section
scanf("%s%s",r.firstname,r.lastname);
printf("Enter hourly pay rate: ");
scanf("%f",ADDR(r.payrate));
printf("Enter deferred amount: ");
scanf("%f",ADDR(r.defr));
printf("Enter hours worked this pay period: ");
scanf("%f",ADDR(r.hours));
if (r.hours > 40)
{
reghours = 40;
ovthours = r.hours - 40;
}
else {
reghours = r.hours;
ovthours = 0;
}
r.gross = (reghours * r.payrate)+(ovthours * r.payrate * 1.5);
CalculateTaxes(r.gross,r.defr,&ft,&st,&ssit);//Call 3.5 CalculateTaxes

r.net = r.gross - ft - st - ssit;
printf(REPORTCOLUMNHEADINGS1);
printf(REPORTCOLUMNHEADINGS2);
printf(BARS);
printf(REPORTCOLUMN1,r.lastname,r.firstname,r.payrate,reghours,r.gross,ft,ssit,r.net);
printf(REPORTCOLUMN2,ovthours,st,r.defr);
ReportFile = fopen("report.txt", "a");
fprintf(ReportFile,REPORTCOLUMN1,r.lastname,r.firstname,r.payrate,reghours,r.gross,ft,ssit,r.net);
fprintf(ReportFile,REPORTCOLUMN2,ovthours,st,r.defr);
fclose(ReportFile);

void AddDetailToAccumulators(int *empCount,float *totpr,float *totfed,float *totreg,float *totovt,
float *totgp,float *totstt,float *totsst,float *totnp,float *totdef,float *r.payrate);//3.7

avgpr = totpr / empCount;
avgfedt = totfed / empCount;
avgreg = totreg / empCount;
avgovt = totovt / empCount;
avggp = totgp / empCount;
avgstt = totstt / empCount;
avgsst = totsst / empCount;
avgnp = totnp / empCount;
avgdef = totdef / empCount;

fflush(stdin);
printf(" Repeat (Y/N)? : ");
scanf("%c",ADDR(answer));
} while (answer == 'Y' || answer == 'y');
printf("\n"); // print one line for spacing
printf("\nTotals %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",totpr,totreg,totgp,totfed,totsst,totnp);
printf("%32.2f%18.2f%8.2f\n",totovt,totstt,totdef);
printf("\nAverages %15.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",avgpr,avgreg,avggp,avgfedt,avgstt,avgnp);
printf("%32.2f%18.2f%8.2f\n",avgovt,avgsst,avgdef);
ReportFile = fopen("report.txt", "a");
fprintf(ReportFile,"\nTotals %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",totpr,totreg,totgp,totfed,totsst,totnp);
fprintf(ReportFile,"%32.2f%18.2f%8.2f\n",totovt,totstt,totdef);
fprintf(ReportFile,"\nAverages %15.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",avgpr,avgreg,avggp,avgfedt,avgsst,avgnp);
fprintf(ReportFile,"%32.2f%18.2f%8.2f\n",avgovt,avgstt,avgdef);
fclose(ReportFile);
fflush(stdin);
getchar();
return 0;
}

void PrintReportHeadings(FILE *ReportFile)//3.1
{
ReportFile = fopen("report.txt", "w");
fprintf(ReportFile,REPORTCOLUMNHEADINGS1);
fprintf(ReportFile,REPORTCOLUMNHEADINGS2);
fprintf(ReportFile,BARS);
fclose(ReportFile);
}
void InitializeAccumulators(float *totpr,float *totreg,float *totovt,float *totgp,float *totfed,
float *totstt,float *totsst,float *totdef,float *totnp,int *empCount)//3.2
{
empCount = 0;
totpr = totreg = totovt = totgp = totfed = totstt = totdef = totsst = totnp = 0;
}
void AddDetailToAccumulators(int *empCount,float *totpr,float *totfed,float *totreg,float *totovt,
float *totgp,float *totstt,float *totsst,float *totnp,float *totdef,float *r.payrate)
{
empCount = empCount + 1;
totpr = totpr + payrate;
totfed = totfed + ft;
if (r.hours > 40)
{
reghours = 40;
ovthours = r.hours - 40;
}
else {
reghours = r.hours;
ovthours = 0;
}
totreg = totreg + reghours;
totovt = totovt + ovthours;
totgp = totgp + r.gross;
totstt = totstt + st;
totsst = totsst + ssit;
totnp = totnp + r.net;
totdef = totdef + r.defr;
}

最佳答案

您的变量是指针类型,但您将它们用作基元。

这可不好。

您还需要研究按引用传递和按值传递 InitializeAccumulators() 只是不会执行您假设的操作。

关于c - 将 C 程序分成 void 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com