0

我是编程新手。我试图把这个程序分成更小的部分。首先,我正在查看的部分是项目 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;
}
4

1 回答 1

1

您的变量是指针类型,但随后您将它们用作原语。

那可不好。

您还需要研究按引用传递和按值传递 InitializeAccumulators() 只是没有按照您的假设进行。

于 2012-07-25T02:57:10.230 回答