Initializcia
This commit is contained in:
parent
13efda128e
commit
746d776383
30
cv4/Makefile
30
cv4/Makefile
@ -1,13 +1,29 @@
|
|||||||
CFLAGS= -std=c99 -g -Wall
|
# 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
|
%.o: %.c
|
||||||
gcc -c -o $@ $< $(CFLAGS)
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
train: main.o a_train.o
|
|
||||||
gcc main.o a_train.o -o train
|
|
||||||
|
|
||||||
|
# Clean up generated files
|
||||||
clean:
|
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>
|
#include <string.h>
|
||||||
|
|
||||||
struct car* add_car(struct car* first, const char* target) {
|
struct car* add_car(struct car* first, const char* target) {
|
||||||
struct car* last = first;
|
|
||||||
struct car* newcar = calloc(1, sizeof(struct car));
|
struct car* newcar = calloc(1, sizeof(struct car));
|
||||||
strcpy(newcar->value, target);
|
strcpy(newcar->value, target);
|
||||||
|
newcar->next = NULL;
|
||||||
|
|
||||||
if (last != NULL) {
|
if (first == NULL) {
|
||||||
struct car* current = last;
|
first = newcar;
|
||||||
|
} else {
|
||||||
|
struct car* current = first;
|
||||||
while (current->next != NULL) {
|
while (current->next != NULL) {
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
current->next = newcar;
|
current->next = newcar;
|
||||||
} else {
|
|
||||||
last = newcar;
|
|
||||||
return last;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return first;
|
return first;
|
||||||
@ -25,7 +24,7 @@ struct car* add_car(struct car* first, const char* target) {
|
|||||||
void print_train(struct car* first) {
|
void print_train(struct car* first) {
|
||||||
if(first != NULL){
|
if(first != NULL){
|
||||||
struct car* current = first;
|
struct car* current = first;
|
||||||
while(current->next != NULL){
|
while(current != NULL){
|
||||||
printf("%s\n", current->value);
|
printf("%s\n", current->value);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
@ -41,7 +40,6 @@ void cancel_train(struct car* first) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct car* clear_train(struct car* first, const char* target) {
|
struct car* clear_train(struct car* first, const char* target) {
|
||||||
if (first == NULL) {
|
if (first == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
88
cv4/main.c
88
cv4/main.c
@ -1,17 +1,85 @@
|
|||||||
#include "a_train.h"
|
#include "a_train.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Testovaci subor pre vlak
|
int main() {
|
||||||
int main(){
|
|
||||||
struct car* train = NULL;
|
struct car* train = NULL;
|
||||||
train = add_car(train,"Presov");
|
int choice;
|
||||||
train = add_car(train,"Bratislava");
|
char station[100];
|
||||||
train = add_car(train,"Levoca");
|
|
||||||
train = add_car(train,"Spiska Nova Ves");
|
while (1) {
|
||||||
print_train(train);
|
printf("\nMenu:\n");
|
||||||
train = clear_train(train,"Levoca");
|
printf("1. Add station\n");
|
||||||
print_train(train);
|
printf("2. Remove a station\n");
|
||||||
cancel_train(train);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user