Добавил:
masterdos
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Курсовая работа / KURSOVA_ROBOTA / Дроби / CalculateFrac
.h#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;
}
};
Соседние файлы в папке Дроби