-1

我需要按姓氏的字母顺序对输出进行排序。问题在于,除了姓氏之外,还有其他数据(工资率、总额等)。我负责使用快速排序算法按姓氏对条目进行排序。我还需要使用外部文件来执行此操作。我知道如何链接以及所有这些,尽管我需要弄清楚如何开始。有人可以指出我正确的方向吗?我对编程很陌生,可以使用我能得到的所有帮助。非常感谢!

#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 PrintReportHeadings
void InitializeAccumulators(int * empCount,float * pr, float * reg,float * ovt,float * gp,float * fedt,float * stt,float * def,float * sst,float * np); //3.2 InitializeAccumulators
void InputEmployeeData(char * firstname,char * lastname,float * hours,float * payrate,float * defr); //3.3 InputEmployeeData
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 *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,float *avgfedt,float *avgreg,float *avgovt,float *avggp,
                                float *avgsst,float *avgstt,float *avgnp,float *avgdef);//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 PrintSummaryReport
extern void SortByLastName();//3.8 SortByLastName                        

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 PrintReportHeadings           
     InitializeAccumulators(&empCount,&pr,&reg,&ovt,&gp,&fedt,&stt,&def,&sst,&np); //3.2 InitializeAccumulators

     do
       {
         InputEmployeeData(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(&empCount,&pr,&r.payrate,&reg,&reghours,&ovt,&ovthours,&gp,&r.gross,&fedt,&ft,&stt,&st,&sst,&ssit,
                                 &def,&r.defr,&np,&r.net,&avgpr,&avgfedt,&avgreg,&avgovt,&avggp,
                                &avgsst,&avgstt,&avgnp,&avgdef);//3.6 AddDetailToAccumulator

              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("\nAverages %15.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",avgpr,avgreg,avggp,avgfedt,avgsst,avgnp); 
       printf("%32.2f%18.2f%8.2f\n",avgovt,avgstt,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 InitializeAccumulators(int * empCount,float * pr, float * reg,float * ovt,float * gp,float * fedt,float * stt,float * def,float * sst,float * np) //3.2 InitializeAccumulators
{
     *empCount = 0;// count initializations 
     *pr = *reg = *ovt = *gp = *fedt = *stt = *def = *sst = *np = 0;
}


void InputEmployeeData(char * firstname,char * lastname, 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,float *avgfedt,float *avgreg,float *avgovt,float *avggp,
                                float *avgsst,float *avgstt,float *avgnp,float *avgdef)//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;
         *avgfedt = *fedt / *empCount; 
         *avgreg = *reg / *empCount;
         *avgovt = *ovt / *empCount;
         *avggp = *gp / *empCount;
         *avgstt = *stt / *empCount;
         *avgsst = *sst / *empCount;
         *avgnp = *np / *empCount;
         *avgdef = *def / *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

1 回答 1

3

我建议您使用“结构”将数据组合在一起,使其更具可读性。

例如

struct person { 
  char firstname[MAXLENNAME];  
  char lastname[MAXLENNAME]; 
... 
};

然后当你想使用排序时qsort,你创建一个函数来比较 struct person 记录。

于 2012-07-31T04:49:16.843 回答