Advertisement
Guest User

Fuel Consumption

a guest
Apr 15th, 2014
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <Windows.h>
  5.  
  6. struct CR{
  7.     int year;
  8.     char make[20];
  9.     char model[40];
  10.     char type[30];
  11.     double engineSize;
  12.     char transmission[4];
  13.     char fuelType;
  14.     double city;
  15.     double hwy;
  16.     double fuel;
  17.     double co2;
  18. };
  19.  
  20. struct Queries{
  21.     char queryType;
  22.     char make[20];
  23.     char model[40];
  24.     char type[30];
  25.     double engineSize;
  26.     char transmission[4];
  27.     char fuelType;
  28. };
  29.  
  30. int LoadCR(struct CR[], FILE *);
  31. bool GetQuery(FILE *, struct CR[], int);
  32. void Compare(struct Queries, struct CR[], int, FILE *);
  33. void Print(bool, bool, struct Queries, int, double[], FILE *);
  34. int AddData(double[], struct CR[], int);
  35. void AverageVC(double[], int);
  36. void ResetVC(double[], int *);
  37.  
  38. int main(){
  39.     struct CR CRArray[14500] = { 0 };
  40.  
  41.     int numRecords = 0;
  42.  
  43.     FILE *records = fopen("fuelconsumption.csv", "rt");
  44.     printf("\nLoading records...");
  45.     numRecords = LoadCR(CRArray, records);
  46.  
  47.     fclose(records);
  48.  
  49.     if (numRecords == 0){
  50.         printf("\nError: File fuelconsumption.csv is empty\n");
  51.         getchar();
  52.         return 0;
  53.     }
  54.     else{
  55.         printf("\nRecords loaded successfully!\n\n");
  56.     }
  57.  
  58.     FILE *queries = fopen("fullqueries.txt", "rt");
  59.     printf("\nWriting report.txt...");
  60.     if (GetQuery(queries, CRArray, numRecords) == false){
  61.         printf("\nFile fullqueries.txt is corrupt\n");
  62.         getchar();
  63.         return 0;
  64.     }
  65.     else{
  66.         printf("\nreport.txt written successfully!\n\n");
  67.     }
  68.  
  69.     fclose(queries);
  70.  
  71.     getchar();
  72.  
  73.     return 0;
  74. }
  75.  
  76. int LoadCR(struct CR CRArray[], FILE *records){
  77.     int ctr = 0;
  78.  
  79.     while (fscanf(records, "%d,%[^,],%[^,],%[^,],%lf,%[^,],%c,%lf,%lf,%lf,%lf\n", &CRArray[ctr].year, CRArray[ctr].make, CRArray[ctr].model, CRArray[ctr].type,
  80.         &CRArray[ctr].engineSize, CRArray[ctr].transmission, &CRArray[ctr].fuelType, &CRArray[ctr].city, &CRArray[ctr].hwy, &CRArray[ctr].fuel, &CRArray[ctr].co2) != EOF) ctr++;
  81.  
  82.     return ctr;
  83. }
  84.  
  85. bool GetQuery(FILE *queries, struct CR CRArray[], int numRecords){
  86.     struct Queries currQuery;
  87.  
  88.     FILE *report = fopen("report.txt", "wt");
  89.  
  90.     while (fscanf(queries, "%c:", &currQuery.queryType) != EOF){
  91.  
  92.         if (currQuery.queryType == 'S'){
  93.             fscanf(queries, "%[^;];%[^;];%[^;];%lf\n", currQuery.make, currQuery.model, currQuery.transmission, &currQuery.engineSize);
  94.         }
  95.         else if ((currQuery.queryType) == 'C'){
  96.             fscanf(queries, "%[^;];%[^;];%c\n", currQuery.type, currQuery.transmission, &currQuery.fuelType);
  97.         }
  98.         else{
  99.             return false;
  100.         }
  101.  
  102.         Compare(currQuery, CRArray, numRecords, report);
  103.     }
  104.  
  105.     fclose(report);
  106.  
  107.     return true;
  108. }
  109.  
  110. void Compare(struct Queries currQuery, struct CR CRArray[], int numRecords, FILE *report){
  111.     int currElement, year = 2000, vcCtr = 0;
  112.     double vcValues[4];
  113.     bool newQuery = true;
  114.     bool content = false;
  115.  
  116.     for (currElement = 0; currElement <= numRecords; currElement++){
  117.         if (currQuery.queryType == 'S'){
  118.             if (strcmp(CRArray[currElement].make, currQuery.make) == 0){
  119.                 if (strcmp(CRArray[currElement].model, currQuery.model) == 0){
  120.                     if (strcmp(CRArray[currElement].transmission, currQuery.transmission) == 0){
  121.                         if (CRArray[currElement].engineSize == currQuery.engineSize){
  122.                             content = true;
  123.                             AddData(vcValues, CRArray, currElement);
  124.                             vcCtr = 1;
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.         else{
  131.             if (strcmp(CRArray[currElement].type, currQuery.type) == 0){
  132.                 if (strcmp(CRArray[currElement].transmission, currQuery.transmission) == 0){
  133.                     if (CRArray[currElement].fuelType == currQuery.fuelType){
  134.                         content = true;
  135.                         vcCtr += AddData(vcValues, CRArray, currElement);
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.         if (CRArray[currElement].year != CRArray[currElement + 1].year){
  141.             AverageVC(vcValues, vcCtr);
  142.             Print(newQuery, content, currQuery, year, vcValues, report);
  143.             year++;
  144.             newQuery = false;
  145.             content = false;
  146.             ResetVC(vcValues, &vcCtr);
  147.         }
  148.     }
  149. }
  150.  
  151. void Print(bool newQuery, bool content, struct Queries currQuery, int year, double vcValues[], FILE *report){
  152.     if (newQuery){
  153.  
  154.         fprintf(report, "\n********************************************************************************\n\n");
  155.  
  156.         if (currQuery.queryType == 'S'){
  157.             fprintf(report, "Specific Search: %s - %s - %s - %.1f\n\n", currQuery.make, currQuery.model, currQuery.transmission, currQuery.engineSize);
  158.         }
  159.         else{
  160.             fprintf(report, "Vehicle Class Search: %s - %s - %c\n\n", currQuery.type, currQuery.transmission, currQuery.fuelType);
  161.         }
  162.  
  163.         fprintf(report, " Year | city (L/100km) | highway (L/100km) | fuel consumption/year | CO2 / year \n");
  164.         fprintf(report, "--------------------------------------------------------------------------------\n");
  165.     }
  166.  
  167.     if (!content){
  168.         fprintf(report, " %d |            N/A |               N/A |                   N/A |        N/A \n", year);
  169.     }
  170.     else{
  171.         fprintf(report, " %d |%15.1lf |%18.1lf |%22.0lf |%11.0lf \n", year, vcValues[0], vcValues[1], vcValues[2], vcValues[3]);
  172.     }
  173. }
  174.  
  175. int AddData(double vcValues[], struct CR CRArray[], int currElement){
  176.     vcValues[0] += CRArray[currElement].city;
  177.     vcValues[1] += CRArray[currElement].hwy;
  178.     vcValues[2] += CRArray[currElement].fuel;
  179.     vcValues[3] += CRArray[currElement].co2;
  180.  
  181.     return 1;
  182. }
  183.  
  184. void AverageVC(double vcValues[], int vcCtr){
  185.     vcValues[0] /= vcCtr;
  186.     vcValues[1] /= vcCtr;
  187.     vcValues[2] /= vcCtr;
  188.     vcValues[3] /= vcCtr;
  189. }
  190.  
  191. void ResetVC(double vcValues[], int *vcCtr){
  192.     vcValues[0] = 0;
  193.     vcValues[1] = 0;
  194.     vcValues[2] = 0;
  195.     vcValues[3] = 0;
  196.     *vcCtr = 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement