将C程序分离为void函数

时间:2012-07-25 02:50:30

标签: c void

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

1 个答案:

答案 0 :(得分:1)

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

这不可能是好事。

你还需要通过引用来研究传递和传递值InitializeAccumulators()只是没有按照你的假设做。