Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------------ COMPLEX.H ------------------------------- //
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Complex {
- private:
- double Re, Im;
- public:
- Complex() : Re(0.0), Im(0.0) {} // konstruktor bezargumentowy
- Complex(double Re) : Re(Re), Im(0.0) {} // jaks nowy konstruktor
- Complex(double Re, double Im) : Re(Re), Im(Im) {} // konstruktor z dwoma argumentami
- double getRe() const { return Re; } // pobeiranie czesci rzeczywistej
- double getIm() const { return Im; } // poebiranie czesci urojonej
- double getAmp() const { return sqrt( (Re*Re) + (Im*Im) ); }
- double getPhase();
- friend Complex operator-(const Complex&, const Complex&); // odejmowanie
- friend Complex operator*(const Complex&, const Complex&); // mnozenie
- friend Complex operator/(const Complex&, const Complex&); // dzielenie
- Complex & operator+=(Complex); // dodawanie 2
- Complex & operator-=(Complex); // dodawanie 2
- Complex & operator*=(Complex); // dodawanie 2
- Complex & operator/=(Complex); // dodawanie 2
- friend bool operator ==(const Complex&, const Complex&); // porownywanie
- friend ostream& operator<<(ostream&, const Complex&); // wyswietlanie
- /* "friend" oznacza tutaj, ze dana jakaś klasa/funkcja potrzebuje dostępu do
- * prywatnych lub chronionych składowych klasy. Aby zezwolić na dostęp,
- * używa się deklaracji przyjaźni miedzy klasą a inną klasą lub funkcją. */
- };
- Complex operator+(const Complex&, const Complex&); // dodawanie
- // ------------------------------ COMPLEX.CPP ------------------------------- //
- #include <ostream>
- #include "complex.h"
- #include <cmath>
- #define PI 3.14159265359
- using namespace std;
- double Complex::getPhase(){
- Complex z1(Complex::getRe(), Complex::getIm());
- if (z1.getRe() < 0)
- return atan( z1.getIm() / z1.getRe() ) + PI;
- else if (z1.getRe() > 0)
- return atan( z1.getIm() / z1.getRe() );
- else {
- if (z1.getIm() > 0)
- return PI/2;
- else if (z1.getIm() < 0)
- return -PI/2;
- else return 0;
- }
- }
- Complex operator+(const Complex& a, const Complex& b) {
- double r, i;
- r = a.getRe() + b.getRe();
- i = a.getIm() + b.getIm();
- return Complex(r, i);
- }
- Complex operator-(const Complex& a, const Complex& b) {
- double r, i;
- r = a.getRe() - b.getRe();
- i = a.getIm() - b.getIm();
- return Complex(r, i);
- }
- Complex operator*(const Complex& z1, const Complex& z2) {
- double r, i;
- double a = z1.getRe(), b = z1.getIm(), c = z2.getRe(), d = z2.getIm();
- r = (a*c) - (b*d);
- i = (a*d) + (b*c);
- return Complex(r, i);
- }
- Complex operator/(const Complex& z1, const Complex& z2) {
- double r, i;
- double a = z1.getRe(), b = z1.getIm(), c = z2.getRe(), d = z2.getIm();
- r = ( (a*c) + (b*d) ) / ( (c*c) + (d*d) );
- i = ( (b*c) - (a*d) ) / ( (c*c) + (d*d) );
- return Complex(r, i);
- }
- Complex & Complex::operator+=(Complex z1){
- Re += z1.getRe();
- Im += z1.getIm();
- return * this;
- }
- Complex & Complex::operator-=(Complex z1){
- Re -= z1.getRe();
- Im -= z1.getIm();
- return * this;
- }
- Complex & Complex::operator*=(Complex z1){
- Re *= z1.getRe();
- Im *= z1.getIm();
- return * this;
- }
- Complex & Complex::operator/=(Complex z1){
- *this = *this / z1;
- return * this;
- }
- bool operator ==(const Complex& z1, const Complex& z2){
- if ( (z1.getRe() == z2.getRe()) && ( z1.getIm() == z2.getIm()) )
- return true;
- else return false;
- }
- ostream& operator<<(ostream& out, const Complex &a) {
- out << " " << a.getRe() << " + " << a.getIm() << "i ";
- return out;
- }
- // ------------------------------ MAIN.CPP ------------------------------- //
- #include <iostream>
- #include "complex.h"
- #define PI 3.14159265359
- int main() {
- Complex a(1.8, 2.3), b(4.5, -4.1), a1(1.8, 2.3), a2(1.8, 2.3) , a3(1.8, 2.3),suma, rozn, mnoz, dziel;
- bool rown, rown2;
- suma = a+b;
- rozn = a-b;
- mnoz = a*b;
- dziel = a/b;
- rown = a==b;
- rown2 = a==a1;
- cout << "\t\t" << "a" << "\t\t" << "b" << "\t\t" << "c" << endl;
- cout << "\t\t" << a << "\t" << b << "\t" << a1 << endl << endl;
- cout << "Real = \t" << a.getRe() << "\t\t" << b.getRe() << "\t\t" << a1.getRe() << endl;
- cout << "Imaginary = \t" << a.getIm() << "\t\t" << b.getIm() << "\t\t" << a1.getIm() << endl;
- cout << "Amplitude = \t" << a.getAmp() << "\t\t" << b.getAmp() << "\t\t" << a1.getAmp() << endl;
- cout << "Phase (deg) = \t" << a.getPhase()* 180 / PI<< "\t\t" << b.getPhase() * 180 / PI << "\t" << a1.getPhase() * 180 / PI << endl << endl;
- cout << "Suma (a+b) \t= \t" << suma << endl;
- a+=b;
- cout << "Suma2 (a+=b) \t= \t" << a << endl;
- cout << "Roznica (a-b) \t= \t" << rozn << endl;
- a1-=b;
- cout << "roznica (a-=b) \t= \t" << a1 << endl;
- cout << "Iloczyn (a*b) \t= \t" << mnoz << endl;
- a2*=b;
- cout << "iloczyn (a*=b) \t= \t" << a2 << endl;
- cout << "Iloraz (a/b) \t= \t" << dziel << endl;
- cout << "Rownosc (a=b) \t= \t" << rown << endl;
- cout << "Rownosc (a=c) \t= \t" << rown2 << endl << endl;
- cout << "10*b \t= \t " << 10*b << endl;
- cout << "b*10 \t= \t " << b*10 << endl;
- a3 /= b;
- cout << "Iloraz (a/=b) \t= \t" << a3 << endl;
- a+= b+= a1;
- cout << "a += b += c \t= \t" << a << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement