Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <Windows.h>
- struct CR{
- int year;
- char make[20];
- char model[40];
- char type[30];
- double engineSize;
- char transmission[4];
- char fuelType;
- double city;
- double hwy;
- double fuel;
- double co2;
- };
- struct Queries{
- char queryType;
- char make[20];
- char model[40];
- char type[30];
- double engineSize;
- char transmission[4];
- char fuelType;
- };
- int LoadCR(struct CR[], FILE *);
- bool GetQuery(FILE *, struct CR[], int);
- void Compare(struct Queries, struct CR[], int, FILE *);
- void Print(bool, bool, struct Queries, int, double[], FILE *);
- int AddData(double[], struct CR[], int);
- void AverageVC(double[], int);
- void ResetVC(double[], int *);
- int main(){
- struct CR CRArray[14500] = { 0 };
- int numRecords = 0;
- FILE *records = fopen("fuelconsumption.csv", "rt");
- printf("\nLoading records...");
- numRecords = LoadCR(CRArray, records);
- fclose(records);
- if (numRecords == 0){
- printf("\nError: File fuelconsumption.csv is empty\n");
- getchar();
- return 0;
- }
- else{
- printf("\nRecords loaded successfully!\n\n");
- }
- FILE *queries = fopen("fullqueries.txt", "rt");
- printf("\nWriting report.txt...");
- if (GetQuery(queries, CRArray, numRecords) == false){
- printf("\nFile fullqueries.txt is corrupt\n");
- getchar();
- return 0;
- }
- else{
- printf("\nreport.txt written successfully!\n\n");
- }
- fclose(queries);
- getchar();
- return 0;
- }
- int LoadCR(struct CR CRArray[], FILE *records){
- int ctr = 0;
- while (fscanf(records, "%d,%[^,],%[^,],%[^,],%lf,%[^,],%c,%lf,%lf,%lf,%lf\n", &CRArray[ctr].year, CRArray[ctr].make, CRArray[ctr].model, CRArray[ctr].type,
- &CRArray[ctr].engineSize, CRArray[ctr].transmission, &CRArray[ctr].fuelType, &CRArray[ctr].city, &CRArray[ctr].hwy, &CRArray[ctr].fuel, &CRArray[ctr].co2) != EOF) ctr++;
- return ctr;
- }
- bool GetQuery(FILE *queries, struct CR CRArray[], int numRecords){
- struct Queries currQuery;
- FILE *report = fopen("report.txt", "wt");
- while (fscanf(queries, "%c:", &currQuery.queryType) != EOF){
- if (currQuery.queryType == 'S'){
- fscanf(queries, "%[^;];%[^;];%[^;];%lf\n", currQuery.make, currQuery.model, currQuery.transmission, &currQuery.engineSize);
- }
- else if ((currQuery.queryType) == 'C'){
- fscanf(queries, "%[^;];%[^;];%c\n", currQuery.type, currQuery.transmission, &currQuery.fuelType);
- }
- else{
- return false;
- }
- Compare(currQuery, CRArray, numRecords, report);
- }
- fclose(report);
- return true;
- }
- void Compare(struct Queries currQuery, struct CR CRArray[], int numRecords, FILE *report){
- int currElement, year = 2000, vcCtr = 0;
- double vcValues[4];
- bool newQuery = true;
- bool content = false;
- for (currElement = 0; currElement <= numRecords; currElement++){
- if (currQuery.queryType == 'S'){
- if (strcmp(CRArray[currElement].make, currQuery.make) == 0){
- if (strcmp(CRArray[currElement].model, currQuery.model) == 0){
- if (strcmp(CRArray[currElement].transmission, currQuery.transmission) == 0){
- if (CRArray[currElement].engineSize == currQuery.engineSize){
- content = true;
- AddData(vcValues, CRArray, currElement);
- vcCtr = 1;
- }
- }
- }
- }
- }
- else{
- if (strcmp(CRArray[currElement].type, currQuery.type) == 0){
- if (strcmp(CRArray[currElement].transmission, currQuery.transmission) == 0){
- if (CRArray[currElement].fuelType == currQuery.fuelType){
- content = true;
- vcCtr += AddData(vcValues, CRArray, currElement);
- }
- }
- }
- }
- if (CRArray[currElement].year != CRArray[currElement + 1].year){
- AverageVC(vcValues, vcCtr);
- Print(newQuery, content, currQuery, year, vcValues, report);
- year++;
- newQuery = false;
- content = false;
- ResetVC(vcValues, &vcCtr);
- }
- }
- }
- void Print(bool newQuery, bool content, struct Queries currQuery, int year, double vcValues[], FILE *report){
- if (newQuery){
- fprintf(report, "\n********************************************************************************\n\n");
- if (currQuery.queryType == 'S'){
- fprintf(report, "Specific Search: %s - %s - %s - %.1f\n\n", currQuery.make, currQuery.model, currQuery.transmission, currQuery.engineSize);
- }
- else{
- fprintf(report, "Vehicle Class Search: %s - %s - %c\n\n", currQuery.type, currQuery.transmission, currQuery.fuelType);
- }
- fprintf(report, " Year | city (L/100km) | highway (L/100km) | fuel consumption/year | CO2 / year \n");
- fprintf(report, "--------------------------------------------------------------------------------\n");
- }
- if (!content){
- fprintf(report, " %d | N/A | N/A | N/A | N/A \n", year);
- }
- else{
- fprintf(report, " %d |%15.1lf |%18.1lf |%22.0lf |%11.0lf \n", year, vcValues[0], vcValues[1], vcValues[2], vcValues[3]);
- }
- }
- int AddData(double vcValues[], struct CR CRArray[], int currElement){
- vcValues[0] += CRArray[currElement].city;
- vcValues[1] += CRArray[currElement].hwy;
- vcValues[2] += CRArray[currElement].fuel;
- vcValues[3] += CRArray[currElement].co2;
- return 1;
- }
- void AverageVC(double vcValues[], int vcCtr){
- vcValues[0] /= vcCtr;
- vcValues[1] /= vcCtr;
- vcValues[2] /= vcCtr;
- vcValues[3] /= vcCtr;
- }
- void ResetVC(double vcValues[], int *vcCtr){
- vcValues[0] = 0;
- vcValues[1] = 0;
- vcValues[2] = 0;
- vcValues[3] = 0;
- *vcCtr = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement