du4
This commit is contained in:
parent
5918eae042
commit
c48c12b36f
13
du4/Makefile
Normal file
13
du4/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
|
||||||
|
|
||||||
64
du4/a_train.c
Normal file
64
du4/a_train.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#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));
|
||||||
|
if(!newcar) return first;
|
||||||
|
|
||||||
|
strcpy(newcar->value, target);
|
||||||
|
newcar->next = NULL;
|
||||||
|
|
||||||
|
if(first == NULL){
|
||||||
|
return newcar;
|
||||||
|
}
|
||||||
|
struct car* temp = first;
|
||||||
|
while(temp->next !=NULL){
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
temp->next = newcar;
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_train(struct car* first) {
|
||||||
|
struct car* temp = first;
|
||||||
|
while(temp != NULL) {
|
||||||
|
printf("%s\n",temp->value);
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancel_train(struct car* first) {
|
||||||
|
struct car* temp;
|
||||||
|
while(first != NULL){
|
||||||
|
temp = first -> next;
|
||||||
|
free(first);
|
||||||
|
first = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct car* clear_train(struct car* first, const char* target) {
|
||||||
|
struct car* temp = first;
|
||||||
|
struct car* prev = NULL;
|
||||||
|
|
||||||
|
while(temp != NULL){
|
||||||
|
if(strcmp(temp->value, target)== 0){
|
||||||
|
if(prev == NULL){
|
||||||
|
first= temp->next;
|
||||||
|
free(temp);
|
||||||
|
temp= first;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
prev->next= temp->next;
|
||||||
|
free(temp);
|
||||||
|
temp = prev->next;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
prev= temp;
|
||||||
|
temp =temp->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
51
du4/a_train.h
Normal file
51
du4/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
|
||||||
BIN
du4/a_train.o
Normal file
BIN
du4/a_train.o
Normal file
Binary file not shown.
17
du4/main.c
Normal file
17
du4/main.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "a_train.h"
|
||||||
|
#include <stdio.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);
|
||||||
|
clear_train(train,"Levoca");
|
||||||
|
print_train(train);
|
||||||
|
cancel_train(train);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
du4/main.o
Normal file
BIN
du4/main.o
Normal file
Binary file not shown.
@ -1,73 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define MAX 101
|
|
||||||
|
|
||||||
// funkcia, ktorá vracia zodpovedajúcu zatváraciu zátvorku
|
|
||||||
char expected_close(char open) {
|
|
||||||
switch (open) {
|
|
||||||
case '(': return ')';
|
|
||||||
case '{': return '}';
|
|
||||||
case '[': return ']';
|
|
||||||
case '<': return '>';
|
|
||||||
default: return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
char line[MAX];
|
|
||||||
char stack[MAX];
|
|
||||||
int pos[MAX]; // pozície otvorených zátvoriek
|
|
||||||
int top = -1; // index vrcholu zásobníka
|
|
||||||
|
|
||||||
// načítanie jedného riadku
|
|
||||||
if (fgets(line, MAX, stdin) == NULL)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// odstrániť znak nového riadku, ak je prítomný
|
|
||||||
line[strcspn(line, "\n")] = '\0';
|
|
||||||
|
|
||||||
printf("Read: %s\n", line);
|
|
||||||
|
|
||||||
int ok = 1;
|
|
||||||
int length = strlen(line);
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
char ch = line[i];
|
|
||||||
|
|
||||||
// ak je otváracia zátvorka
|
|
||||||
if (ch == '(' || ch == '{' || ch == '[' || ch == '<') {
|
|
||||||
top++;
|
|
||||||
stack[top] = ch;
|
|
||||||
pos[top] = i + 1; // indexovanie od 1
|
|
||||||
}
|
|
||||||
// ak je zatváracia zátvorka
|
|
||||||
else if (ch == ')' || ch == '}' || ch == ']' || ch == '>') {
|
|
||||||
if (top < 0) {
|
|
||||||
printf("Unexpected closing bracket %c in %d\n", ch, i + 1);
|
|
||||||
ok = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
char last_open = stack[top];
|
|
||||||
char expected = expected_close(last_open);
|
|
||||||
if (ch != expected) {
|
|
||||||
printf("Crossed bracket %c in %d, expected %c\n", ch, i + 1, expected);
|
|
||||||
ok = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
top--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ok && top >= 0) {
|
|
||||||
printf("Missing closing bracket for %c from %d\n", stack[top], pos[top]);
|
|
||||||
ok = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ok)
|
|
||||||
printf("All brackets OK\n");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user