
/*П░ове░ка на ве░но▒▓▓а на в║ведени▓е ▒коби в п░ог░амен ▓ек▒▓ на езика "С"*/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <conio.h>
#include <process.h>

 FILE  *fileopen();       /* О▓ва░┐не на ▓ек▒▓ови┐ ┤айл */

 int   checker(FILE*);    /* П░ове░ка за ко░ек▓но▒▓ на п░ог░амни┐ ▓ек▒▓ */

 void  messager(int);     /* С║об╣ени┐ за г░е╕ки */

 FILE *fileopen()
 {
    char c;
    char s[80];
    FILE *f=NULL;

    while(f==NULL)
    {
      clrscr();
      printf("\n Write a filename of 'C'-language program : ");
      scanf("%s",s);
      f=fopen(s,"rt");
      if (f==NULL)
      {
	printf("\n %s",strerror(errno));
	printf("\n New or exit ? n/e");
	c=getch();
	if(c=='e') exit(0);
		 }
		  }
    return(f);
      }


 void messager(int mes)
 {

   clrscr();
   switch(mes)
   {

     case 1: printf("\n Error in brackets priory! [{}],({}),([]) ");
	     break;
     case 2: printf("\n There are more closed then opened brackets!");
	     break;
     case 3: printf("\n There is an error of the type ')('!");
	     break;
     case 4: printf("\n There are more opened then closed brackets!");
	     break;
     default: printf("\n All is right!");
	      break;
	       }
   printf("\n\n------------- Press a key ------------");
   getch();
		}

 int  checker(FILE *f)
 { char c;
   int count1=0,count2=0,count3=0;

   fseek(f,0,SEEK_SET);
   while(!feof(f))
   {  c=fgetc(f);
      switch(c)
      { case '{': if(count2>0 || count3>0) return(1);
		  count1++;
		  break;
	case '}':
		if(count2>0 || count3>0) return(1);
		count1--;
		if(count1<0) return(2);
		break;
       case '[':
		count2++;
		break;
       case ']':
		count2--;
		if(count2<0) return(2);
		break;
       case ')':
		c=fgetc(f);
		if(c=='(') return(3);
		else
		  fseek(f,1,SEEK_CUR);
		count3--;
		if(count3<0) return(2);
		break;
       case '(':
		count3++;
		break;
       }
     }
   if((count1>0)||(count2>0)||(count3>0)) return(4);
   }
 main()
 { FILE *flag=NULL;
   flag=fileopen();
   messager(checker(flag));
 }


