1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#ifndef DEBUG #define DEBUG 1 #endif // DEBUG #ifndef CARS #define CARS #define LEN_MODIFIER 8 #define CAR_COUNT 3 #define PEOPLE_COUNT 10 typedef struct vehicle { char make[3 * LEN_MODIFIER]; char model[3 * LEN_MODIFIER]; } vehicle; typedef struct { char owner[3 * LEN_MODIFIER]; char regNum[LEN_MODIFIER]; char regCity[3 * LEN_MODIFIER]; vehicle *car; } car_registry_entry; void InitStructMembers(vehicle *cars, car_registry_entry *people); void AssignCars(vehicle *cars, car_registry_entry *people); vehicle CreateVehicle(char *make, char *model); car_registry_entry CreateEntry(char *name, char *reg, char *city); void PrintCarOwners(car_registry_entry *owners, int count); #endif // CARS |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "dmv.h" int main(void) { vehicle vehicles[CAR_COUNT]; car_registry_entry owners[PEOPLE_COUNT]; srand((unsigned)time(NULL)); InitStructMembers(vehicles, owners); AssignCars(vehicles, owners); PrintCarOwners(owners, PEOPLE_COUNT); return EXIT_SUCCESS; } void InitStructMembers(vehicle *cars, car_registry_entry *people) { cars[0] = CreateVehicle("Bently", "Blower"); cars[1] = CreateVehicle("Audi", "Quattro"); cars[2] = CreateVehicle("Ford", "GT40"); people[0] = CreateEntry("Mati", "ABC 011", "Tallinn"); people[1] = CreateEntry("Meelis", "ABC 011", "Tartu"); people[2] = CreateEntry("Jaanika", "XXD 191", "Tapa"); people[3] = CreateEntry("Viljar", "XKC 020", "Loobu"); people[4] = CreateEntry("Jane", "PLO 113", "Narva"); people[5] = CreateEntry("Kaspar", "TPN 421", "Kuressaare"); people[6] = CreateEntry("Tiina", "MSV 053", "Viljandi"); people[7] = CreateEntry("Vello", "SPP 416", "Saue"); people[8] = CreateEntry("Tarmo", "VTP 751", "Rakvere"); people[9] = CreateEntry("Kaja", "SRS 086", "Vaindloo"); } vehicle CreateVehicle(char *make, char *model) { vehicle newCar; if (DEBUG) printf("CreateVehicle instance init\n"); strcpy(newCar.make, make); strcpy(newCar.model, model); if (DEBUG) printf("CreateVehicle instance complete\n"); return newCar; } car_registry_entry CreateEntry(char *name, char *reg, char *city) { car_registry_entry newPerson; if (DEBUG) printf("CreatePerson instance init\n"); strcpy(newPerson.owner, name); strcpy(newPerson.regNum, reg); strcpy(newPerson.regCity, city); if (DEBUG) printf("CreatePerson instance complete\n"); return newPerson; } void AssignCars(vehicle *cars, car_registry_entry *people) { int i = PEOPLE_COUNT - 1; while (i) { if (DEBUG) printf("Assign instance\n"); (people + i)->car = (cars + (rand() % CAR_COUNT)); i--; } } void PrintCarOwners(car_registry_entry *owners, int count) { int i = count - 1; while (i) { printf("%s %s (reg num %s) belongs to %s and is registered in %s\n", (owners + i)->car->make, (owners + i)->car->model, (owners + i)->regNum, (owners + i)->owner, (owners + i)->regCity); i--; } } |