From 746d776383a84d8f3d64f567c68d65a6d44e4f9d Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 18 Nov 2024 14:43:38 +0100 Subject: [PATCH] Initializcia --- cv4/Makefile | 30 +++++++++++++---- cv4/a_train.c | 14 ++++---- cv4/main | Bin 0 -> 21376 bytes cv4/main.c | 88 ++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 107 insertions(+), 25 deletions(-) create mode 100755 cv4/main diff --git a/cv4/Makefile b/cv4/Makefile index 3c2c463..f7793c7 100644 --- a/cv4/Makefile +++ b/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 - 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 diff --git a/cv4/a_train.c b/cv4/a_train.c index c117b1a..f078efd 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -4,19 +4,18 @@ #include 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; diff --git a/cv4/main b/cv4/main new file mode 100755 index 0000000000000000000000000000000000000000..d6be8e2eabe89469298a81828530ec7d038941fd GIT binary patch literal 21376 zcmeHPdvp}ndB3x}Bdy*Nk`MyhjIen)SgVJ{)5t6q!Yd30W7POPtaeA*Ez+*C4?xrp z#R3Nf6GPi1q^60T)UoS0DRt`{65<5cj)~nm#ZB9zq&W>vAe9Pnu$+gQhqu4)K321X z6!+w`e^qzS%y+-%{qA?~+_^hoTKjuUPdWTv1h?>6XBT>hs? zHS;!E-gND7RK{OH8D7@V8|_-&($E{O?~TUNd+PVBTwcGt#g~lxnz;+)mw|)&)Xlec zGMG6hYR^+Cx^w*Z}$OXAp7)6l;Hy1^xKPb+{w9^blcADM6XfNweL}RIL77F(E#zTzC zpdP-Q-Mp#erZ%(Lw<4Qs$>y$~%r^U$F|%X)W;1Lhte$8xWhJ(6ZtIQ5tnI* z^u=S+ZBvL&DMVxbl{pg_dd2%P7tSo(2=aJl z;Vl4V^BKuUh%-L+Rwk1<%yF7vjE}vU#c2XGKK4=;r-{$_*k5OHns|(lJ(%co5_+|&*?!a3e_%b`*^plR^H}4A!|1dCg?$Xxn?MipVCx%Tv-Hx=z)=&6ZF4W4pnmOegdLAHH}P7Q%spo`p-e zsrHGcQ-Ra%gFvWW+e*m(>CtU43v{DI+j7wsfo_r9g}LZ#fo_%De1Ucd^aK?&TFGhq z_rSHl@T(|jt)1Hm?x-K9gIr>0LNEKsLUjA#Q-MQO`=zJ7rHmc)xB^E5hpKlVAJER+ zm^lkmzr@THRS*R|`&*Q7uzKh^s$KixuZO9csNeHM2LZ}>+6L0U67^9~UtO;sW@TS) zKfLc$YDM5s=i$J?_Cd)%iEA-143spv->=&XYlZsQUb-;G_LJlx{X2H{Rl=8FE+v-D zmqnAlq_{lyh2{EEw(nVTo%_;$;Pn3cQ4B@mLIY?AxSyg96T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ti7JacFpYr=AZg?gTQ9s&ZBV z0yPzV5vP}5_V1(_bVC`n!Fl)>U@%Mp{Wc=9w{knVG{S&A_D~|#MQ^_d~sagQmD8IGGhz45MKuNlUz zHfaXadsrymhj)eW_L8r0Y6<%0K-3H-62SpXGw?zY?nHPc%M7Rc`Uaq4!%Rq~azy1V zX>#;7%?;buZEiQ)Z(VO<$N%~}Z(X;!qYY*-y)fGYk{ejRjhQ!Z+H%vnP3D#j8*Xpk zZf;+9)24Ql-v#~Z^8@_*0?tn#WV!edLGHbG`VSF^TWwa8?D9_^$lJD>V$$ama)kJt zAcvAWkFvx}f+C12@~KYt;|f`|7C9r#5~z6c2w!%{q494%(AlKxo8f}`5gu`oQ#{1U@T1EXrL(zeJsMH*(4J)O|vVRltu8_WA1k(vbI;C()i$m zYHa!v#FU?R$o;lOR-HF5bt%St$$>u1ssK|70V67hDXf*~XFj}FXZbLZ@nJ66ZzWO# zHd0qQiZ8W9!;-Y_rVdO%dl(mwV1mhf;ei;65D?R3&e?@2KfUE>M@;Y%R&S6LWUjwA z#eCdNAF{rlI8e$Ytq}92Xm01@9l)1}^X}lYBJ!e%gkeo#!iKndgoetas9;|d#)LXz z&wRLS`)F3^3tL_39!LgbJ-Egt8jW?wRX*6&g?XaF@UZ`AAu% zPmkg#MRCxSR+X>Ta}|7#glx$f{||vjYokhEt%oY8;-~yqathKzc3Nvy_;D$y;8t0- z(|_gCA!Mjcls<0^Vv+*VhyYBL@2vkP!6*Hjq?}q$R#2@!lRm}FssAKs6cc4Xdmonf zW9r}bXJ-<5xBv;W7`_v6%e;^gI&?fw= z56X6Z=9kiq0;+P94W*}WdUu*=s(dw{E}urFDLqBLb{hT1rT+J`1B=jb>Mxr{fAb>M5Y~6#PS}ulNc+?iV>)wXr9qr)Y#bs>oR< z{6gZayx^|^O3yx2|5Nu*dM@OsB3C>rS0LYZ7 +#include +#include -// Testovaci subor pre vlak -int main(){ +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"); - print_train(train); - cancel_train(train); + 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; } +