-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_file.c
163 lines (136 loc) · 4.43 KB
/
root_file.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the structure for each travel stop
typedef struct Stop {
char location[50];
char date[15];
char description[100];
struct Stop *next;
} Stop;
// Function prototypes
Stop* addStop(Stop *head, char location[], char date[], char description[]);
Stop* deleteStop(Stop *head, char location[]);
void viewItinerary(Stop *head);
void clearScreen();
void pause();
int main() {
Stop *head = NULL; // Start with an empty itinerary
int choice;
char location[50], date[15], description[100];
// Main menu loop
while (1) {
clearScreen();
printf("\n========= TRAVEL ITINERARY PLANNER =========\n");
printf("1. Add a New Stop\n");
printf("2. Delete a Stop\n");
printf("3. View Itinerary\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear newline from input buffer
switch (choice) {
case 1:
// Adding a new stop
printf("\nEnter Location: ");
fgets(location, 50, stdin);
location[strcspn(location, "\n")] = 0; // Removes newline
printf("Enter Date (e.g., 31-10-2024): ");
fgets(date, 15, stdin);
date[strcspn(date, "\n")] = 0;
printf("Enter Description: ");
fgets(description, 100, stdin);
description[strcspn(description, "\n")] = 0;
head = addStop(head, location, date, description);
printf("\nStop added successfully!\n");
pause();
break;
case 2:
// Deleting a stop
printf("\nEnter Location to Delete: ");
fgets(location, 50, stdin);
location[strcspn(location, "\n")] = 0;
head = deleteStop(head, location);
printf("\nStop deleted (if it existed).\n");
pause();
break;
case 3:
// Viewing the itinerary
viewItinerary(head);
pause();
break;
case 4:
// Exiting the program
printf("Thank you for using the Itinerary Planner. Safe travels!\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
pause();
}
}
return 0;
}
// Function to add a new stop to the itinerary
Stop* addStop(Stop *head, char location[], char date[], char description[]) {
Stop *newStop = (Stop*)malloc(sizeof(Stop));
strcpy(newStop->location, location);
strcpy(newStop->date, date);
strcpy(newStop->description, description);
newStop->next = NULL;
if (head == NULL) {
return newStop;
}
Stop *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newStop;
return head;
}
// Function to delete a stop from the itinerary based on location
Stop* deleteStop(Stop *head, char location[]) {
if (head == NULL) return NULL;
Stop *temp = head, *prev = NULL;
// If the stop to be deleted is the head
if (strcmp(temp->location, location) == 0) {
head = temp->next;
free(temp);
return head;
}
// Traverse the list to find the stop
while (temp != NULL && strcmp(temp->location, location) != 0) {
prev = temp;
temp = temp->next;
}
// If the stop is not found
if (temp == NULL) return head;
// Remove the stop from the list
prev->next = temp->next;
free(temp);
return head;
}
// Function to display the itinerary
void viewItinerary(Stop *head) {
if (head == NULL) {
printf("The itinerary is empty.\n");
return;
}
Stop *temp = head;
while (temp != NULL) {
printf("Location: %s\nDate: %s\nDescription: %s\n\n", temp->location, temp->date, temp->description);
temp = temp->next;
}
}
// Function to clear the screen (platform dependent)
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// Function to pause the program and wait for user input
void pause() {
printf("Press Enter to continue...");
getchar();
}