89 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			89 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <stdio.h>
 | |
| #include <assert.h>
 | |
| #include <stdlib.h>
 | |
| #include <ctype.h>
 | |
| //// @ 
 | |
| #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++)
 | |
|     {
 | |
|         printf("%c ", stack->values[i]);
 | |
|     }
 | |
|     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];
 | |
|     scanf("%s", str);
 | |
|     printf("Read: %s", str);
 | |
| 
 | |
|     int i=0;
 | |
|     char c, cs;
 | |
|     while(str[i]!='\0')
 | |
|     {
 | |
|         c=str[i];
 | |
|         if(c=='<'||c=='('||c=='{'||c=='[')
 | |
|         {
 | |
|            push_stack(&mystack, c); 
 | |
|         }
 | |
|         if(c=='>')
 | |
|         {
 | |
|             cs=pop_stack(&mystack);
 | |
|             if(cs!='<')
 | |
|             {
 | |
|                 printf("Crossed bracket %c in %d, expected %c", c, i);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
| //Crossed bracket > in 12, expected )
 | |
| 
 | |
|       
 | |
| 	return 0;
 | |
| }
 | |
|        
 |