1

下面是我的程序的主文件。我在第 44 行遇到了问题。当我输入递延收益(defr)的数字时,程序正常运行。但是,如果我将递延收益 (defr) 保留为“0”,我的输出就会偏离应有的水平。任何人都可以为我指出正确的方向吗?我不相信包含的文件会有任何问题。它们是独立的功能。谢谢!

编辑:抱歉,第 44 行是:InputEmployeeData(r.firstname,r.lastname,&r.payrate,&r.defr,&r.hours);//Call 3.3

#include <stdio.h>
#include <stdlib.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 InputEmployeeData(char * lastname,char * firstname,float * hours,float * payrate,float * defr); //3.3
extern void CalculateTaxes(float gross, float defr, float *ft, float *st, float *ssit); //3.5
void PrintSummaryReport(float pr,float reg,float ovt,float gp,float fedt,float stt,float sst,float def,float np,float avgpr,float avgreg,float avgovt, 
                        float avggp,float avgfedt,float avgstt,float avgsst,float avgdef,float avgnp,FILE * ReportFile); //3.7

int main()
    {
     EmployeeRecord r; // Call Employee Record Definitions
     float reghours,ovthours; 
     float ft,st,ssit;
     float pr,reg,ovt,gp,fedt,stt,sst,def,np;
     float avgpr,avgreg,avgovt,avggp,avgfedt,avgstt,avgsst,avgdef,avgnp;
     char answer;
     int empCount;
     FILE * ReportFile;

     PrintReportHeadings(ReportFile); //Call 3.1    

     empCount = 0;// count initializations  
     pr = reg = ovt = gp = fedt = stt = def = sst = np = 0;
     do
       {
         InputEmployeeData(r.firstname,r.lastname,&r.payrate,&r.defr,&r.hours);//Call 3.3

         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);
          empCount = empCount + 1;
          pr = pr + r.payrate;
          fedt = fedt + ft;
            if (r.hours > 40)
              {
                   reghours = 40;
                   ovthours = r.hours - 40;
              }
              else {
                   reghours = r.hours;
                   ovthours = 0;
              }
          reg = reg + reghours;    
          ovt = ovt + ovthours;
          gp = gp + r.gross;
          stt = stt + st;
          sst = sst + ssit;
          np = np + r.net;
          def = def + r.defr;
          avgpr = pr / empCount;
          avgfedt = fedt / empCount;
          avgreg = reg / empCount;
          avgovt = ovt / empCount;
          avggp = gp / empCount;
          avgstt = stt / empCount;
          avgsst = sst / empCount;
          avgnp = np / empCount;
          avgdef = def / 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",pr,reg,gp,fedt,sst,np); 
       printf("%32.2f%18.2f%8.2f\n",ovt,stt,def); 
       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);

       PrintSummaryReport(pr,reg,ovt,gp,fedt,stt,sst,def,np,avgpr,avgreg,avgovt,
                        avggp,avgfedt,avgstt,avgsst,avgdef,avgnp,ReportFile); //Call 3.7       

        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 InputEmployeeData(char * lastname,char * firstname, float * hours, float * payrate, float * defr) //3.3
{
     printf("Enter employee's name: ");  // input section
     scanf("%s%s",firstname,lastname);
     printf("Enter hourly pay rate: ");
     scanf("%f",payrate);
     printf("Enter deferred amount: ");
     scanf("%f",defr);
     printf("Enter hours worked this pay period: ");
     scanf("%f",hours);
}

void PrintSummaryReport(float pr,float reg,float ovt,float gp,float fedt,float stt,float sst,float def,float np,float avgpr,float avgreg,float avgovt, 
                        float avggp,float avgfedt,float avgstt,float avgsst,float avgdef,float avgnp,FILE * ReportFile) //3.7
{
     ReportFile = fopen("report.txt", "a");
     fprintf(ReportFile,"\nTotals %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",pr,reg,gp,fedt,sst,np); 
     fprintf(ReportFile,"%32.2f%18.2f%8.2f\n",ovt,stt,def); 
     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);
}
4

1 回答 1

1

你为什么不这样做呢?

void InputEmployeeData(EmployeeRecord& r)
{
     printf("Enter employee's name: ");  // input section
     scanf("%s%s", r.firstname, r.lastname);
     printf("Enter hourly pay rate: ");
     scanf("%f", &r.payrate);
     printf("Enter deferred amount: ");
     scanf("%f", &r.defr);
     printf("Enter hours worked this pay period: ");
     scanf("%f", &r.hours);
}

调用站点:

EmployeeRecord record;
InputEmployeeData(&record);
于 2012-07-26T03:55:50.590 回答