Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct ComplexNumber
- {
- double real = 0;
- double imaginary = 0;
- };
- ComplexNumber init(double re, double im)
- {
- ComplexNumber number = { re, im };
- return number;
- }
- void print(const ComplexNumber& complex)
- {
- std::cout << "Complex number (" << complex.real << ", " << complex.imaginary << "):\n";
- std::cout << "Real part: " << complex.real << std::endl;;
- std::cout << "Imaginary part: " << complex.imaginary << std::endl;
- }
- ComplexNumber conjugate(const ComplexNumber& complex)
- {
- ComplexNumber conjugateComplex = { complex.real, -complex.imaginary };
- return conjugateComplex;
- }
- ComplexNumber add(const ComplexNumber& lhs, const ComplexNumber& rhs)
- {
- ComplexNumber complex = { lhs.real + rhs.real, lhs.imaginary + rhs.imaginary };
- return complex;
- }
- ComplexNumber subratct(const ComplexNumber& lhs, const ComplexNumber& rhs)
- {
- ComplexNumber complex = { lhs.real - rhs.real, lhs.imaginary - rhs.imaginary };
- return complex;
- }
- ComplexNumber multiply(const ComplexNumber& lhs, const ComplexNumber& rhs)
- {
- double real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary;
- double imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real;
- ComplexNumber complex = { real, imaginary };
- return complex;
- }
- ComplexNumber divide(const ComplexNumber& lhs, const ComplexNumber& rhs)
- {
- double denominator = (rhs.real * rhs.real + rhs.imaginary * rhs.imaginary);
- double real = (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / denominator;
- double imaginary = (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / denominator;
- ComplexNumber complex = { real, imaginary };
- return complex;
- }
- int main()
- {
- ComplexNumber complex = multiply({ 0, 1 }, { 0, 1 });
- print(complex);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement