Mam spory problem. Nie rozumiem czemu nam każą zdefiniować tyle operatorów. Ten program ma wczytywać i wypisywać liczby zespolone i robić na nich jakieś działania. Co do tych operatorów to nam kazał przeczytać to: https://stackoverflow.com/questions/7578682/why-should-i-overload-a-c-operator-as-a-global-function-stl-does-and-what-ar and
-
Muszę zrobić 5 operatorów jako funkcje składowe klasy, które przyjmują jeden argument: +, −, !, ++, −−.
-
Muszę zrobić 5 operatorów jako funkcje składowe klasy, które przyjmują dwaargumenty: =,+=, −=, *=, /=; Then
-
Muszę zrobić 8 operatorów jako globalne funkcje zaprzyjaźnione +, −, *, /, ==, !=, <<, >> które przyjmują dwa parametry. Zrobiłem je jako tako
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
A tu co mam zrobić, chyba to ma zwracać wartość bool no nie?
friend const Comp operator==(const Comp& x, const Comp& y)
{
}
friend const Comp operator!=(const Comp& x, const Comp& y)
{
}
No i nie wiem co mam wstawić w operatorach tych 1) 2). 1) przyjmuje tylko jeden argument, więc co mam tam napisać wewnątrz tej funkcji, że dodaje ten pierwszy argument z pierwszym argumentem czy co?. 2) Np. += - o co w tym chodzi dla mnie to bez sensu. To chyba po prostu będzie to samo co dodawnie w tych globalnych funkcjach zaprzyjaźnionych?
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
bool operator!(void);
const Comp& operator++();
const Comp operator++(int);
const Comp& operator--();
const Comp operator--(int);
Comp operator=(const Comp x);
Comp operator-=(const Comp& x);
Comp operator+=(const Comp& x);
Comp operator*=(const Comp& x);
Comp operator/=(const Comp& x);
Wyskoczył mi error, gdy nic tam do środka nie dawałem
/tmp/ccAWMNiS.o: In function `main':
complex.cpp:(.text+0x516): undefined reference to `Comp::operator+(Comp const&)'
complex.cpp:(.text+0x536): undefined reference to `Comp::operator=(Comp)'
complex.cpp:(.text+0x5b2): undefined reference to `Comp::operator-(Comp const&)'
complex.cpp:(.text+0x5d2): undefined reference to `Comp::operator=(Comp)'
complex.cpp:(.text+0x66e): undefined reference to `Comp::operator=(Comp)'
complex.cpp:(.text+0x70a): undefined reference to `Comp::operator=(Comp)'
collect2: error: ld returned 1 exit status
Mam też problem z obliczaniem sprzężenia. Jak mogę to policzyć, próbowałem tak, ale mi wychodzi 0,0 zamiast np. rzecz i -urojona
const Comp conj(void) const
{
Comp temp;
-im();
return temp;
}
Oto mój cały kod. Później go rozbiję na osobny plik h, osobny plik z klasą i osbny tam gdzie main.
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;
class Comp {
double real, imag;
public:
Comp(){
real;
imag;
}
double re(void) const
{
return real;
}
double im(void) const
{
return imag;
}
double mod(void) const
{
return sqrt(re()*re() + im()*im());
}
double arg(void) const
{
double faza;
if (im() >= 0)
faza = acos(re()/mod());
else
faza = 2*M_PI - acos(re()/mod());
return faza;
}
const Comp conj(void) const
{
Comp temp;
-im();
return temp;
}
~Comp(){}
/*
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
bool operator!(void);
const Comp& operator++();
const Comp operator++(int);
const Comp& operator--();
const Comp operator--(int);
Comp operator=(const Comp x);
Comp operator-=(const Comp& x);
Comp operator+=(const Comp& x);
Comp operator*=(const Comp& x);
Comp operator/=(const Comp& x);
*/
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
friend const Comp operator==(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator!=(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend std::ostream& operator<<(std::ostream& wart1, const Comp& a)
{
return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
}
friend std::istream& operator>>(std::istream& wart2, Comp& b){
char c;
return wart2>>c>>b.real>>c>>b.imag>>c;
}
};
int main(int argc, char* argv[])
{
ifstream read(argv[1]);
if (!read)
{ cerr << "Open error: " << argv[1] << endl; exit(1);}
ofstream write(argv[2]);
if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);}
read.clear();
read.seekg(0);
Comp x1;
read >> x1;
write << x1;
cout << x1;
Comp x2;
read >> x2;
write << x2;
cout << x2;
cout << x1.mod() << endl;
cout << x2.mod() << endl;
cout << x1.arg() << endl;
cout << x2.arg() << endl;
cout << x1.conj();
cout << x2.conj();
write << x2;
write << x1.mod() << endl;
write << x2.mod() << endl;
write << x1.arg() << endl;
write << x2.arg() << endl;
write << x1.conj();
write << x2.conj();
Comp sum;
sum = x1 + x2;
cout << sum;
write << sum;
Comp sub;
sub = x1 - x2;
cout << sub;
write << sub;
Comp mult;
mult = x1 * x2;
cout << mult;
write << mult;
Comp div;
div = x1 / x2;
cout << div;
write << div;
return 0;
}