1

所以下面是我的代码的主要部分。我正在使用一些外部文件。在函数 3.6 中,我无法取我(在这种情况下)总工资率(pr)和总雇员数(empCount)的平均值。我在这里缺少什么吗?当我编译它时,我收到一个错误,指出操作数无效。当我修复这些时,我的输出为 0。我对编程很陌生,所以在我尝试回答任何问题时请耐心等待。我遇到问题的函数是代码中倒数第二个函数(3.6 AddDetailToAccumulator)。非常感谢。

#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(int count,char * lastname,char * firstname,float * hours,float * payrate,float * defr); //3.3
float CalculateGross(float hours, float payrate, float reghours, float ovthours); //3.4 CalculateGross
extern void CalculateTaxes(float gross, float defr, float *ft, float *st, float *ssit); //3.5 CalculateTaxes
void AddDetailToAccumulator(int count,int *empCount,float *pr,float *payrate,float *reg,float *reghours,float *ovt,float *ovthours,float *gp,
                                float *gross,float *fedt,float *ft,float *stt,float *st,float *sst,float *ssit,float *def,
                                float *defr,float *np,float *net,float *avgpr);//3.6 AddDetailToAccumulator
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,count;
     FILE * ReportFile;

     PrintReportHeadings(ReportFile); //Call 3.1 PrintReportHeadings      

     empCount = 0;// count initializations  
     pr = reg = ovt = gp = fedt = stt = def = sst = np = 0;
     avgpr = avgreg = avgovt = avggp = avgfedt = avgstt = avgsst = avgdef = avgnp = 0;
     do
       {
         InputEmployeeData(count,r.firstname,r.lastname,&r.payrate,&r.defr,&r.hours);//Call 3.3 InputEmployeeData   
         if (r.hours > 40)//Check for Overtime Hours
           {
             reghours = 40;
             ovthours = r.hours - 40;
           }
         else {
             reghours = r.hours;
             ovthours = 0;
         }
          r.gross = CalculateGross(r.hours,r.payrate,reghours,ovthours); //3.4 CalculateGross
          CalculateTaxes(r.gross,r.defr,&ft,&st,&ssit);//Call 3.5 CalculateTaxes

          r.net = r.gross - ft - st - ssit;// Calculate Net Earnings

          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);

          AddDetailToAccumulator(count,&empCount,&pr,&r.payrate,&reg,&reghours,&ovt,&ovthours,&gp,&r.gross,&fedt,&ft,&stt,&st,&sst,&ssit,
                                 &def,&r.defr,&np,&r.net,&avgpr);//3.6


              while (getchar() != '\n');
              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("%d",count);

       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 PrintSummaryReport       

        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(int count,char * lastname,char * firstname, float * payrate, float * defr, float * hours) //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);
}

float CalculateGross(float hours, float payrate, float reghours, float ovthours) // 3.4
{
    return (reghours * payrate)+(ovthours * payrate * 1.5);
}

void AddDetailToAccumulator(int *empCount,float *pr,float *payrate,float *reg,float *reghours,float *ovt,float *ovthours,float *gp,
                                float *gross,float *fedt,float *ft,float *stt,float *st,float *sst,float *ssit,float *def,
                                float *defr,float *np,float *net,float *avgpr)//3.6
{
         empCount = empCount +1;
         *pr = *pr + *payrate;
         *reg = *reg + *reghours;
         *ovt = *ovt + *ovthours;
         *gp = *gp + *gross;
         *fedt = *fedt + *ft; 
         *stt = *stt + *st; 
         *sst = *sst + *ssit;
         *def = *def + *defr;
         *np = *np + *net; 

         *avgpr = *pr / empCount;        
} 


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

2 回答 2

3

您在这里缺少 empcount 变量上的 * :

雇员计数 = 雇员计数 +1;

和这里

*avgpr = *pr / empCount ;

于 2012-07-31T03:53:17.427 回答
2

正如前面的答案所指出的,您缺少*操作员。这称为dereference运算符。

你在做什么:

empCount = empCount +1;
// or
empCount++;     // Same assignment

在这里,您正在执行指针运算。这个赋值将指针推进到下一个整数。

你需要做的:

(*empcount)++;

取消引用指针将增加指针指向的 。换句话说,如果指针的值具有地址0x01,则上面的行将访问内存位置0x01处的整数并将其加一。原始代码只会将指针递增到下一个整数(对于 32 位整数将递增到0x05 )。

于 2012-07-31T04:07:36.267 回答