Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- # include "functions.h"
- # include <string.h>
- void main()
- {
- int eof,ctr=0,index;
- int totalkm=0,CarNo=0;
- int carkm[10];
- int nextRecord;
- char PrevRegistration[10][30]; //Assuming a max of 10 cars
- int EmployeeKilos[20][100]; // Assuming a max of 20 employees, and each will only do a max of 15 trips a month.
- int maxtrips = 100; // // If necessary change the maxtrips and the 15 in the line above.
- float average;
- FILE * logbookfile;
- logbookfile = fopen("logbook.txt","r");
- FILE * employeefile;
- employeefile = fopen("Employee.txt","rb+");
- FILE * report;
- report = fopen("report.txt","w");
- FILE * table;
- table = fopen("table.txt","w");
- struct Employee employee;
- struct Employee employeenames[20];
- struct Log log;
- // Read employees into array for finding the names (part1), and mainly for part 3
- eof = fread(&employeenames[ctr], sizeof(employeenames[ctr]), 1, employeefile);
- nextRecord = employeenames[ctr].Record -1;
- while(eof != 0)
- {
- fseek(employeefile,nextRecord * sizeof(employeenames[ctr]),SEEK_SET);
- eof = fread(&employeenames[ctr], sizeof(employeenames[ctr]), 1, employeefile);
- eof = fread(&nextRecord, sizeof(int), 1, employeefile);
- nextRecord = nextRecord - 1;
- ctr++;
- }
- for(int x=0;x<ctr-1;x++)
- {
- for(int y=0;y<maxtrips;y++)
- {
- EmployeeKilos[x][y] = -9999; // Set all the kilos to -9999 to signify emptiness.
- }
- }
- // Part 1
- fscanf(logbookfile,"%[^,],%d,%d/%d/%d,%d%*c",log.RegistrationNumber,&log.Record,&log.Day,&log.Month,&log.Year,&log.Kilometers);
- fprintf(report,"Wiz Bang Manufacturing - Monthly Car Usage Report\n");
- while(!feof(logbookfile))
- {
- strcpy(PrevRegistration[CarNo],log.RegistrationNumber);
- carkm[CarNo] = 0;
- fprintf(report,"\n Car Registration %s\n\n",log.RegistrationNumber);
- fprintf(report, " Emp Num Employee Name Date Kms\n");
- while(strcmp(PrevRegistration[CarNo],log.RegistrationNumber) == 0 && !feof(logbookfile))
- {
- carkm[CarNo]+=log.Kilometers;
- index = 0;
- while (index < 20 && employeenames[index].EmployeeNumber != log.Record) // Searching for the right employee for the name
- {
- index++;
- }
- fprintf(report,"%8d\t\t%8s\t\t %d/%d/%d\t%8d\n",log.Record,employeenames[index].EmployeeName,log.Day,log.Month,log.Year,log.Kilometers);
- //Part 3 - If the employeenumber from logbook matches the emp number from the array, find the first non 9999 value (in kilos array)
- for(int x=0;x<ctr-1;x++) // and set it to the log kilometers
- {
- if(employeenames[x].EmployeeNumber == log.Record)
- {
- for(int y=0;y<maxtrips;y++)
- {
- if(EmployeeKilos[x][y] == -9999)
- {
- EmployeeKilos[x][y] = log.Kilometers;
- break;
- }
- }
- }
- }
- fscanf(logbookfile,"%[^,],%d,%d/%d/%d,%d%*c",log.RegistrationNumber,&log.Record,&log.Day,&log.Month,&log.Year,&log.Kilometers);
- }
- fprintf(report,"\n\tTotal Kms for Registration %-5s was %d\n",PrevRegistration[CarNo],carkm[CarNo]);
- fprintf(report,"--------------------------------------------------\n");
- totalkm+=carkm[CarNo];
- CarNo++;
- }
- average = totalkm / CarNo;
- fprintf(report,"\n\n Number of Kms Travelled this Month was %d",totalkm);
- fprintf(report,"\n Average Kms per car was %.2f",average);
- printf(" Screen Report - Total Kms per Car\n\n"); // start producing screen report
- for(int x=0;x<CarNo;x++)
- {
- printf("%10s Total Kms for Month was %d\n",PrevRegistration[x],carkm[x]);
- } // finish producing screen report
- fprintf(table,"\t Kms Travelled by each Employee\n"); // Start producing Table.txt
- fprintf(table,"\t (First four trips only)\n");
- fprintf(table,"\nName \t\t\t\t\t Trip 1 Trip 2 Trip 3 Trip 4 \n\n");
- for(int x=0;x<ctr-1;x++)
- {
- fprintf(table,"\n\n%-8s\t ",employeenames[x].EmployeeName);
- for(int y=0;y<4;y++) // y <4 ensures that a total of 4 trips are printed
- {
- if(EmployeeKilos[x][y] != -9999)
- {
- fprintf(table,"%10d",EmployeeKilos[x][y]);
- }
- }
- } // finish producing Table.txt
- scanf("%*c");
- fclose(logbookfile);
- fclose(employeefile);
- fclose(report);
- fclose(table);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement