usaa24/a1/program.c

133 lines
2.8 KiB
C
Raw Permalink Normal View History

2024-10-25 19:39:56 +00:00
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
2024-10-25 19:59:56 +00:00
#include <stdbool.h>
2024-10-25 19:39:56 +00:00
#define STACK_SIZE 200
struct Stack
{
char values[STACK_SIZE];
int size;
};
void push_stack(struct Stack* stack, char value)
{
assert(stack->size < STACK_SIZE);
stack->values[stack->size] = value;
stack->size += 1;
}
char pop_stack(struct Stack* stack)
{
assert(stack->size > 0);
char value = stack->values[stack->size-1];
stack->size -= 1;
return value;
}
int count_stack(struct Stack* stack)
{
return stack->size;
}
void print_stack(struct Stack* stack)
{
for (int i=0; i<stack->size; i++)
{
2024-10-25 19:41:46 +00:00
printf("%c ", stack->values[i]);
2024-10-25 19:39:56 +00:00
}
printf("\n");
}
char para(char c)
{
if(c=='(') return ')';
if(c=='<') return '>';
if(c=='{') return '}';
if(c=='[') return ']';
if(c==')') return '(';
if(c=='>') return '<';
if(c=='}') return '{';
if(c==']') return '[';
}
int main()
{
struct Stack mystack;
mystack.size=0;
char str[128];
2024-10-25 20:48:21 +00:00
//scanf("%s", str);
2024-10-25 20:27:36 +00:00
fgets(str,100,stdin);
2024-10-25 20:31:57 +00:00
if(str[strlen(str)-1]=='\n') { str[strlen(str)-1]='\0';}
2024-10-25 19:39:56 +00:00
printf("Read: %s", str);
int i=0;
char c, cs;
2024-10-25 19:59:56 +00:00
bool is_ok=true;
2024-10-25 20:36:23 +00:00
while(str[i]!='\0' && is_ok)
2024-10-25 19:39:56 +00:00
{
c=str[i];
if(c=='<'||c=='('||c=='{'||c=='[')
{
push_stack(&mystack, c);
2024-10-25 20:48:21 +00:00
2024-10-25 19:39:56 +00:00
}
2024-10-25 19:51:03 +00:00
if(c=='>'||c==']'||c=='}'||c==')')
2024-10-25 19:39:56 +00:00
{
2024-10-25 19:51:03 +00:00
if(count_stack(&mystack)>0)
cs=pop_stack(&mystack);
else
{
2024-10-25 20:20:59 +00:00
printf("\nUnexpected closing bracket %c in %d\n", c,i);
2024-10-25 19:59:56 +00:00
is_ok=false;
2024-10-25 20:14:36 +00:00
break;
2024-10-25 19:51:03 +00:00
}
2024-10-25 20:03:19 +00:00
if(c=='>' && cs!='<')
2024-10-25 19:39:56 +00:00
{
2024-10-25 20:48:21 +00:00
printf("\nCrossed bracket %c in %d, expected %c \n", c, i, para(cs));
2024-10-25 19:59:56 +00:00
is_ok=false;
2024-10-25 20:14:13 +00:00
break;
2024-10-25 19:51:03 +00:00
}
2024-10-25 20:20:59 +00:00
if(c==']' && cs!='[')
2024-10-25 19:51:03 +00:00
{
2024-10-25 20:48:21 +00:00
printf("\nCrossed bracket %c in %d, expected %c \n", c, i, para(cs));
2024-10-25 19:59:56 +00:00
is_ok=false;
2024-10-25 20:14:13 +00:00
break;
2024-10-25 19:51:03 +00:00
}
2024-10-25 20:03:19 +00:00
if(c==')' && cs!='(')
2024-10-25 19:51:03 +00:00
{
2024-10-25 20:48:21 +00:00
printf("\nCrossed bracket %c in %d, expected %c \n", c, i, para(cs));
2024-10-25 19:59:56 +00:00
is_ok=false;
2024-10-25 20:14:13 +00:00
break;
2024-10-25 19:51:03 +00:00
}
2024-10-25 20:03:19 +00:00
if(c=='}' && cs!='{')
2024-10-25 19:51:03 +00:00
{
2024-10-25 20:48:21 +00:00
printf("\nCrossed bracket %c in %d, expected %c \n", c, i, para(cs));
2024-10-25 19:59:56 +00:00
is_ok=false;
2024-10-25 20:14:13 +00:00
break;
2024-10-25 19:39:56 +00:00
}
}
2024-10-25 19:51:03 +00:00
i++;
2024-10-25 19:39:56 +00:00
}
2024-10-25 20:36:23 +00:00
2024-10-25 20:48:21 +00:00
if(is_ok)
2024-10-25 20:20:59 +00:00
{
2024-10-25 20:43:05 +00:00
if(count_stack(&mystack)>0)
{
printf("\nMissing closing brackets: ");
do
{
2024-10-25 20:36:23 +00:00
cs=pop_stack(&mystack);
printf("%c", para(cs));
2024-10-25 20:43:05 +00:00
}while(count_stack(&mystack)>0);
printf("\n");
}
else
2024-10-25 20:22:28 +00:00
printf("\nAll brackets OK\n");
2024-10-25 20:43:05 +00:00
}
2024-10-25 19:39:56 +00:00
2024-10-25 19:41:46 +00:00
2024-10-25 19:39:56 +00:00
return 0;
2024-10-25 19:51:03 +00:00
}