Exercise G

  • Upload
    theo

  • View
    4

  • Download
    0

Embed Size (px)

DESCRIPTION

Exercise G

Citation preview

//Exercise G#include #include typedef struct car car;struct car {long arKykl;time_t xronosEis;};int main(void) {float cph = 0.0f;int choice = -1, carNum = 0, vgikan = 0, j = 0;long temp;car inCar, outCar, lastCar;time_t synXronos = 0;FILE* allCars;if (!(allCars = fopen("data.bin", "w+b"))) {printf("\n\nError on opening file.\n\n");return 1;}printf("How much is the cost per hour? ");scanf(" %f", &cph);while(choice != 3) {printf("Choose one from below (1, 2, 3): ");printf("\n\t1. Entrance of car");printf("\n\t2. Exit of car");printf("\n\t3. End of program\t");scanf(" %d", &choice);while (choice < 1 || choice > 3)scanf(" %d", &choice);switch (choice) {case 1:carNum++;printf("\n\nPlease type the registration number: ");scanf(" %ld", &inCar.arKykl);inCar.xronosEis = time(NULL);fseek(allCars, (vgikan) ? -vgikan*sizeof(car) : 0, SEEK_END);fwrite(&inCar, sizeof(car), 1, allCars);if (vgikan)vgikan--;break;case 2:printf("\n\nPlease type the registration number: ");scanf(" %ld", &temp);fseek(allCars, -(long)sizeof(car), SEEK_END);fread(&lastCar, sizeof(car), 1, allCars);rewind(allCars);for (j = 0; j < carNum; j++) {fread(&outCar, sizeof(car), 1, allCars);if (outCar.arKykl == temp)break;}if (j < carNum) {synXronos = time(NULL) - outCar.xronosEis;printf("\nThe total time you have parked here is %lf hours.\nYou pay %.2lf$.", synXronos/3600.0, synXronos/3600.0 * cph);printf("\n\t\tThank you! :-)\n");fseek(allCars, -(long)sizeof(car), SEEK_CUR);fwrite(&lastCar, sizeof(car), 1, allCars);carNum--;vgikan++;}elseprintf("There is no car with registration number %ld.\n", temp);break;default:rewind(allCars);for (j = 0; j < carNum; j++) {fread(&outCar, sizeof(car), 1, allCars);printf("\nCar %d:\n\tRN: %ld\n\ttime: %lf hours\n", j+1, outCar.arKykl, (time(NULL) - outCar.xronosEis)/3600.0);}break;}}fclose(allCars);return 0;}