Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //used to use scanf instead of scanf_s
- #pragma warning(disable: 4996)
- //used for validation of year
- const int P_YEAR = 2020;
- //date-time struct in the correct format
- struct dateTime {
- int mount;
- int day;
- int year;
- };
- //printing date time obj
- void showDateTime(const struct dateTime* toPrint) {
- printf("$Mount: %d\n$Day: %d\n$Year: %d \n", toPrint->mount, toPrint->day, toPrint->year);
- }
- //setting date time to default vals
- void nullDateTime(struct dateTime* toInit) {
- toInit->day = 0;
- toInit->mount = 0;
- toInit->year = 0;
- }
- //getting data from the user about date-time obj
- void initDateTime(struct dateTime* toInit) {
- int mnt = 0;
- int day = 0;
- int year = 0;
- printf("$Enter mount[number between 1-12]: ");
- scanf("%4d", &mnt);
- printf("$Enter day[number between 1-31]: ");
- scanf("%4d", &day);
- printf("$Enter year[number greater than %d]: ", P_YEAR);
- scanf("%4d", &year);
- if (mnt < 13 && year > P_YEAR && day < 31) {
- toInit->mount = mnt; toInit->day = day; toInit->year = year;
- }
- else {
- nullDateTime(toInit);
- return;
- }
- }
- //tourist agency structure
- struct touristAgency {
- char* name;
- char* dest;
- struct dateTime departure;
- struct dateTime arrival;
- };
- //takes care of dynamic memory
- void freeAgency(struct touristAgency* freeMe) {
- free(freeMe->name);
- free(freeMe->dest);
- }
- //initializing t.a. obj. with passed data
- void initAgency(struct touristAgency* toInit, struct dateTime departure, struct dateTime arrival) {
- printf("$Enter name: ");
- char name[1024];
- scanf("%s", &name);
- printf("$Enter destination: ");
- char dest[1024];
- scanf("%s", &dest);
- int lenName = strlen(name);
- if (!lenName)
- return;
- toInit->name = malloc(sizeof(char) * lenName + 1);
- memcpy(toInit->name, name, lenName * sizeof(char));
- toInit->name[lenName] = '\0';
- int lenDest = strlen(dest);
- if (!lenDest) {
- return;
- }
- toInit->dest = malloc(sizeof(char) * lenDest + 1);
- memcpy(toInit->dest, dest, lenDest * sizeof(char));
- toInit->dest[lenDest] = '\0';
- printf("======arrival======\n");
- initDateTime(&toInit->arrival, arrival.mount, arrival.day, arrival.year);
- printf("======departure======\n");
- initDateTime(&toInit->departure, departure.mount, departure.day, departure.year);
- }
- //printing t.a. data
- void showAgency(const struct touristAgency* printMe) {
- printf("$Name: %s\n", printMe->name);
- printf("$Destination: %s\n", printMe->dest);
- showDateTime(&printMe->arrival);
- showDateTime(&printMe->departure);
- }
- //checks if two dates are "equal"
- int equalDates(const struct dateTime* _first, const struct dateTime* _second) {
- return ((_first->day == _second->day) &&
- (_first->mount == _second->mount) &&
- (_first->year == _second->year));
- }
- //linear search
- void search(const struct touristAgency* database, int len) {
- struct dateTime dt;
- int found = 0;
- initDateTime(&dt);
- for (int i = 0; i < len; i++)
- if (equalDates(&database[i].arrival, &dt) == 1) {
- printf("\n======FOUND======\n");
- showAgency(&database[i]);
- found = 1;
- printf("\n");
- }
- if (!found)
- printf("No such list\n");
- }
- //main loop:
- void run() {
- struct touristAgency* data = malloc(20 * sizeof(struct touristAgency));
- char c = '\0';
- struct dateTime arrival;
- struct dateTime departure;
- for (int i = 0; i < 20; i++) {
- printf("$Press\n$a to add\n$s to search\n$v to view\n$x to exit: ");
- scanf("%s", &c);
- switch (c) {
- case'a':
- nullDateTime(&arrival);
- nullDateTime(&departure);
- initAgency(&data[i], departure, arrival);
- break;
- case 'v':
- for (int j = 0; j < i; j++)
- showAgency(&data[j]);
- i--;
- break;
- case 's':
- if (i == 0)
- printf("No data!\n");
- else
- search(data, i);
- i--;
- break;
- case 'x':
- printf("Bye!!!");
- for (int j = 0; j < i; j++)
- freeAgency(&data[j]);
- free(data);
- exit(1);
- break;
- default:
- printf("$invalid comand\n");
- i--;
- break;
- }
- }
- printf("$Maximum of 20 records reached!");
- for (int i = 0; i < 20; i++)
- freeAgency(&data[i]);
- free(data);
- }
- int main() {
- run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement