//---------------------------------------------------------------------------
#include "GUI.h"
#include <iostream>
#pragma hdrstop
#include "complex.h"
#include <stdio.h>
#include <math.h>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
//---------------------------------------------------------------------------
#pragma package(smart_init)
int invoercomplex, invoerj, invoercomplex2, invoerj2, invoermodulus, invoermodulus2, invoerhoek, invoerhoek2; //algemene (lokale) objecten, die de invoer moeten
char functie;
//lidfuncties
void Complex::rekenUit()
{
//beveiliging tegen de mogelijkheid dat er met een letter gerekend gaat worden.
if((Layout->a_complex->Text == "a" || Layout->b_complex->Text == "+/- b") || (Layout->Modulus->Text == "Mod." || Layout->Hoek_cartetisch->Text == "Hoek"))
{
Layout->Uitvoer_complex->Text = 0;
Layout->Uitvoer_cartesisch->Text = 0;
Layout->a_complex->Text = 0;
Layout->b_complex->Text = 0;
Layout->Modulus->Text = 0;
Layout->Hoek_cartetisch->Text = 0;
Layout->Uitvoer_b->Text = 0;
Layout->Uitvoer_hoek->Text = 0;
maakBufferleeg();
}
else
{
//rekenoperators bekijken
if(Layout->plus->Font->Color == clLime)
{
functie = '+';
}
if(Layout->min->Font->Color == clLime)
{
functie = '-';
}
if(Layout->maal->Font->Color == clLime)
{
functie = '*';
}
if(Layout->delen->Font->Color == clLime)
{
functie = '/';
}
//------------------inlezen oude waardes----------------------------------------
ifstream leesuit("buffer.txt");
leesuit >> invoercomplex;
leesuit >> invoerj;
leesuit >> invoermodulus;
leesuit >> invoerhoek;
leesuit.close();
//-----------------Iets ingevuld, maar nog geen antwoord? Dan waarde opslaan----
if( ((Layout->operation->ItemIndex == 0 || Layout->operation->ItemIndex == 2 ) && Layout->Uitvoer_complex->Text == 0) || ((Layout->operation->ItemIndex == 1 || Layout->operation->ItemIndex == 3) && Layout->Uitvoer_cartesisch->Text == 0))
{
waardesOpslaan(0);
}
//---------------'HUIDIGE' waardes ophalen vanaf scherm-------------------------
invoercomplex2 = StrToInt(Layout->a_complex->Text);
invoerj2 = StrToInt(Layout->b_complex->Text);
invoermodulus2 = StrToInt(Layout->Modulus->Text);
invoerhoek2 = StrToInt(Layout->Hoek_cartetisch->Text);
//als de complexreken manier (alleen complex rekenen) is aangevinkt
if(Layout->isbutton->Font->Color == clRed)
{
//0 = complex -> complex
//1 = Polair -> polair
//2 = complex -> polair
//3 = polair->complex
if(Layout->operation->ItemIndex == 0)
{
//afhandelen van operations, als er álleen complex gerekend moet worden
switch(functie)
{
int antwoord;
int hoek;
case '+' : antwoord = (invoercomplex + invoercomplex2);
hoek = (invoerj + invoerj2);
Layout->Uitvoer_complex->Text = IntToStr(antwoord);
Layout->Uitvoer_b->Text = IntToStr(hoek);
waardesOpslaan(1);
break;
case '-' : antwoord = ((invoercomplex) - (invoercomplex2));
hoek = ((invoerj) - (invoerj2));
Layout->Uitvoer_complex->Text = IntToStr(antwoord);
Layout->Uitvoer_b->Text = IntToStr(hoek);
waardesOpslaan(1);
break;
case '*' : {
int modulus1 = geefModulus(invoercomplex, invoerj); //werkt
int modulus2 = geefModulus(invoercomplex2, invoerj2); //werkt
int hoek1 = geefArgument(invoercomplex, invoerj);
int hoek2 = geefArgument(invoercomplex2, invoerj2);
int modulustotaal = (modulus1 * modulus2);
int hoektotaal = (hoek1 + hoek2);
//dan weer terug naar cartesiaans
antwoord = geefA(modulustotaal, hoektotaal);
hoek = geefB(modulustotaal, hoektotaal);
Layout->Uitvoer_complex->Text = IntToStr(hoek1);
Layout->Uitvoer_b->Text = IntToStr(hoek);
waardesOpslaan(1);
break;
}
case '/' :
break;
}
}
if(Layout->operation->ItemIndex == 1) //polair->polair
{
//afhandelen van operations, als er álleen complex gerekend moet worden
switch(functie)
{
int antwoord;
int hoek;
case '+' :
waardesOpslaan(1);
break;
case '-' :
waardesOpslaan(1);
break;
case '*' : {
antwoord = invoermodulus * invoermodulus2;
hoek = invoerhoek + invoerhoek2;
Layout->Uitvoer_cartesisch->Text = IntToStr(antwoord);
Layout->Uitvoer_hoek->Text = IntToStr(hoek);
waardesOpslaan(1);
break;
}
case '/' : antwoord = invoermodulus / invoermodulus2;
hoek = invoerhoek - invoerhoek2;
Layout->Uitvoer_cartesisch->Text = IntToStr(antwoord);
Layout->Uitvoer_hoek->Text = IntToStr(hoek);
waardesOpslaan(1);
break;
}
} //einde polair->polair
if(Layout->operation->ItemIndex == 2) //complex->polair
{
//afhandelen van operations, als er álleen complex gerekend moet worden
switch(functie)
{
int antwoord;
int hoek;
case '+' :
waardesOpslaan(1);
break;
case '-' :
waardesOpslaan(1);
break;
case '*' : {
antwoord = invoermodulus * invoermodulus2;
hoek = invoerhoek + invoerhoek2;
Layout->Uitvoer_cartesisch->Text = IntToStr(antwoord);
Layout->Uitvoer_hoek->Text = IntToStr(hoek);
waardesOpslaan(1);
break;
}
case '/' : antwoord = invoermodulus / invoermodulus2;
hoek = invoerhoek - invoerhoek2;
Layout->Uitvoer_cartesisch->Text = IntToStr(antwoord);
Layout->Uitvoer_hoek->Text = IntToStr(hoek);
waardesOpslaan(1);
break;
}
} //einde polair->polair
}
}
}
void Complex::waardesOpslaan(int opslaanals) //voor een langere tijd
{
if(opslaanals != 1) //dus moet invoerwaardes opslaan
{
//opslaan (als er een cijfer ingevuld is)
if((Layout->a_complex->Text != "a" && Layout->b_complex->Text != "+/- b") && (Layout->Modulus->Text != "Mod." && Layout->Hoek_cartetisch->Text != "Hoek"))
{
if(Layout->plus->Font->Color == clLime || Layout->min->Font->Color == clLime || Layout->maal->Font->Color == clLime || Layout->delen->Font->Color == clLime)
{
ofstream buffer("buffer.txt");
invoercomplex2 = StrToInt(Layout->a_complex->Text);
invoerj2 = StrToInt(Layout->b_complex->Text);
invoermodulus2 = StrToInt(Layout->Modulus->Text);
invoerhoek2 = StrToInt(Layout->Hoek_cartetisch->Text);
buffer << invoercomplex2 << endl;
buffer << invoerj2 << endl;
buffer << invoermodulus2 << endl;
buffer << invoerhoek2 << endl;
buffer.close();
}
}
else //als geen van de situaties hierboven juist is, dus foute invoer
{
Layout->Uitvoer_complex->Text = "Verkeerde invoer";
Layout->Uitvoer_complex->Font->Color = clRed;
}
}
if(opslaanals == 1)
{
ofstream buffer("buffer.txt");
invoercomplex2 = StrToInt(Layout->Uitvoer_complex->Text);
invoerj2 = StrToInt(Layout->Uitvoer_b->Text);
invoermodulus2 = StrToInt(Layout->Uitvoer_cartesisch->Text);
invoerhoek2 = StrToInt(Layout->Uitvoer_hoek->Text);
buffer << invoercomplex2 << endl;
buffer << invoerj2 << endl;
buffer << invoermodulus2 << endl;
buffer << invoerhoek2 << endl;
buffer.close();
}
}
//----------------------OMREKENFUNCTIES-----------------------------------------
//reken cartesisch naar polair of omgekeerd
float Complex::geefModulus(int a, int b) //werkt
{
return sqrt(((a*a) + (b*b)));
}
float Complex::geefArgument(int a, int b) //werkt nog niet
{
return (atan2(b,a)); // arctan(b/a)
}
float Complex::geefA(int modulus, int argument) //werkt niet
{
return modulus*cos(argument);
}
float Complex::geefB(int modulus, int argument) //werkt nog niet
{
return modulus*sin(argument);
}
//----------------WISSEN VAN WAARDES--------------------------------------------
//om alle variabelen te wissen nadat een rekenfunctie is uitgevoerd dient onderstaande functie gebruikt te worden
//deze moet nog even aangepast worden, werkt nog niet...
void Complex::maakBufferleeg()
{
ofstream buffer("buffer.txt", ios::trunc);
}
//setters
void Complex::setinvoer_a(float a)
{
invoer_a = a;
}
void Complex::setinvoer_b(float b)
{
invoer_b = b;
}
void Complex::setinvoer_mod(float mod)
{
invoer_mod = mod;
}
void Complex::setinvoer_hoek(float hoek)
{
invoer_hoek = hoek;
}
void Complex::setoperator(float op)
{
operatorr = op;
}
//getters
int Complex::getinvoer_a()
{
return invoer_a;
}
int Complex::getinvoer_b()
{
return invoer_b;
}
int Complex::getinvoer_mod()
{
return invoer_mod;
}
int Complex::getinvoer_hoek()
{
return invoer_hoek;
}
int Complex::getoperator()
{
return operatorr;
}