/*
islem-kontrol v1.0 Girilen matematiksel ifadenin dogrulugunu denetler.
Copyright (C) 2005 Engin KUZU

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

//http://www.gnu.org/copyleft/gpl.html
//Turkce cevirisi: http://www.belgeler.org/howto/gpl_copy.html
//Web: http://www.enginkuzu.org

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

char stack[20];
char *stackpointer;

char dizi[256];
char *p;
char *r;

void hata()	//hata mesajlari
{
	printf("HATA ::: %d. karakterde sorun var.\nKarakter = \"%c\" dir.\n",p-dizi,*p);
	getch();
	exit(1);
}
void stack_oku()	//stack dan veri cekme
{
	if(stackpointer==stack)
	{
		printf("HATA ::: Fazla parantez kullanimi var.\n");
		printf("HATA ::: %d. karakterde = \"%c\" dir.\n",p-dizi,*p);
		getch();
		exit(1);
	}
	stackpointer--;
}
void stack_yaz(char c)	//stack a veri ekleme
{
	*stackpointer=c;
	stackpointer++;
}
int parantez_ac()	//acilan parantez kontrolu
{
	if( *p=='(' | *p=='{' | *p=='[' )
	{
		return 1;
	}
	else
		return 0;
}
int parantez_kapa()	//kapanan parantez kontrolu
{
	char c = *(stackpointer-1);

	if( *p==')')
	{
		if( c == '(' )
			return 1;
		else
			return 0;
	}
	else if( *p=='}' )
	{
		if( c == '{' )
			return 1;
		else
			return 0;
	}
	else if( *p==']' )
	{
		if( c == '[' )
			return 1;
		else
			return 0;
	}	
	else
		return 0;
}
int sayi_mi()	//sayi kontrolu
{
	if ( *p>96 && *p<123 )
		return 1;
	else
		return 0;
}

int operator_mu()	//operator kontrolu
{
	if ( *p=='+' | *p=='-' | *p=='*' | *p=='/' )
		return 1;
	else
		return 0;
}

int main()
{
	stackpointer=stack;
	
	printf("incelenecek ifadeyi giriniz = ");
	scanf("%s",dizi);

	p=dizi-1;
	r=dizi+strlen(dizi)-1;

	while(1)
	{
		p++;
		if(p>r)		//inceleme sonu kontrolu
			break;
		if(p==dizi)	//dizi bası durumu
		{
			if( !( sayi_mi() | parantez_ac() ) )
				hata();
		}
		if(p==r)	//dizi sonu durumu
		{
			if( sayi_mi() )
				continue;
			else if( parantez_kapa() )
			{
				stack_oku();
				continue;
			}
			else
				hata();				
		}
		if( parantez_ac() | operator_mu() )
		{
			if( parantez_ac() )
				stack_yaz(*p);
			p++;
			if( sayi_mi() )
			{
				p--;
				continue;
			}
			else if( parantez_ac() )
			{
				p--;
				continue;
			}
			else
				hata();			
		}
		if( parantez_kapa() | sayi_mi() )
		{
			if( parantez_kapa() )
				stack_oku();
			p++;
			if( operator_mu() )
			{
				p--;
				continue;
			}
			else if( parantez_kapa() )
			{
				p--;
				continue;
			}
			else
				hata();	
		}	
	}

	if( stackpointer == stack )
	{
		printf("IFADE SORUNSUZ.\n");
	}
	else
	{
		printf("HATA ::: Kapatilmamis parantezler var.\n");
	}
	getch();
	return 0;
}

