usaa24/cv4/main.c

86 lines
2.8 KiB
C
Raw Normal View History

2024-10-15 12:34:20 +00:00
#include "a_train.h"
#include <stdio.h>
2024-11-18 13:43:38 +00:00
#include <stdlib.h>
#include <string.h>
2024-10-15 12:34:20 +00:00
2024-11-18 13:43:38 +00:00
int main() {
2024-10-15 12:34:20 +00:00
struct car* train = NULL;
2024-11-18 13:43:38 +00:00
int choice;
char station[100];
while (1) {
printf("\nMenu:\n");
printf("1. Add station\n");
printf("2. Remove a station\n");
printf("3. Print route\n");
printf("4. Cancel train\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch(choice) {
case 1:
while (1) {
printf("Enter the station name to add (or press Enter to stop): ");
fgets(station, sizeof(station), stdin);
station[strcspn(station, "\n")] = '\0'; // Remove newline character
if (station[0] == '\0') {
break; // Exit the loop if input is empty
}
train = add_car(train, station);
printf("Station '%s' added to the train.\n", station);
}
break;
case 2:
while (1) {
printf("Enter the station name to clear (or press Enter to stop): ");
fgets(station, sizeof(station), stdin);
station[strcspn(station, "\n")] = '\0'; // Remove newline character
if (station[0] == '\0') {
break; // Exit the loop if input is empty
}
struct car* temp = train;
int found = 0;
while (temp != NULL) {
if (strcmp(temp->value, station) == 0) {
found = 1;
break;
}
temp = temp->next;
}
if (found) {
train = clear_train(train, station);
printf("Station '%s' deleted from the train.\n", station);
} else {
printf("Station '%s' does not exist in the train.\n", station);
}
}
break;
case 3:
printf("\nRoute List:\n");
print_train(train);
break;
case 4:
printf("\nCanceling the train...\n");
cancel_train(train);
train = NULL;
break;
case 5:
printf("Exiting program...\n");
cancel_train(train);
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
2024-10-15 12:34:20 +00:00
return 0;
}
2024-11-18 13:43:38 +00:00