Initializcia
This commit is contained in:
parent
13efda128e
commit
746d776383
28
cv4/Makefile
28
cv4/Makefile
@ -1,13 +1,29 @@
|
||||
# Compiler and flags
|
||||
CC = gcc
|
||||
CFLAGS = -std=c99 -g -Wall
|
||||
|
||||
all: train
|
||||
# Source files and object files
|
||||
SRCS = main.c a_train.c
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
# Target executable
|
||||
TARGET = main
|
||||
|
||||
# Default rule to build the executable
|
||||
all: $(TARGET)
|
||||
|
||||
# Rule to link object files and create the executable
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $(OBJS) -o $(TARGET)
|
||||
|
||||
# Rule to compile .c files into .o files
|
||||
%.o: %.c
|
||||
gcc -c -o $@ $< $(CFLAGS)
|
||||
|
||||
train: main.o a_train.o
|
||||
gcc main.o a_train.o -o train
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# Clean up generated files
|
||||
clean:
|
||||
rm *.o train
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
|
||||
# Phony targets (targets that are not actual files)
|
||||
.PHONY: all clean
|
||||
|
||||
|
@ -4,19 +4,18 @@
|
||||
#include <string.h>
|
||||
|
||||
struct car* add_car(struct car* first, const char* target) {
|
||||
struct car* last = first;
|
||||
struct car* newcar = calloc(1, sizeof(struct car));
|
||||
strcpy(newcar->value, target);
|
||||
newcar->next = NULL;
|
||||
|
||||
if (last != NULL) {
|
||||
struct car* current = last;
|
||||
if (first == NULL) {
|
||||
first = newcar;
|
||||
} else {
|
||||
struct car* current = first;
|
||||
while (current->next != NULL) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = newcar;
|
||||
} else {
|
||||
last = newcar;
|
||||
return last;
|
||||
}
|
||||
|
||||
return first;
|
||||
@ -25,7 +24,7 @@ struct car* add_car(struct car* first, const char* target) {
|
||||
void print_train(struct car* first) {
|
||||
if(first != NULL){
|
||||
struct car* current = first;
|
||||
while(current->next != NULL){
|
||||
while(current != NULL){
|
||||
printf("%s\n", current->value);
|
||||
current = current->next;
|
||||
}
|
||||
@ -41,7 +40,6 @@ void cancel_train(struct car* first) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct car* clear_train(struct car* first, const char* target) {
|
||||
if (first == NULL) {
|
||||
return NULL;
|
||||
|
82
cv4/main.c
82
cv4/main.c
@ -1,17 +1,85 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Testovaci subor pre vlak
|
||||
int main() {
|
||||
struct car* train = NULL;
|
||||
train = add_car(train,"Presov");
|
||||
train = add_car(train,"Bratislava");
|
||||
train = add_car(train,"Levoca");
|
||||
train = add_car(train,"Spiska Nova Ves");
|
||||
print_train(train);
|
||||
train = clear_train(train,"Levoca");
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user