//#define WBWINNAME "con:0/10/320/120/Rechentrainer/CLOSE"

/*  Ansi  */
#pragma +
#include <math.h>
#include <stdlib.h>
#pragma -
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <wbstartup.h>

/* Libraries */
#include <libraries/mui.h>
#include <libraries/gadtools.h>

/* protos */
#include <clib/muimaster_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>

/*  Pragmas  */
#include <pragma/muimaster_lib.h>
#include <pragma/exec_lib.h>
#include <pragma/intuition_lib.h>


void init(void);
void end(void);
int Multiplikation(char*);
int Division(char*);
int Addition(char*);
int Subtraktion(char*);
extern "C++" void Dateilesen(int[], int[]);
extern "C++" void Dateischreiben(int[], int[]);
void Zufall(int*, int*, int, int);
void Vergleichen(int*, int*, int*);
void auswertung(int[], int[], int*, int*, int*, int*, int*, int*, int*);


/* MUIBuilder */

#include "rechentrainerGui.c"


struct Library * MUIMasterBase;
struct Library * LocaleBase;


/* main function */
int main(int argc, char *argv[])
{
        struct ObjApp * App = NULL;     /* Application object */
        BOOL    running = TRUE;
        ULONG   signal;
        char *erga,*mal,str[12],z[8],x[8];
        int j,i=0,erg,richtig[5]={0,0,0,0,0},falsch[5]={0,0,0,0,0};
        int prozg=0, prozm=0, prozd=0, proza=0, prozs=0, gesr=0, gesf=0;

                          //zu oben [0]Mul, [1]Div, [2]Addi, [3]Subtr
        init();           //Initialisierung

                          //Create Application von Mui-Builder
        if (!(App = CreateApp()))
        {
                printf("Can't Create App\n");
                end();
        }
        while (running)
        {
                switch (DoMethod(App->App,MUIM_Application_Input,&signal))
                {
                case MUIV_Application_ReturnID_Quit:
                        running = FALSE;
                    break;
                case BT_AUSWAHL:
                      auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
                      set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
                      set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
                      set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
                      set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
                      set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
                      inttostr(gesr,x);
                      set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
                      inttostr(gesf,x);
                      set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
                      inttostr(richtig[0],x);
                      set(App->Mul_Richtig, MUIA_Text_Contents, x);
                      inttostr(falsch[0],x);
                      set(App->Mul_Falsch, MUIA_Text_Contents, x);
                      inttostr(richtig[1],x);
                      set(App->Div_Richtig, MUIA_Text_Contents, x);
                      inttostr(falsch[1],x);
                      set(App->Div_Falsch, MUIA_Text_Contents, x);
                      inttostr(richtig[2],x);
                      set(App->Addi_Richtig, MUIA_Text_Contents, x);
                      inttostr(falsch[2],x);
                      set(App->Addi_Falsch, MUIA_Text_Contents, x);
                      inttostr(richtig[3],x);
                      set(App->Subtr_Richtig, MUIA_Text_Contents, x);
                      inttostr(falsch[3],x);
                      set(App->Subtr_Falsch, MUIA_Text_Contents, x);
                    break;
                case ANZ_MUL:
                    get(App->Anz_Mul, MUIA_String_Contents, &mal);
                    
                    if (atoi(mal) != 0 )
                       {
                        erg=Multiplikation(str);
                        set(App->Aufg_Mul, MUIA_Text_Contents, str);
                        set(App->Mul_Nr, MUIA_Text_Contents, "1");
                        i=atoi(mal);
                        j=1;
                       }
                    else
                       {
                        set(App->Aufg_Mul, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                        set(App->Mul_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                       }
                    break;
                case ERG_MUL:
                    get(App->Erg_Mul, MUIA_String_Contents, &erga);
                    set(App->Mul_Erg, MUIA_Text_Contents, "");
                    if(i)
                     {
                      ++j;
                      if (erg == atoi(erga))
                        {
                           set(App->Mul_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
                           ++richtig[0];
                           inttostr(richtig[0],x);
                           set(App->Mul_Richtig, MUIA_Text_Contents, x);
                        }
                      else
                        {
                           ++falsch[0];
                           inttostr(falsch[0],x);
                           set(App->Mul_Falsch, MUIA_Text_Contents, x);
                           set(App->Mul_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
                           inttostr(erg,x);
                           set(App->Mul_Erg, MUIA_Text_Contents, x);
                        }
                      auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
                      set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
                      set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
                      set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
                      set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
                      set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
                      inttostr(gesr,x);
                      set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
                      inttostr(gesf,x);
                      set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
                      --i;
                      if(i)
                        {
                           set(App->Erg_Mul, MUIA_String_Contents, "");
                           inttostr(j,z);
                           set(App->Mul_Nr, MUIA_Text_Contents, z);
                           erg=Multiplikation(str);
                           set(App->Aufg_Mul, MUIA_Text_Contents, str);
                        }
                      else
                        {
                           set(App->Aufg_Mul, MUIA_Text_Contents,GetMBString(MSG_Ready));
                           set(App->Erg_Mul, MUIA_Text_Contents, "");
                        }
                     }
                    break;
                case ANZ_DIV:
                    get(App->Anz_Div, MUIA_String_Contents, &mal);
                    
                    if (atoi(mal) != 0 )
                       {
                        erg=Division(str);
                        set(App->Aufg_Div, MUIA_Text_Contents, str);
                        set(App->Div_Nr, MUIA_Text_Contents, "1");
                        i=atoi(mal);
                        j=1;
                       }
                    else
                       {
                        set(App->Aufg_Div, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                        set(App->Div_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                       }
                    break;
                case ERG_DIV:
                    get(App->Erg_Div, MUIA_String_Contents, &erga);
                    set(App->Div_Erg, MUIA_Text_Contents, "");
                    if(i)
                     {
                      ++j;
                      if (erg == atoi(erga))
                        {
                           set(App->Div_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
                           ++richtig[1];
                           inttostr(richtig[1],x);
                           set(App->Div_Richtig, MUIA_Text_Contents, x);
                        }
                      else
                        {
                           ++falsch[1];
                           inttostr(falsch[1],x);
                           set(App->Div_Falsch, MUIA_Text_Contents, x);
                           set(App->Div_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
                           inttostr(erg,x);
                           set(App->Div_Erg, MUIA_Text_Contents, x);
                        }
                      auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
                      inttostr(prozm,x);
                      set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
                      set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
                      set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
                      set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
                      set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
                      inttostr(gesr,x);
                      set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
                      inttostr(gesf,x);
                      set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
                      --i;
                      if(i)
                        {
                           set(App->Erg_Div, MUIA_String_Contents, "");
                           inttostr(j,z);
                           set(App->Div_Nr, MUIA_Text_Contents, z);
                           erg=Division(str);
                           set(App->Aufg_Div, MUIA_Text_Contents, str);
                        }
                      else
                        {
                           set(App->Aufg_Div, MUIA_Text_Contents, GetMBString(MSG_Ready));
                           set(App->Erg_Div, MUIA_Text_Contents, "");
                        }
                     }
                    break;
                    case ANZ_ADDI:
                    get(App->Anz_Addi, MUIA_String_Contents, &mal);
                    
                    if (atoi(mal) != 0 )
                       {
                        erg=Addition(str);
                        set(App->Aufg_Addi, MUIA_Text_Contents, str);
                        set(App->Addi_Nr, MUIA_Text_Contents, "1");
                        i=atoi(mal);
                        j=1;
                       }
                    else
                       {
                        set(App->Aufg_Addi, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                        set(App->Addi_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                       }
                    break;
                case ERG_ADDI:
                    get(App->Erg_Addi, MUIA_String_Contents, &erga);
                    set(App->Addi_Erg, MUIA_Text_Contents, "");
                    if(i)
                     {
                      ++j;
                      if (erg == atoi(erga))
                        {
                           set(App->Addi_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
                           ++richtig[2];
                           inttostr(richtig[2],x);
                           set(App->Addi_Richtig, MUIA_Text_Contents, x);
                        }
                      else
                        {
                           ++falsch[2];
                           inttostr(falsch[2],x);
                           set(App->Addi_Falsch, MUIA_Text_Contents, x);
                           set(App->Addi_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
                           inttostr(erg,x);
                           set(App->Addi_Erg, MUIA_Text_Contents, x);
                        }
                      auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
                      inttostr(prozm,x);
                      set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
                      set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
                      set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
                      set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
                      set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
                      inttostr(gesr,x);
                      set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
                      inttostr(gesf,x);
                      set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
                      --i;
                      if(i)
                        {
                           set(App->Erg_Addi, MUIA_String_Contents, "");
                           inttostr(j,z);
                           set(App->Addi_Nr, MUIA_Text_Contents, z);
                           erg=Addition(str);
                           set(App->Aufg_Addi, MUIA_Text_Contents, str);
                        }
                      else
                        {
                           set(App->Aufg_Addi, MUIA_Text_Contents,GetMBString(MSG_Ready));
                           set(App->Erg_Addi, MUIA_Text_Contents, "");
                        }
                     }
                    break;
                    case ANZ_SUBTR:
                    get(App->Anz_Subtr, MUIA_String_Contents, &mal);
                    
                    if (atoi(mal) != 0 )
                       {
                        erg=Subtraktion(str);
                        set(App->Aufg_Subtr, MUIA_Text_Contents, str);
                        set(App->Subtr_Nr, MUIA_Text_Contents, "1");
                        i=atoi(mal);
                        j=1;
                       }
                    else
                       {
                        set(App->Aufg_Subtr, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                        set(App->Subtr_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Rubish));
                       }
                    break;                                      
                case ERG_SUBTR:
                    get(App->Erg_Subtr, MUIA_String_Contents, &erga);
                    set(App->Subtr_Erg, MUIA_Text_Contents, "");
                    if(i)
                     {
                      ++j;
                      if (erg == atoi(erga))
                        {
                           set(App->Subtr_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Right));
                           ++richtig[3];
                           inttostr(richtig[3],x);
                           set(App->Subtr_Richtig, MUIA_Text_Contents, x);
                        }
                      else
                        {
                           ++falsch[3];
                           inttostr(falsch[3],x);
                           set(App->Subtr_Falsch, MUIA_Text_Contents, x);
                           set(App->Subtr_Erg_tex, MUIA_Text_Contents,GetMBString(MSG_Wrong));
                           inttostr(erg,x);
                           set(App->Subtr_Erg, MUIA_Text_Contents, x);
                        }
                      auswertung(richtig,falsch,&prozg,&prozm,&prozd,&proza,&prozs,&gesr,&gesf);
                      inttostr(prozm,x);
                      set(App->Gauge_Mul, MUIA_Gauge_Current, prozm);
                      set(App->Gauge_Div, MUIA_Gauge_Current, prozd);
                      set(App->Gauge_Addi, MUIA_Gauge_Current, proza);
                      set(App->Gauge_Subtr, MUIA_Gauge_Current, prozs);
                      set(App->Gauge_Ges, MUIA_Gauge_Current, prozg);
                      inttostr(gesr,x);
                      set(App->Gesammt_Richtig, MUIA_Text_Contents, x);
                      inttostr(gesf,x);
                      set(App->Gesammt_Falsch, MUIA_Text_Contents, x);
                      --i;
                      if(i)
                        {
                           set(App->Erg_Subtr, MUIA_String_Contents, "");
                           inttostr(j,z);
                           set(App->Subtr_Nr, MUIA_Text_Contents, z);
                           erg=Subtraktion(str);
                           set(App->Aufg_Subtr, MUIA_Text_Contents, str);
                        }
                      else
                        {
                           set(App->Aufg_Subtr, MUIA_Text_Contents,GetMBString(MSG_Ready));
                           set(App->Erg_Subtr, MUIA_Text_Contents, "");
                        }
                     }
                    break;
                case LADEN:     Dateilesen(richtig,falsch);
                    break;
                case SPEICHERN: Dateischreiben(richtig,falsch);
                    break;
                }
        if (running && signal) Wait(signal);
        }
        DisposeApp(App);
        end();
}

void auswertung(int ri[5], int fa[5], int *prozg, int *prom, int *prod, int *proa, int *pros, int *ria, int *faa )
{
    int pr, i;
    *ria=0;
    *faa=0;
    for (i=0;i<4;++i)
        {
           *ria=*ria+ri[i];
           *faa=*faa+fa[i];
        }
     pr=(*ria)+(*faa);
     if(pr)
     {
      *prozg=(int)((float(*ria)/float(pr))*100);
      //Mul
      if(ri[0]||fa[0])
       *prom=(int)((float(ri[0])/(float(ri[0])+float(fa[0])))*100);
      //Div
      if(ri[1]||fa[1])
       *prod=(int)((float(ri[1])/(float(ri[1])+float(fa[1])))*100);
      //Addi
      if(ri[2]||fa[2])
       *proa=(int)((float(ri[2])/(float(ri[2])+float(fa[2])))*100);
      //Subtr
      if(ri[3]||fa[3])
       *pros=(int)((float(ri[3])/(float(ri[3])+float(fa[3])))*100);
      }
     else
       *prozg=0
}
int Multiplikation(char str[12])
{
       int f1,f2,erg,g1=50,g2=50;
       char f11[6],f22[6],f33[12]="";
                    do
                     {
                        Zufall(&f1,&f2,g1,g2);
                        erg=f1*f2;
                     }
                    while(erg>100||f1==0||f2==0);
                    inttostr(f1,f11);
                    inttostr(f2,f22);
                    strcat(f33,f11);
                    strcat(f33," * ");
                    strcat(f33,f22);
                    strcpy(str,f33);
                    return(erg);
}
int Division(char str[12])
{
       int f1,f2,erg,g1=800,g2=50,tmp;
       char f11[6],f22[6],f33[12]="";
                do
                 {
                   Zufall(&f1,&f2,g1,g2);
                   tmp=f1/f2;
                   f1=tmp*f2;
                   erg=f1/f2;
                 }
                while (erg<=1||f1<=f2||erg>100||f2==0);
                inttostr(f1,f11);
                inttostr(f2,f22);
                strcat(f33,f11);
                strcat(f33," : ");
                strcat(f33,f22);
                strcpy(str,f33);
                return(erg);
}
int Addition(char str[12])
{
       int f1,f2,erg,g1=50,g2=50;
       char f11[6],f22[6],f33[6]="";
                    do
                     {
                        Zufall(&f1,&f2,g1,g2);
                        erg=f1+f2;
                     }
                    while(erg>100);
                    inttostr(f1,f11);
                    inttostr(f2,f22);
                    strcat(f33,f11);
                    strcat(f33," + ");
                    strcat(f33,f22);
                    strcpy(str,f33);
                    return(erg);
}
int Subtraktion(char str[12])
{
       int f1,f2,erg,g1=150,g2=150;
       char f11[6],f22[6],f33[12]="";
                    do
                     {
                        Zufall(&f1,&f2,g1,g2);
                        erg=f1-f2;
                     }
                    while (erg>100||f1<f2||erg<=0);
                    inttostr(f1,f11);
                    inttostr(f2,f22);
                    strcat(f33,f11);
                    strcat(f33," - ");
                    strcat(f33,f22);
                    strcpy(str,f33);
                    return(erg);
}
/* ----------------------------------- init ------------------------------------

 Kommentar: Zum öffnen der benötigten Libraries und des Katalogs

*/
void init(void)
{
        if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
        {
                printf("Can't Open MUIMaster Library\n");
                exit(20);
        }
        if (!(LocaleBase = OpenLibrary("locale.library",38)))
        {
                printf("Can't Open Locale Library\n");
                printf("Built-in Language will be used !!!\n");
        }
        OpenAppCatalog(NULL,NULL);
}

/* ------------------------------------ end ------------------------------------

 Kommentar: Ende zum schließen der geöffneten Libraries und des Katalogs

*/
void end(void)
{
        CloseAppCatalog();
        if (LocaleBase)
                CloseLibrary(LocaleBase);
        CloseLibrary(MUIMasterBase);
        exit(0);
}

void Zufall(int *z1, int *z2, int gr1, int gr2)
{
        do
         {
            *z1=Random(gr1);
            *z2=Random(gr2);
         }
        while(*z1<=1||*z2<=1);
        return;
}

