add cv4
This commit is contained in:
parent
bd21b7fecb
commit
31878a2ae7
13
cv4/Makefile
Normal file
13
cv4/Makefile
Normal 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
|
||||
|
91
cv4/a_train.c
Normal file
91
cv4/a_train.c
Normal file
@ -0,0 +1,91 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct car* add_car(struct car* first,const char* target)
|
||||
{
|
||||
struct car* newcar = calloc(1,sizeof(struct car));
|
||||
strcpy(newcar->value, target);
|
||||
|
||||
if(first == NULL)
|
||||
{
|
||||
return newcar;
|
||||
}
|
||||
|
||||
struct car* this = first;
|
||||
|
||||
while(this->next != NULL)
|
||||
{
|
||||
this = this->next;
|
||||
}
|
||||
|
||||
this->next = newcar;
|
||||
|
||||
return first;
|
||||
}
|
||||
|
||||
void print_train(struct car* first)
|
||||
{
|
||||
if(first == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct car* this = first;
|
||||
|
||||
while(this->next != NULL)
|
||||
{
|
||||
printf("%s\n", this->value);
|
||||
this = this->next;
|
||||
}
|
||||
printf("%s\n", this->value);
|
||||
}
|
||||
|
||||
void cancel_train(struct car* first)
|
||||
{
|
||||
if(first == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct car* this = first;
|
||||
if(this->next != NULL)
|
||||
{
|
||||
cancel_train(this->next);
|
||||
}
|
||||
free(first);
|
||||
}
|
||||
|
||||
|
||||
struct car* clear_train(struct car* first, const char* target)
|
||||
{
|
||||
if(first == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if(first->next == NULL)
|
||||
{
|
||||
if(strcmp(first->value, target))
|
||||
{
|
||||
free(first);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return first;
|
||||
}
|
||||
}
|
||||
struct car* prev = first;
|
||||
while (prev->next->next != NULL)
|
||||
{
|
||||
if(strcmp(prev->value, target))
|
||||
{
|
||||
struct car* third = prev->next->next;
|
||||
free(prev->next);
|
||||
prev->next = third;
|
||||
return first;
|
||||
}
|
||||
prev = prev->next;
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
51
cv4/a_train.h
Normal file
51
cv4/a_train.h
Normal 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
|
32
cv4/main.c
Normal file
32
cv4/main.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char buf[50];
|
||||
struct car* train = NULL;
|
||||
memset(buf, 0, 50);
|
||||
printf("Zadajte zoznam cieľových staníc.\nZoznam zakončite prázdnym riadkom.\n");
|
||||
while (fgets(buf, 50, stdin) != NULL)
|
||||
{
|
||||
if(buf[0] == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
buf[strcspn(buf, "\n")] = '\0';
|
||||
train = add_car(train,buf);
|
||||
}
|
||||
|
||||
print_train(train);
|
||||
|
||||
printf("Zadajte stanicu, ktorá sa má vyradiť:\n");
|
||||
scanf("%s", buf);
|
||||
clear_train(train,buf);
|
||||
|
||||
print_train(train);
|
||||
|
||||
cancel_train(train);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user