From 138c5f4e91ee67862f9667b63cb32ef5688d7f27 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 29 Oct 2024 17:46:23 +0100 Subject: [PATCH] Initializacia --- a1/main | Bin 0 -> 16336 bytes a1/program.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100755 a1/main diff --git a/a1/main b/a1/main new file mode 100755 index 0000000000000000000000000000000000000000..5bc851cc3d552394b9de71f11fe1d97c3af4ec6b GIT binary patch literal 16336 zcmeHOeQXrR6`!*)IK*(rS1^!2khX+t-}$oV&eZ zNC~bCDV9N{Qqs~YsuJ~&BxLayNCR$CISt|Zdo%Oa z=hZo>qDrl*ozw1{_j?~R^JaE$c6Vlf!=?=-fq>vtD%J?%20{vnXhCSD&VWS3N-+iJ z`QkP)5&Q&+DS1Q@NL9L2GM1KWycCq|Dk)Qh-l|~1lxs+o?8ZxNlaz$1(Cr|*G7@Rt zOXoPLk|ED7E!XX1T4+bHIaa0gkF6d{JGI>*P1%lEvU^6`J)`ZIUZn~l zG39ttOz2pt0^X#cjobw`w&4`gh*4ea!>kmMf0*(ie3cqyiT_c-{b zaqwHl!KtFf{C^j?fy0}g1z>nV@Dz{Ri{Q(FO%roPD58Ws?uBK=I-^!gA{9+0_S&MQ z)poKX>ttftjueFG{+N|@q8Z0(k0w&0BPYdiWG%vD4_dLd2d$Q9A}KmDiImeK$b<&S z3bAohnt%SA-@Yt|YC-wb^@h***N0gj#<}a;PIE_{6BTfSac;WaSEWp_p*<3BaQE@ko6yS7S zaN_=^>yxevPRF%A{Y~(-O0vEJoUTJo{RO!Gi;$vM3UGBDDvJvRxc)0*S3dP4;77oZ zfFA)r0)7Pi2>ee(;P1iP{%Lk!DK~q|UR@%Dd9dFp$zL$LUo1Z>>z7}?7w~X?!NVZI z`GWM@NHRS1=X^eYRPr>j8y>po@-(3v9y;UlG|?I!dd}r(f;Bw!6!Y-jF1^(#AI4K@8W=glLlkD^-nS-*qk+b!YX z`~#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8vKy{fW8mNcU%yCTNhH}nlxz8`}AQs7iiul^d~2ISs!Kk(bVAowis=_UBL z^4^!W$1+Y$IF04*(dLBZL|X-btdnbL3CG0f(s;{hw_>z7J`2rw+GZt7tu1xur`yj?(^2ILnE&U2uZJ8LOf*wNQYU=_DAG?piif7YSTQn7? zJr0dqpoO|yIlNRBcwvnF6xuI3+D^q`-ki;9S8@l2Wnr5HhoAOO6x|dtc3a2T+EMQc z5zg*uccRUpPDar-&S4veo#_zaRNAq_t*KlX-QQtnoIM3lb1s2RL5aAA)~#=>qQNWG z+oIVv5svRkp$J8tjFR9rdNz?xjbSXvGIlab20GV~bVOKIGmLDwH4TY&6YI~;Dq*Rc2KF81sWD4!Q``PF{e_`VLEMqiJD75E! z3R8m>HTBwm9{4{nH{|;B{F&)OZO8LxZ~dc?)7+WuFX{F(H7K}9R7c9PJM=%ia*8gUVY}7()>JFV9(FzPgVg$Vm-x;@{GR^0gYWQpPv_a zKQZqcrt6Oq+i^es02noq?fLoP*b>qfqD>L^%=Szt!nn|0M6_zPqFfF;U_0h3U`!a7 z&-2gFIQCwD({%Y-o!ttyU+f|(Lv^K8NO|lii8@EL!<-uDh~S e=gwM1_jsIhxvWc%FU9Py(H|SqT#tc=75@Uo{Av#X literal 0 HcmV?d00001 diff --git a/a1/program.c b/a1/program.c index 53c5fdf..bd9dc62 100644 --- a/a1/program.c +++ b/a1/program.c @@ -1 +1,72 @@ #include +#include + +#define MAX_LEN 101 + +typedef struct { + char bracket; + int position; +} StackItem; + +int is_opening(char ch) { + return ch == '(' || ch == '{' || ch == '[' || ch == '<'; +} + +int is_closing(char ch) { + return ch == ')' || ch == '}' || ch == ']' || ch == '>'; +} + +char matching_bracket(char ch) { + switch (ch) { + case ')': return '('; + case '}': return '{'; + case ']': return '['; + case '>': return '<'; + default: return 0; + } +} + +void check_brackets(const char *line) { + StackItem stack[MAX_LEN]; + int stack_top = -1; + + printf("Read: %s\n", line); + + for (int i = 0; line[i] != '\0'; i++) { + char ch = line[i]; + + if (is_opening(ch)) { + if (stack_top < MAX_LEN - 1) { + stack[++stack_top].bracket = ch; + stack[stack_top].position = i; + } else { + printf("Error: Stack overflow\n"); + return; + } + } else if (is_closing(ch)) { + if (stack_top >= 0 && stack[stack_top].bracket == matching_bracket(ch)) { + stack_top--; + } else { + printf("Crossed bracket %c in %d, expected %c\n", ch, i, + stack_top >= 0 ? matching_bracket(stack[stack_top].bracket) : '#'); + return; + } + } + } + + if (stack_top >= 0) { + printf("Unmatched bracket %c in %d\n", stack[stack_top].bracket, stack[stack_top].position); + } else { + printf("All brackets OK\n"); + } +} + +int main() { + char line[MAX_LEN]; + if (fgets(line, sizeof(line), stdin)) { + line[strcspn(line, "\n")] = '\0'; + check_brackets(line); + } + return 0; +} +