#include <stdio.h>
#include <conio.h>
#include "math.h"
#include "strings.h"
bool dosyayaz=false;
bool cevirmegoster=true;
char dosyaismi[15];
FILE *dosya;
void parantezyoket ( char *);

void options(){
     char d;
     printf("\n\n\n\n\t\tOPTIONS");
    
    do {
     printf("\n\n\t[1]: Print convertions to the results column  ");
     if(cevirmegoster) printf("ON\n"); else printf("OFF\n"); 
      printf("\t[2]: Print convertions to the results column  ");
     if(dosyayaz) printf("ON (%s) \n",dosyaismi); else printf("OFF\n"); 
     printf("Press '1' or '2' to switch the options, any key to exit here.\n\n");
     d=getch();
    if(d=='1' && cevirmegoster) cevirmegoster=false;
    else if(d=='1' && !cevirmegoster) cevirmegoster=true;
  
   if(d=='2' && dosyayaz) dosyayaz=false;
   
   
    else if(d=='2' && !dosyayaz) {
     printf("\nType the file name ('results.txt', for example)  :\n");
     scanf("%s",dosyaismi);    
  if(   dosya=fopen(dosyaismi,"a+")) dosyayaz=true;   
     else printf("\nFile couldn't be created.\n\n");
      
         }
   
   
    
}while(d=='1' || d=='2');
    
     }




bool tekkarakter(char * st) {
    
    int i;
    bool k=false;
    for(i=0;st[i]!='\0';i++){
      if(st[i]!='\0' || st[i]!=' ') {
                     if(k) return false;
                     if(!k) k=true;
                     
                     }
                             
                             }
     
     return true;
     }

bool parantezvarmi(char * st) {
     int i;
     
       for(i=0;st[i]!='\0';i++){
      if(st[i]=='(' || st[i]==')') {
                  //  printf("aaa");
                      return true;
                          }
                             
                             }
     
     return false;
     
     
     
     }
     
bool terscevirmevarmi( char * st) {
     int i;
  while(parantezvarmi(st)) parantezyoket(st);
       for(i=1;st[i]!='\0';i++){
      if((st[i]!='(' && st[i]!=')')&&st[i-1]=='!') {
                //  printf("a");
                      return true;
                          }
                             
                             }
     return false;
     }     

void terscevir( char * st) {
     int i;
     
       for(i=1;st[i]!='\0';i++){
      if(st[i-1]=='!'&&st[i]=='1') {
                  st[i-1]=' ';
                  st[i]='0';
}
else if(st[i-1]=='!'&&st[i]=='0') {
                  st[i-1]=' ';
                  st[i]='1';
}


} 
     
     
     
     }




void bosluksil(char * st) {
     int i,j;
     for(i=0;st[i]!='\0';i++){
               if (st[i]==' '){
                            for(j=i;st[j]!='\0';j++) st[j]=st[j+1];
                            i--;
                            }               
                              
                              
                              }
     
     }

void hesapla (char * st) {
     bool x,y,i;
         
  //  while(terscevirmevarmi(st)) terscevir(st);
    
    
      if (st[0]=='0') x=false; else x=true;
     if (st[2]=='0') y=false; else y=true;
     
     if (st[1]=='V') i=false; else i=true;
      st[1]=' ';
      st[2]=' '; 
     
           if(i){
          
           if(x&&y) st[0]='1'; else st[0]='0';
           
           }
     else {
          
           if(x||y) st[0]='1'; else st[0]='0';
          
          
          }
     st[1]=' ';
      st[2]=' ';
   bosluksil(st);    
     }

bool parantezuygun(char * st) {
      int a,b,i;
      a=0;
      b=0;
     for(i=0;st[i]!='\0';i++){
      if(st[i]=='(')
      {   a++;   }
      if(st[i]==')')
      {   b++;   }
      }
      
      if (a==b) return true;
      else {
           return false;
      }
     }


void parantezyoket(char * st) {
     int a,b,i;
     char tmp[50];
     for(i=0;st[i]!='\0';i++){
      if(st[i]=='(')
      {   a=i;   }
      }
      for(i=a;st[i]!='\0';i++){
      if(st[i]==')')
      {   b=i; break;  }
      }
      
    //    printf("%d %d",a,b);  
      
      for(i=a+1;i<b;i++){
      tmp[i-a-1]=st[i];
      st[i]=' ';
      }
  
      st[b]=' ';
      st[a]=' ';
      tmp[i-a-1]='\0';

   while(!tekkarakter(tmp)) {
  
  while(terscevirmevarmi(tmp)) terscevir(tmp);
   hesapla(tmp); 
}
        
      st[a+1]=tmp[0];
  //   puts(st);
     bosluksil(st);
   //  puts(st);
   //  printf("%s",st);
   
     }













int ifadesayi;

char  ifade[50],harfler[50],ifadetmp[50];
bool a[26],c;
 bool varmi[26];
 
int ifadesayisi() {
 int i;

  int sayi=0;
  for(i=0;i<26;i++) varmi[i]=false;
 for(i=0;ifade[i]!='\0';i++) {
   if(ifade[i]>96 &&  ifade[i]<123) {
              if(!varmi[ifade[i]-97]) {  varmi[ifade[i]-97]=true; harfler[sayi]=ifade[i];  sayi++;     }                  
                            }
                         } 
harfler[sayi+1]='\0';
//puts(harfler);
    return sayi;
    
    
}


void cevir() {
     int i;
     for(i=0;ifade[i]!='\0';i++) {  ifadetmp[i]=ifade[i];
     
     
     if(ifade[i]>96 && ifade[i]<123) {
         if( a[ifade[i]-97]==0) ifadetmp[i]='0';  
         if( a[ifade[i]-97]==1) ifadetmp[i]='1';
                    }                            
                                 
                                 }
     
     ifadetmp[i]='\0';
     
     }


bool coz() {
     
     if(parantezuygun(ifadetmp)) {  
    while(!tekkarakter(ifadetmp)) {   
     while(terscevirmevarmi(ifadetmp) ) terscevir(ifadetmp);
     while(parantezvarmi(ifadetmp) ) parantezyoket(ifadetmp);


    bosluksil(ifadetmp);
    hesapla(ifadetmp);

}}
    
    
    
    if(ifadetmp[0]=='0') return 0;
    else return 1;
      
     }

     
void help() {
  printf("\n\tHELP\n\n!pV(q&r) is a good example of input. p,q,r are the variables.\n\n");   
  printf("!:NOT\nV:OR\n&:AND\n");
  printf("\nAvailable commands: quit, help, options\n\nIt is possible to print the results to a file and to disable to print the       convertions to the results column with the 'Options' section.\n\n");   
  printf("\nAs this version of the software is only a beta, it is not recommended for your  complex calculations.\nYour feedback is important for me: admin@coolbluegames.net\n\n");
  printf("Press any key to continue...\n");
  getch();    
     }     

int main() {
    
 int i,j,k;
 
 int b[10],p;
 p=2;
for(i=0;i<20;i++) a[i]=false;

printf("\tMODUS PONENS v0.9 BETA\n\t\tby Ahmet \"Gord10\" Kamil Kele%c\n\n\t\t\t\t04.14.2009\n",159);
help();
printf("Type 'help' for reading the instructions again.\n");
 
while(strcmp(ifade,"quit")!=0) {
                
          
printf("\n\nInput the logical expression that you want to calculate\n(Use single letters [except for 'V'] for variables and !, &, V for logical\noperators):\n");
gets(ifade);
if(ifade[0]==0 && ifade[1]==0) continue;  


if(dosyayaz) fputs(ifade,dosya);
if(dosyayaz) fputc('\n',dosya);
if(strcmp(ifade,"help")==0) {
                            help();
                            continue;
                            
                            }
             
if(strcmp(ifade,"options")==0) {
                            options();
                            continue;
                            
                            }               
 ifadesayi=ifadesayisi();
 
 for(i=0;i<26;i++){ if(varmi[i]) {printf("%c ",i+97); if(dosyayaz) fprintf(dosya,"%c ",i+97);                }}
 printf("Results\n");
 if(dosyayaz) fprintf(dosya,"Results\n");
 for(i=0;i<pow(2,ifadesayi);i++){
 for(j=ifadesayi-1;j>=0;j--) {
  p=pow(2,j);                         
                         
         if( (((i-i%p)/p)) %2   ){ a[harfler[ifadesayi-1-j]-97]=true;   }         
           else { a[harfler[ifadesayi-1-j]-97]=false;   }     
                
         
         if(a[harfler[ifadesayi-1-j]-97]==true){  printf("1 "); if(dosyayaz) fprintf(dosya,"1 ");  }         
           else { printf("0 "); if(dosyayaz) fprintf(dosya,"0 "); } 
        
           
        
           
           }
         
 cevir();  if(cevirmegoster)  {printf("%s  ",ifadetmp); if(dosyayaz) fprintf(dosya,"%s  ",ifadetmp);}
     if( coz()    ){  printf("   1\n");  if(dosyayaz) fprintf(dosya,"   1\n");  }         
          else { printf("   0\n"); if(dosyayaz) fprintf(dosya,"   0\n"); } 
         
                  
           }
           if(dosyayaz) fprintf(dosya,"\n\n\n");
           printf("Press any key to continue...\n");
  getch();    
           
           
           }
        //   getch();
    return 0;
}
