First try

This commit is contained in:
Anton Dolozin 2025-10-13 14:13:36 +02:00
parent 6e3093db61
commit 1b7f8c3c99

194
a1/program.c Normal file
View File

@ -0,0 +1,194 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define BRACKETS_NUM 100
typedef struct Node{
char data;
struct Node* next;
} LinkedNode;
LinkedNode* createNode(char data){
LinkedNode* node = (LinkedNode*)malloc(sizeof(LinkedNode));
node->data = data;
node->next = NULL;
return node;
}
void push(LinkedNode** head, char data){
if(*head == NULL){
LinkedNode* node = createNode(data);
*head = node;
return;
}
LinkedNode* cur = *head;
while(cur->next){
cur = cur->next;
}
cur->next = createNode(data);
}
bool compare(char data1, char data2){
if(data1 == '(' && data2 == ')'){
return true;
}
else if(data1 == '<' && data2 == '>'){
return true;
}
else if (data1 == '[' && data2 == ']'){
return true;
}
else if (data1 == '{' && data2 == '}'){
return true;
}
return false;
}
char pop(LinkedNode** head){
LinkedNode* prev = NULL;
LinkedNode* ptr = *head;
while (ptr->next)
{ prev = ptr;
ptr = ptr->next;
}
char res = ptr->data;
if(prev){
prev->next = NULL;}
else{
*head = NULL;
}
free(ptr);
return res;
}
void read_input(LinkedNode** head){
char buff[BRACKETS_NUM];
fgets(buff, 100, stdin);
int i =0;
while (buff[i])
{
if(buff[i] == '[' || buff[i] == '<' || buff[i] == '{' || buff[i] == '('){
push(head, buff[i]);}
if(buff[i] == '>' || buff[i] == '}' || buff[i] == ']' || buff[i] == ')'){
char popped = pop(head);
if (!compare(popped, buff[i]))
{ if (popped == '[' && buff[i] == '>')
{
printf("Read: %s\n", buff);
printf("Crossed bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected ]\n");
return;
}
if (popped == '(' && buff[i] == '>')
{
printf("Read: %s\n", buff);
printf("Crossed bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected )\n");
return;
}
if (popped == '{' && buff[i] == '>')
{
printf("Read: %s\n", buff);
printf("Crossed bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected }\n");
return;
}
if (popped == '<' && buff[i] == ']')
{
printf("Read: %s\n", buff);
printf("Square bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected >\n");
return;
}
if (popped == '(' && buff[i] == ']')
{
printf("Read: %s\n", buff);
printf("Square bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected )\n");
return;
}
if (popped == '{' && buff[i] == ']')
{
printf("Read: %s\n", buff);
printf("Square bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected }\n");
return;
}
if (popped == '<' && buff[i] == ')')
{
printf("Read: %s\n", buff);
printf("Round bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected >\n");
return;
}
if (popped == '[' && buff[i] == ')')
{
printf("Read: %s\n", buff);
printf("Round bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected ]\n");
return;
}
if (popped == '{' && buff[i] == ')')
{
printf("Read: %s\n", buff);
printf("Round bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected }\n");
return;
}
if (popped == '<' && buff[i] == '}')
{
printf("Read: %s\n", buff);
printf("Curly bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected >\n");
return;
}
if (popped == '(' && buff[i] == '}')
{
printf("Read: %s\n", buff);
printf("Curly bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected )\n");
return;
}
if (popped == '[' && buff[i] == '}')
{
printf("Read: %s\n", buff);
printf("Curly bracket %c", buff[i]);
printf(" in %d,", i);
printf(" expected ]\n");
return;
}
}
}
i++;
}
printf("Read: %s\n", buff);
printf("All brackets OK\n");
}
int main(void){
LinkedNode* head = NULL;
read_input(&head);
return 0;
}