Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
2
Добавлен:
30.05.2020
Размер:
2.01 Mб
Скачать

Додаток б Лістинг модуля CalculateFrac

#include "Math.h"

class CalculateFrac{

public:

void AddFrac(int a, int b, int c, int d)

{

int zero=0;

int e=b*d;

int g=a*d+c*b;

if (g==0)

{

zero=1;

this->a=0;

}

else

{

if(abs(g)>=e)

{

this->a=g/e;

g=abs(g-=this->a*e);

if (g==0)

zero=1;

}

else

this->a=0;

}

if(zero==0)

{

this->p=(g/this->nod(abs(g),abs(e)));

this->q=(e/this->nod(abs(g),abs(e)));

}

else

{

this->p=0;

this->q=1;

}

}

void SubFrac(int a, int b, int c, int d)

{

int zero=0;

int e=b*d;

int g=a*d-c*b;

if (g==0)

{

zero=1;

this->a=0;

}

else

{

if(abs(g)>=e)

{

this->a=g/e;

g=abs(g-=this->a*e);

if (g==0)

zero=1;

}

else

this->a=0;

}

if(zero==0)

{

this->p=(g/this->nod(abs(g),abs(e)));

this->q=(e/this->nod(abs(g),abs(e)));

}

else

{this->p=0; this->q=1; }

}

void MultFrac(int a, int b, int c, int d)

{

int zero=0;

int g=a*c;

int e=b*d;

if (g==0)

{

zero=1;

this->a=0;

}

else

{

if(abs(g)>=e)

{this->a=g/e;g=abs(g-=this->a*e);if (g==0) zero=1;}

Else this->a=0;

}

if(zero==0)

{this->p=(g/this->nod(abs(g),abs(e)));

this->q=(e/this->nod(abs(g),abs(e)));}

else

{this->p=0; this->q=1;}

}

void DivFrac(int a, int b, int c, int d)

{

int zero=0;

int g=a*d;

int e=b*c;

if (g==0)

{

zero=1;

this->a=0;

}

else

{

if(abs(g)>=e)

{this->a=g/e; g=abs(g-=this->a*e);

if (g==0) zero=1; }

else this->a=0;

}

if(zero==0)

{this->p=(g/this->nod(abs(g),abs(e)));this->q=(e/this->nod(abs(g),abs(e))); }

else

{this->p=0; this->q=1; }

}

void PowFrac(int a, int b, int n)

{

MultFrac(a,b,a,b);

for(int i=1;i<n-1;i++)

MultFrac(this->p,this->q,a,b);

}

int RivnFrac(int a, int b, int c, int d)

{

int g=b*d;

int e=a*d;

int f=c*b;

if (e==f)

return 1;

else

return 0;

}

int CheckFrac(int a, int b, int c, int d)

{

int g=b*d;

int e=a*d;

int f=c*b;

if (e==f)

return 1;

else

if(e>f)

return 2;

else

return 3;

}

int GetP()

{

return this->p;

}

int GetQ()

{

return this->q;

}

int GetA()

{

return this->a;

}

private:

int p,q,a;

int nod(int x,int y)

{

while(x!=y)

{

if(x<y)

y-=x;

else

x-=y;

}

return x;

}

};

Додаток в Лістинг модуля AddUnit

#include <vcl.h>

#pragma hdrstop

#include "AddUnit.h"

#include "FracUnit.h"

#include "CalculateFrac.h"

#include "Math.h"

//---------------------------------------------------------------------------

void __fastcall TSumForm::Button2Click(TObject *Sender)

{Label3->Hide(); Label6->Hide();

Edit5->Hide(); Edit6->Hide(); Edit7->Hide();

Edit1->Clear(); Edit2->Clear(); Edit3->Clear(); Edit4->Clear(); Edit5->Clear();

Edit6->Clear(); Edit7->Clear();

Button1->Enabled=true;

//---------------------------------------------------------------------------

void __fastcall TSumForm::Button3Click(TObject *Sender)

{ SumForm->Close();

MainForm->Show();}

//---------------------------------------------------------------------------

void __fastcall TSumForm::Button1Click(TObject *Sender)

{ int a,b,c,d;

if(((Edit1->Text).Length()==0)||((Edit2->Text).Length()==0)||

((Edit3->Text).Length()==0)||((Edit4->Text).Length()==0))

{

ShowMessage("Заповніть всі поля!");

return;

}

a=StrToInt(Edit1->Text);

b=StrToInt(Edit2->Text);

c=StrToInt(Edit3->Text);

d=StrToInt(Edit4->Text);

if((d==0)||(b==0))

{

ShowMessage("Ділення на нуль!!");

return;

}

Button1->Enabled=false; Label3->Show();

CalculateFrac *Calculate=new CalculateFrac;

Calculate->AddFrac(a,b,c,d);

if(Calculate->GetA()!=0)

{

Edit5->Show();

Edit5->Text=IntToStr(Calculate->GetA());

}

if(Calculate->GetP()!=0)

{

Edit6->Show(); Edit7->Show(); Label6->Show();

Edit6->Text=IntToStr(Calculate->GetP());

Edit7->Text=IntToStr(Calculate->GetQ());

}

if((Calculate->GetA()==0)&&(Calculate->GetP()==0))

{

Edit5->Show();

Edit5->Text="0";

}

}

Соседние файлы в папке Звіт