Завдання для лабораторної роботи
На основі класу з попередньої лабораторної:
1. Перевантажити як мінімум три функції-члени з попереднього завдання.
2. Перевантажити операції згідно з варіантом .Для операцій, для яких не вказані символи, вибрати символи самостійно.
3. Створити дружні функції згідно з варіантом.
4. Створити статичні поля та статичні методи згідно з варіантом.
5. Продемонструвати розроблені можливості класу завдяки створеному віконному застосуванню.
Лабораторне завдання
Клас Complex – комплексне число.
Перевантажити операції, як дружні функції :
Додавання
Віднімання
Множення
Ділення
Піднесення до n-го степеня
Взяття кореня
Модуль
Перевантажити операції, як дружні-функції:
Введення комплексного числа з форми (²<<²)
Виведення комплексного числа на форму(²>>²)
Більше (²>²)
Менше (²<²)
Рівне (²==²) (при порівнянні порівнювати модулі комплексних чисел).
Код програми
class Complex {
public:
double real;
double image;
public:
void SetValue(TEdit*Real,TEdit*Image){
real=StrToFloat(Real->Text);
image=StrToFloat(Image->Text);
}
Complex (){
real=1;
image=1;
}
~Complex(){
real=0;
image=0;
}
Complex operator +(Complex c2){
Complex res;
res.real=real+c2.real;
res.image=image+c2.image;
return res;
}
Complex operator -(Complex c2){
Complex res;
res.real=real-c2.real;
res.image=image-c2.image;
return res;
}
Complex operator *(Complex c2){
Complex res;
res.real=real*c2.real-image*c2.image;
res.image=real*c2.image+image*c2.real;
return res;
}
Complex operator / (Complex c2){
Complex res;
if(c2.real*c2.real+c2.image*c2.image==0)
{ShowMessage ("Ділення на нуль неможливе!!");}
else{
res.real=(real*c2.real+image*c2.image)/(c2.real*c2.real+c2.image*c2.image);
res.image=(image*c2.real-real*c2.image)/(c2.real*c2.real+c2.image*c2.image);
res.real=SimpleRoundTo(res.real,-3);
res.image=SimpleRoundTo(res.image,-3);
}
return res;
}
friend bool operator >(Complex c1,Complex c2);
friend bool operator ==(Complex c1,Complex c2);
friend void operator <<(TEdit*tmp,Complex c1);
Complex Stepin(Complex c1,int stepin)
{ double r=sqrt(pow(c1.real,2)+pow(c1.image,2));
double i=acos(c1.real/(pow(c1.real,2)+pow(c1.image,2)));
this->real=pow(r,stepin)*cos(i*stepin);
this->image=pow(r,stepin)*sin(i*stepin);
this->real=SimpleRoundTo(this->real,-3);
this->image=SimpleRoundTo(this->image,-3);
}
Complex Korin(Complex c1,int korin){
double r=sqrt(pow(c1.real,2)+pow(c1.image,2));
double i=acos(c1.real/(pow(c1.real,2)+pow(c1.image,2)));
this->real=(pow(r,korin)/cos(i*korin)+2*3,14*r)/i;
this->image=(pow(r,korin)/cos(i*korin)+2*3,14*i)/r;
this->real=SimpleRoundTo(this->real,-3);
this->image=SimpleRoundTo(this->image,-3);
}
Complex Modul(Complex c1,TEdit*show){
show->Text=FloatToStr(sqrt(pow(c1.real,2)+pow(c1.image,2)));
}
double Mod(){
double result;
return result=sqrt(pow(real,2)+pow(image,2));
}
Complex TrigForm(Complex c,TEdit*show){
float r=sqrt(pow(c.real,2)+pow(c.image,2));
float i=acos(c.real/(pow(c.real,2)+pow(c.image,2)));
show->Text=" cos " + FloatToStr(r=SimpleRoundTo(r,-3)) + "+isin" + FloatToStr(i=SimpleRoundTo(i,-3));
}
void Show(TEdit*show){
AnsiString znak = "+";
if(FloatToStr(image)<0)
znak="";
show->Text=FloatToStr(real)+znak+"i"+FloatToStr(image);
}
};
bool operator >(Complex c1,Complex c2){
if(c1.Mod()>c2.Mod())return true;else return false;
}
bool operator ==(Complex c1,Complex c2){
if(c1.Mod()==c2.Mod())return true;else return false;
}
void operator <<(TEdit*tmp,Complex c1){
AnsiString znak="+";
if(FloatToStr(c1.image)<0)
znak="";
tmp->Text=FloatToStr(c1.real)+znak+"i"+FloatToStr(c1.image);
}
Complex First,Second,Third;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{First.SetValue(FirstReal,FirstImage);
Second.SetValue(SecondReal,SecondImage);
Edit3->Text=StrToInt(Edit3->Text)+ 1;
Third=First+Second;
Edit2<<Third;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit3->Text="3";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Second.SetValue(SecondReal,SecondImage);
Edit3->Text=StrToInt(Edit3->Text)+ 1;
Third=First-Second;
Edit2<<Third;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{First.SetValue(FirstReal,FirstImage);
Second.SetValue(SecondReal,SecondImage);
Edit3->Text=StrToInt(Edit3->Text)+ 1;
Third=First*Second;
Edit2<<Third;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Second.SetValue(SecondReal,SecondImage);
Edit3->Text=StrToInt(Edit3->Text)+ 1;
Third=First/Second;
Edit2<<Third;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Third.Stepin(First,StrToInt(Edit1->Text));
Edit2<<Third;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Third.Modul(First,Edit2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Third.TrigForm(First,Edit2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Second.SetValue(SecondReal,SecondImage);
if(First>Second)Edit2->Text="Перше";
else Edit2->Text="Друге";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Second.SetValue(SecondReal,SecondImage);
if(First == Second) Edit2->Text = "Так";
else Edit2->Text = "Ні";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{ First.SetValue(FirstReal,FirstImage);
Third.Korin(First,StrToInt(Edit4->Text));
Edit2<<Third;
}
//---------------------------------------------------------------------------
