/*	Teoodo Petkov Peev fak.nom. 111205066 gr90A */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void Compute(int p,int *br1,int *br2,int *br3);
void menu(void);
void file(void);
void keyboard(void);
void prov(char im[]);

char imen[80];
char imen1[80];
int flg=0;
int flg1=0;
int br11=0,br22=0,br33=0;

int main()
{
 clrscr();
 menu();
 return 0;
}

void prov(char im[])
{
 int k=0,o=0,i;
 clrscr();
 while(k==0)
 {
  switch(o)
  {
	case 0:
	{
		if(strlen(im)>50||strlen(im)==0)
		{
			printf("\nFile name must contain max 8 symbols! \n "
					 "\nTry again ->");
			scanf("%s",&im);
			o=0;
			break;
		}
		else o=1;
		break;
	}
	case 1:
	{
		if(im[0]>='0'&&im[0]<='9')
		{
			printf("\nThe filename can't begin with digit! \n"
					 "\nTry again ->");
			scanf("%s",&im);
			o=0;
			break;
		}
		else o=2;
		break;
	}
	case 2:
	{
		for(i=0;i<8;i++)
		{
			if(im[i]=='*'||im[i]=='?'||im[i]=='&'||im[i]=='!')
			{
				printf("\nFilename contain illegal symbols! \n"
						 "\nTry again ->");
				scanf("%s",&im);
				o=0;
				break;
			}
			else o=0;
			k=1;
		}
	break;
	}
  }
 }
}

void menu()
{
 char n,key;
 int i=0;
 clrscr();
 printf("\n		MENU\n"
"\n1-> Open file for reading."
"\n2-> Save file."
"\n3-> Enter code from keyboard."
"\n4-> Show integer count."
"\n5-> Show real count."
"\n6-> Show total count."
"\n7-> Exit."
"\n \nPlease select from 1 to 7 ->");
 scanf("%s",&n);
 while(n<'1' || n>'8')
 {
  printf("\nVyveli ste greshni danni! vyvedi otnovo (ot 1 do 8) %d ->",++i);
  scanf("%s",&n);
 }
 switch(n)
 {
  case '1':
  {
	br11=0;br22=0;br33=0;
	clrscr();
	printf("File open: ->");scanf("%s",&imen);
	prov(imen);
	printf("\nSuccessfully entered name! Press any key...");
	flg=1;
	getch();
	menu();
  }
  case '2':
  {
	clrscr();
	printf("Vyvedi ime na fail za zapis ->");scanf("%s",&imen1);
	prov(imen1);
	printf("\nUspeshno vavedeno ime!Press any key to get back to menu...");
	flg1=1;
	getch();
	menu();
  }
  case '3':
  {
	clrscr();
	keyboard();
  }
  case '4':
  {
	clrscr();
	file();
	printf("\nCount integer numbers:          -> %d\n",br11);
	printf("\nPress any key...");
	getch();
	menu();
  }
  case '5':
  {
	clrscr();
	file();
	printf("\nCount a fractional number:      -> %d\n",br22);
	printf("\nCount exponentional number:     -> %d\n",br33);
	printf("\nPress any key...");
	getch();
	menu();
  }

  case '6':
  {
	clrscr();
	file();
	printf("\nCount integer numbers:          -> %d\n",br11);
	printf("\nCount a fractional number:      -> %d\n",br22);
	printf("\nCount exponentional number:     -> %d\n",br33);
	printf("\nTotal count of numbers:         -> %d\n",(br11+br22+br33));
	printf("\nPress any key...");
	getch();
	menu();
  }
  case '7':
  {clrscr();printf("Are you sure? <Y/N>");key=getch();if(key=='y'||key=='Y') exit(0);else {clrscr(); menu();}}
 }
}

void keyboard()
{
  if(flg1==0)
  {
	printf("Firs enter filename! Press any key...");
	getch();
	menu();
  }
  else
  {
	char txt[81];
	FILE *imm;
	clrscr();
	if((imm=fopen(imen1,"wt"))==NULL)
	{
	 printf("Error:Can't open file! \n\nPress any key...");
	 getch();
	 menu();
	}
	clrscr();
	printf("\nEnter code:\n");
	fgets(txt,80,stdin);
	fprintf(imm,"%s",&txt);
	do
	{
	 fgets(txt,80,stdin);
	 fprintf(imm,"%s",&txt);
	}
	while(txt[1]!='\0');
	fclose(imm);
	printf("\nCode entered succesfully! Press any key...");
	flg1=0;
	getch();
	menu();
  }
}

void file()
{
 if(flg==0)
 {
 printf("First open file! Press any key...");
 getch();
 menu();
 }
 else
 {
  FILE *inn;
  int p;
  if((inn=fopen(imen,"rt"))==NULL)
  {
  printf("Error:Can't open file! Press any key...");
  getch();
  menu();
  }
  while((p=fgetc(inn))!=EOF)
  {
	Compute(p,&br11,&br22,&br33);
  }
  flg=0;
  fclose(inn);
 }
}

void Compute(int p,int *br1,int *br2,int *br3)
{
 static int st1=0;
 switch(st1)
 {
  case 0:
  {
	if(p>='0'&&p<='9')
	st1=2;
	if(p=='-'||p=='+')st1=1;
	break;
  }

  case 1:
  {
	if(p>='0'&&p<='9')
	st1=2;
	else st1=0;
	break;
  }

  case 2:
  {
	if(p=='.') {st1=3;break;}
	if(!(p>='0'&&p<='9'))
		if(p!='.'||p!='E'||p!='e')
		{
			(*br1)++;
			st1=0;
		}
		break;
	}

	case 3:
	{
		if(p>='0'&&p<='9')
		st1=4;
		else st1=0;
		break;
	}

	case 4:
	{
		if(p=='E'||p=='e') {st1=5; break;}
		if(!(p>='0'&&p<='9'))
			if(p!='E'||p!='e')
			{
				(*br2)++;
				st1=0;
			}
		break;
	}

	case 5:
	{
		if(p>='0'&&p<='9')
		st1=6;
		else st1=0;
		break;
	}

	case 6:
	{
		if(!(p>='0'&&p<='9'))
			{
				(*br3)++;
				st1=0;
			}
		break;
	}



  }

}
