The fourth one

This commit is contained in:
Anton Dolozin 2025-10-28 17:35:25 +01:00
parent 8daf043277
commit aa583c302e
8 changed files with 174 additions and 0 deletions

13
du4/Makefile Normal file
View File

@ -0,0 +1,13 @@
CFLAGS= -std=c99 -g -Wall
all: train
%.o: %.c
gcc -c -o $@ $< $(CFLAGS)
train: main.o a_train.o
gcc main.o a_train.o -o train
clean:
rm *.o train

74
du4/a_train.c Normal file
View File

@ -0,0 +1,74 @@
#include "a_train.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct car* add_car(struct car* first,const char* target) {
if(first == NULL){
first = calloc(1, sizeof(struct car));
strcpy(first->value, target);
return first;
}
struct car* temp = first;
while(temp->next){
temp = temp->next;
}
struct car* newCar = (struct car*)calloc(1, sizeof(struct car));
strcpy(newCar->value, target);
temp->next = newCar;
return first;
}
void print_train(struct car* first) {
struct car* temp = first;
while(temp){
printf("%s\n", temp->value);
temp = temp->next;
}
}
void cancel_train(struct car* first) {
if(first == NULL){
return;
}
cancel_train(first->next);
free(first);
}
struct car* clear_train(struct car* first, const char* target) {
if(first == NULL){
return NULL;
}
if (first->next == NULL && strcmp(first->value, target) == 0)
{
free(first);
return NULL;
}
else if(first->next == NULL && strcmp(first->value, target) != 0){
return first;
}
struct car* temp = first;
struct car* next = first->next;
while (next->next)
{if(strcmp(next->value, target) == 0){
temp->next = next->next;
free(next);
next = temp->next;
}
else{
temp = next;
next = next->next;}
}
return first;
}

51
du4/a_train.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef TRAIN_H
#define TRAIN_H
#define SIZE 100
/**
* Jeden vozen vlaku
*/
struct car {
/**
* Nazov cielovej stanice
*/
char value[SIZE];
/**
* Smenik na dalsi vozen
*/
struct car* next;
};
/**
* Prida vozen na koniec vlaku.
*
* @arg nazov cielovej stanice, ktory sa ma priradit novemu voznu.
* @return smernik na zaciatok vlaku.
*/
struct car* add_car(struct car* first,const char* target);
/**
* Vypise vsetky vozne vo vlaku
*
* @arg smernik na prvy vozen
*/
void print_train(struct car* first);
/**
* Zrusenie vsetkych voznov vo vlaku.
* @arg smernik na prvy vozen
*/
void cancel_train(struct car* first);
/**
* Vyradenie vsetkych voznov, ktorych cielova stanica je target
*
* @arg smernik na prvy vozen
* @arg cielova stanica, ktora sa ma vyradit z vlaku.
* @return smernik na novy prvy vozen
*
*/
struct car* clear_train(struct car* first,const char* target);
#endif // TRAIN_H

BIN
du4/a_train.o Normal file

Binary file not shown.

1
du4/input.txt Normal file
View File

@ -0,0 +1 @@
Bratislava

35
du4/main.c Normal file
View File

@ -0,0 +1,35 @@
#include "a_train.h"
#include <stdio.h>
#include <ctype.h>
// Testovaci subor pre vlak
int main(){
char buff[1000];
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);
clear_train(train,"Levoca");
print_train(train);
printf("Zadajte zoznam cieľových staníc a počet cestujúcich.\n");
printf("Zoznam zakončite prázdnym riadkom.\n");
while (fgets(buff, SIZE, stdin))
{
add_car(train, buff);
}
print_train(train);
printf("Zadajte stanicu, ktorá sa má vyradiť:\n");
char buff2[SIZE];
fgets(buff2, SIZE, stdin);
printf("%s\n", buff2);
printf("Výsledný vlak bez stanice %s bude:\n", buff2);
clear_train(train, buff2);
print_train(train);
return 0;
}

BIN
du4/main.o Normal file

Binary file not shown.

BIN
du4/train Executable file

Binary file not shown.