#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int commentcheck=0;
int broisimnared(char *string)
{int i,br=0;
 for (i=0;i<strlen(string);i++)
  if (isspace(string[i])=='\n')
	br++;
  return br;
}
int broielsenared(char *string)
{int i,br=0;
if (strlen(string)>=4)
 for (i=0;i<strlen(string);i++)
  {
  if (string[i]=='/'&&string[i+1]=='*') commentcheck=1;
  if (string[i]=='*'&&string[i+1]=='/') commentcheck=0;
  if (string[i]=='"'&&string[i-1]!=string[i+1])
	{
	do
		i++;
	while (string[i]!='"');
	}
	if (commentcheck==0)
	 {
		if (string[i]=='/'&&string[i+1]=='/') break;
		if (string[i]=='e'&&string[i+1]=='l'&&string[i+2]=='s'&&string[i+3]=='e')
		if (isspace(string[i-1])==1||string[i-1]==';'||i-1<0)
		if (isspace(string[i+4])==1||string[i+4]==';'||string[i+4]=='('||string[i+4]=='\n') br++;
	 }
  }
 return br;
}

int broiifnared(char *string)
{int i,br=0;
if (strlen(string)>=2)
 for (i=0;i<strlen(string);i++)
  {
  if (string[i]=='/'&&string[i+1]=='*') commentcheck=1;
  if (string[i]=='*'&&string[i+1]=='/') commentcheck=0;
  if (string[i]=='"'&&string[i-1]!=string[i+1])
	{
	do
		i++;
	while (string[i]!='"');
	}
	if (commentcheck==0)
	 {
		if (string[i]=='/'&&string[i+1]=='/') break;
		if (string[i]=='i'&&string[i+1]=='f')
		if (isspace(string[i-1])==1||string[i-1]==';'||i-1<0)
		if (isspace(string[i+2])==1||string[i+2]=='('||string[i+2]=='\n') br++;
	 }
  }
 return br;
}

void tmp(void)
{
  FILE *ftmp;
  int b=0;
  char text[81];
  ftmp = fopen("$$$$$$$$.$$$","w+t");
  do
  {
  fgets(text,81,stdin);
  fputs(text,ftmp);
  if (text[0]=='\n')
  {
  fputs(text,ftmp);
  b++;
  }
  }
  while (b!=2);
  fclose(ftmp);
}

void minred(char *input,char *output)
{
  FILE *in;
  FILE *out;
  char text[255];
  int br=255;
  in = fopen(input,"rt");if (in==NULL) printf("\nNe moga da otvorq faila %c za 4etene",input);
  if (*output!='n')
  {out = fopen(output,"w+t");
  if (out==NULL) printf("\nNe moga da otvorq faila %c za zapis",output);
  }
  do
	 {
		fgets(text,254,in);
		if (br>broisimnared(text)&&broisimnared(text)!=0) br=broisimnared(text);
	 }
  while (feof(in)==NULL);
  fclose(in);       //moje da se izpolzva i rewind(in)
  fopen(input,"rt");
  do
	 {
	  fgets(text,254,in);
		if (broisimnared(text)==br&&*output!='n') fputs(text,out);
		if (broisimnared(text)==br&&*output=='n') puts(text);
	 }
  while (feof(in)==NULL);
  fclose(in);
  fclose(out);
}
void broiif(char *input,char *output)
{
  FILE *in;
  FILE *out;
  char text[255];
  int br=0,br2=0;
  in = fopen(input,"rt");
  if (in==NULL) printf("\nNe moga da otvorq faila %c za 4etene",input);
  if (*output!='n')
  {out = fopen(output,"at");
  if (out==NULL) printf("\nNe moga da otvorq faila %c za zapis",output);
  }
  do
	 {
		fgets(text,254,in);
		br+=broiifnared(text);
		br2+=broielsenared(text);
	 }
  while (feof(in)==NULL);
  fclose(in);
  itoa(br-br2,text,10);
  if (*output!='n')
	{
	 fputs("\nBroqt na if usloviqta e:",out);
	 fputs(text,out);
	}
  if (*output=='n')
	{
	 fputs("\nBroqt na if/else e:",stdout);
	 puts(text);
	}
  itoa(br2,text,10);
  if (*output!='n')
  {
  fputs("\nBroqt na if/else e:",out);
  fputs(text,out);
  }
  if (*output=='n')
	{
	fputs("\nBroqt na if/else e:",stdout);
	puts(text);
	}
  fclose(out);
}

char menu(void)
{
	  puts("\n1. Chetene na programa ot fail i zapis na rezultata vyv fail");
	  puts("\n2. Chetene na programa ot fail i izvejdane rezultata na ekrana");
	  puts("\n3. Chetene na programa ot klaviatura i zapis na rezultata vyv fail");
	  puts("\n4. Chetene na programa ot klaviatura i izvejdane rezultata na ekrana");
	  puts("\n5. Exit");
  return getch();
}

void main(void)
{ char g;
char infile[255],outfile[255];
  do
	{
	 clrscr();
	 g=menu();
	 switch (g) {
	 case '1':
	  {
	  puts("\nVyvedete vhodqsht fail");
	  scanf("%s",&infile);
	  puts("\nVyvedete ime na izhodqshtiq fail");
	  scanf("%s",&outfile);
	  minred(infile,outfile);
	  broiif(infile,outfile);
	  puts("Press any key to continue...");
	  getch();
	  break;
	  }
	 case '2':
	  {
		puts("\nVyvedete vhodqsht fail");
		scanf("%s",&infile);
		minred(infile,"n");
		broiif(infile,"n");
		puts("Press any key to continue...");
		getch();
		break;
	  }
	 case '3':
	 {
		puts("\nVyvedete ime za izhodqst fail");
		scanf("%s",&outfile);
		puts("\nMoje da zapo4nete vyvejdaneto na programata");
		tmp();
      puts("\nKrai na vyvejdaneto");
		minred("$$$$$$$$.$$$",outfile);
		broiif("$$$$$$$$.$$$",outfile);
		puts("Press any key to continue...");
		getch();
		break;
	 }
	 case '4':
	 {
		puts("\nMoje da zapo4nete vyvejdaneto na programata");
		tmp();
		puts("\nKrai na vyvejdaneto");
		minred("$$$$$$$$.$$$","n");
		broiif("$$$$$$$$.$$$","n");
		puts("Press any key to continue...");
		getch();
		break;
	 }
	 case '5':break;
	 default:
	 {
	 puts("Molq vyvedete pravilen izbor ot menuto(4islo ot 1 do 5)");
	 puts("Press any key to continue...");
	 getch();
	 }
	 }
	}
  while (g!='5');
}