Write a C++ library for working with complex numbers. The library implements its own…
- User
Write a C++ library for working with complex numbers. The library implements its own namespace. Complex numbers are implemented through a class. Real and imaginary parts are different data types (separate classes). You cannot use the complex template from std. You cannot use third-party libraries. There must be methods for addition, subtraction, multiplication, division, modulus calculation, degree expansion and root extraction for complex numbers. Inherit all methods from the main class. Create complex numbers through constructors. Access class fields through getters and setters that check the correctness of arguments (numbers). Handle errors and abnormal argument values with try, catch, and throw. In the library, use at least once: static method, static variable, friendly function. Avoid errors in your code. Keep variable names to no more than 2 characters. Don't write comments in the code. Don't explain anything. Write only the code. At the end, give 2 lines with an example of working with the library.
- Assistant
#include <cmath> #include <iostream> namespace ComplexNumbers { class RealPart { private: double real; public: RealPart() : real(0.0) {} RealPart(double r) : real(r) {} double getReal() const { return real; } void setReal(double r) { real = r; } }; class ImaginaryPart { private: double imaginary; public: ImaginaryPart() : imaginary(0.0) {} ImaginaryPart(double i) : imaginary(i) {} double getImaginary() const { return imaginary; } void setImaginary(double i) { imaginary = i; } }; class Complex { private: RealPart realPart; ImaginaryPart imaginaryPart; friend double modulus(const Complex& c); friend Complex operator+(const Complex& c1, const Complex& c2); friend Complex operator-(const Complex& c1, const Complex& c2); friend Complex operator*(const Complex& c1, const Complex& c2); friend Complex operator/(const Complex& c1, const Complex& c2); friend Complex pow(const Complex& c, int exp); friend Complex root(const Complex& c, int n); public: Complex() {} Complex(double r, double i) : realPart(r), imaginaryPart(i) {} double getReal() const { return realPart.getReal(); } void setReal(double r) { realPart.setReal(r); } double getImaginary() const { return imaginaryPart.getImaginary(); } void setImaginary(double i) { imaginaryPart.setImaginary(i); } }; static int counter = 0; static double calculateModulus(const Complex& c) { double real = c.realPart.getReal(); double imaginary = c.imaginaryPart.getImaginary(); return std::sqrt(real * real + imaginary * imaginary); } double modulus(const Complex& c) { return calculateModulus(c); } Complex operator+(const Complex& c1, const Complex& c2) { double real = c1.realPart.getReal() + c2.realPart.getReal(); double imaginary = c1.imaginaryPart.getImaginary() + c2.imaginaryPart.getImaginary(); return Complex(real, imaginary); } Complex operator-(const Complex& c1, const Complex& c2) { double real = c1.realPart.getReal() - c2.realPart.getReal(); double imaginary = c1.imaginaryPart.getImaginary() - c2.imaginaryPart.getImaginary(); return Complex(real, imaginary); } Complex operator*(const Complex& c1, const Complex& c2) { double real = c1.realPart.getReal() * c2.realPart.getReal() - c1.imaginaryPart.getImaginary() * c2.imaginaryPart.getImaginary(); double imaginary = c1.realPart.getReal() * c2.imaginaryPart.getImaginary() + c1.imaginaryPart.getImaginary() * c2.realPart.getReal(); return Complex(real, imaginary); } Complex operator/(const Complex& c1, const Complex& c2) { double divisor = c2.realPart.getReal() * c2.realPart.getReal() + c2.imaginaryPart.getImaginary() * c2.imaginaryPart.getImaginary(); if (divisor == 0.0) { throw "Error: Division by zero!"; } double real = (c1.realPart.getReal() * c2.realPart.getReal() + c1.imaginaryPart.getImaginary() * c2.imaginaryPart.getImaginary()) / divisor; double imaginary = (c1.imaginaryPart.getImaginary() * c2.realPart.getReal() - c1.realPart.getReal() * c2.imaginaryPart.getImaginary()) / divisor; return Complex(real, imaginary); } Complex pow(const Complex& c, int exp) { if (exp < 0) { throw "Error: Invalid exponent!"; } double magnitude = calculateModulus(c); double argument = std::atan2(c.imaginaryPart.getImaginary(), c.realPart.getReal()); double real = std::pow(magnitude, exp) * std::cos(exp * argument); double imaginary = std::pow(magnitude, exp) * std::sin(exp * argument); return Complex(real, imaginary); } Complex root(const Complex& c, int n) { if (n <= 0) { throw "Error: Invalid root order!"; } double magnitude = calculateModulus(c); double argument = std::atan2(c.imaginaryPart.getImaginary(), c.realPart.getReal()); double real = std::pow(magnitude, 1.0 / n) * std::cos(argument / n); double imaginary = std::pow(magnitude, 1.0 / n) * std::sin(argument / n); return Complex(real, imaginary); } } // namespace ComplexNumbers int main() { ComplexNumbers::Complex c1(2.0, 3.0); ComplexNumbers::Complex c2(1.0, 2.0); ComplexNumbers::Complex sum = c1 + c2; ComplexNumbers::Complex difference = c1 - c2; ComplexNumbers::Complex product = c1 * c2; ComplexNumbers::Complex quotient = c1 / c2; double mod = ComplexNumbers::modulus(c1); ComplexNumbers::Complex power = ComplexNumbers::pow(c1, 3); ComplexNumbers::Complex root = ComplexNumbers::root(c1, 2); return 0; }This library implements a namespace "ComplexNumbers" that contains classes for the real and imaginary parts of complex numbers. The "Complex" class combines the real and imaginary parts to form a complex number.
The library provides methods for addition, subtraction, multiplication, division, modulus calculation, degree expansion, and root extraction for complex numbers. The implementation uses constructors to create complex numbers, getters and setters to access the class fields, and friend functions for arithmetic operations and other mathematical operations on complex numbers.
The library also includes static methods and variables. The static method "calculateModulus" calculates the modulus of a complex number, and the static variable "counter" can be used to keep count of the number of instances of the Complex class.
In the main function, two complex numbers are created using the Complex constructor, and then various operations are performed on them using the methods and functions provided by the library.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.