Informatics3-2024/Solutions6
A MathWikiből
(Változatok közti eltérés)
Kkovacs (vitalap | szerkesztései)
(Új oldal, tartalma: „== Solutions == Only the solutions we discussed on the practical will be here. All the other tasks are perfect to practice for the written exam. ==== Complex with ope…”)
Újabb szerkesztés →
(Új oldal, tartalma: „== Solutions == Only the solutions we discussed on the practical will be here. All the other tasks are perfect to practice for the written exam. ==== Complex with ope…”)
Újabb szerkesztés →
A lap 2024. április 23., 17:25-kori változata
Solutions
Only the solutions we discussed on the practical will be here. All the other tasks are perfect to practice for the written exam.
Complex with operators
#include<iostream> #include<cmath> using namespace std; class Complex { private: float re; float im; public: Complex(); Complex(const Complex& other); Complex(float r); Complex(float r, float i); Complex operator+(Complex other); Complex operator*(Complex other); float abs(); void print(); friend ostream& operator<<(ostream& os, Complex& c); ~Complex(); }; Complex::Complex() { re = 0; im = 0; } Complex::Complex(const Complex& other) { re = other.re; im = other.im; } Complex::Complex(float r) { re = r; im = 0; } Complex::Complex(float r, float i) { re = r; im = i; } Complex Complex::operator+(Complex other) { return Complex(this->re + other.re, this->im + other.im); } Complex Complex::operator*(Complex other) { float real = this->re * other.re - this->im * other.im; float imag = this->re * other.im + this->im * other.re; return Complex(real, imag); } float Complex::abs() { return sqrt(this->re * this->re + this->im * this->im); } ostream& operator<<(ostream& os, Complex& c) { os << c.re << " + " << c.im << "i"; return os; } Complex::~Complex() { } int main(void) { Complex a; Complex b = Complex(1,2); Complex c = a * b; Complex d = b + b + b; Complex e = b * d; //Complex e = b.operator*(d); // 5 / 6 / 7; // /(/(5, 6), 7); cout << "a: " << a << endl; cout << "b: " << b << endl; cout << "c: " << c << endl; cout << "d: " << d << endl; cout << "e: " << e << endl; //cout << (c + d) << endl; cout << b.abs() << endl; return 0; }