0

私はプログラミングが初めてです。このプログラムを小さな断片に分割しようとしています。まず、私が見ているのは項目 3.2: InitializeAccumulators です。プログラムは実行されていますが、数学は非常に長い数を作成し続けます。次に、ピース 3.7: AddDetailToAccumulators を見ています。このプログラムは実行されません。payrate 変数に問題があります。何かご意見は?どうもありがとう!

#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 に答える