Advertisement
Guest User

Untitled

a guest
Nov 28th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. # include <stdio.h>
  2. # include "functions.h"
  3. # include <string.h>
  4.  
  5. void main()
  6. {
  7. int eof,ctr=0,index;
  8. int totalkm=0,CarNo=0;
  9. int carkm[10];
  10. int nextRecord;
  11. char PrevRegistration[10][30]; //Assuming a max of 10 cars
  12. int EmployeeKilos[20][100]; // Assuming a max of 20 employees, and each will only do a max of 15 trips a month.
  13. int maxtrips = 100; // // If necessary change the maxtrips and the 15 in the line above.
  14. float average;
  15. FILE * logbookfile;
  16. logbookfile = fopen("logbook.txt","r");
  17. FILE * employeefile;
  18. employeefile = fopen("Employee.txt","rb+");
  19. FILE * report;
  20. report = fopen("report.txt","w");
  21. FILE * table;
  22. table = fopen("table.txt","w");
  23.  
  24. struct Employee employee;
  25. struct Employee employeenames[20];
  26. struct Log log;
  27.  
  28. // Read employees into array for finding the names (part1), and mainly for part 3
  29. eof = fread(&employeenames[ctr], sizeof(employeenames[ctr]), 1, employeefile);
  30. nextRecord = employeenames[ctr].Record -1;
  31.  
  32. while(eof != 0)
  33. {
  34. fseek(employeefile,nextRecord * sizeof(employeenames[ctr]),SEEK_SET);
  35. eof = fread(&employeenames[ctr], sizeof(employeenames[ctr]), 1, employeefile);
  36. eof = fread(&nextRecord, sizeof(int), 1, employeefile);
  37. nextRecord = nextRecord - 1;
  38. ctr++;
  39. }
  40.  
  41. for(int x=0;x<ctr-1;x++)
  42. {
  43. for(int y=0;y<maxtrips;y++)
  44. {
  45. EmployeeKilos[x][y] = -9999; // Set all the kilos to -9999 to signify emptiness.
  46. }
  47. }
  48.  
  49. // Part 1
  50. fscanf(logbookfile,"%[^,],%d,%d/%d/%d,%d%*c",log.RegistrationNumber,&log.Record,&log.Day,&log.Month,&log.Year,&log.Kilometers);
  51.  
  52. fprintf(report,"Wiz Bang Manufacturing - Monthly Car Usage Report\n");
  53.  
  54. while(!feof(logbookfile))
  55. {
  56. strcpy(PrevRegistration[CarNo],log.RegistrationNumber);
  57. carkm[CarNo] = 0;
  58.  
  59. fprintf(report,"\n Car Registration %s\n\n",log.RegistrationNumber);
  60.  
  61. fprintf(report, " Emp Num Employee Name Date Kms\n");
  62. while(strcmp(PrevRegistration[CarNo],log.RegistrationNumber) == 0 && !feof(logbookfile))
  63. {
  64. carkm[CarNo]+=log.Kilometers;
  65.  
  66. index = 0;
  67. while (index < 20 && employeenames[index].EmployeeNumber != log.Record) // Searching for the right employee for the name
  68. {
  69. index++;
  70. }
  71.  
  72. 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);
  73.  
  74. //Part 3 - If the employeenumber from logbook matches the emp number from the array, find the first non 9999 value (in kilos array)
  75. for(int x=0;x<ctr-1;x++) // and set it to the log kilometers
  76. {
  77. if(employeenames[x].EmployeeNumber == log.Record)
  78. {
  79. for(int y=0;y<maxtrips;y++)
  80. {
  81. if(EmployeeKilos[x][y] == -9999)
  82. {
  83. EmployeeKilos[x][y] = log.Kilometers;
  84. break;
  85. }
  86. }
  87. }
  88. }
  89.  
  90. fscanf(logbookfile,"%[^,],%d,%d/%d/%d,%d%*c",log.RegistrationNumber,&log.Record,&log.Day,&log.Month,&log.Year,&log.Kilometers);
  91. }
  92. fprintf(report,"\n\tTotal Kms for Registration %-5s was %d\n",PrevRegistration[CarNo],carkm[CarNo]);
  93. fprintf(report,"--------------------------------------------------\n");
  94. totalkm+=carkm[CarNo];
  95. CarNo++;
  96. }
  97. average = totalkm / CarNo;
  98.  
  99. fprintf(report,"\n\n Number of Kms Travelled this Month was %d",totalkm);
  100. fprintf(report,"\n Average Kms per car was %.2f",average);
  101.  
  102.  
  103. printf(" Screen Report - Total Kms per Car\n\n"); // start producing screen report
  104.  
  105. for(int x=0;x<CarNo;x++)
  106. {
  107. printf("%10s Total Kms for Month was %d\n",PrevRegistration[x],carkm[x]);
  108. } // finish producing screen report
  109.  
  110.  
  111. fprintf(table,"\t Kms Travelled by each Employee\n"); // Start producing Table.txt
  112. fprintf(table,"\t (First four trips only)\n");
  113. fprintf(table,"\nName \t\t\t\t\t Trip 1 Trip 2 Trip 3 Trip 4 \n\n");
  114.  
  115. for(int x=0;x<ctr-1;x++)
  116. {
  117. fprintf(table,"\n\n%-8s\t ",employeenames[x].EmployeeName);
  118. for(int y=0;y<4;y++) // y <4 ensures that a total of 4 trips are printed
  119. {
  120. if(EmployeeKilos[x][y] != -9999)
  121. {
  122. fprintf(table,"%10d",EmployeeKilos[x][y]);
  123. }
  124. }
  125. } // finish producing Table.txt
  126. scanf("%*c");
  127. fclose(logbookfile);
  128. fclose(employeefile);
  129. fclose(report);
  130. fclose(table);
  131. }
  132.  
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement