Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <format>
- using std::cout; using std::cin; using std::endl; using std::format; using std::string;
- class Complex {
- public:
- Complex(double numReal, double numImaginary) {
- real = numReal;
- imaginary = numImaginary;
- }
- double getReal() {
- return real;
- }
- double getImaginary() {
- return imaginary;
- }
- void print() {
- if (imaginary >= 0) {
- cout << format("{0}+{1}i", real, imaginary) << endl;
- }
- else {
- cout << format("{0}{1}i", real, imaginary) << endl;
- }
- }
- string strForm() {
- if (imaginary >= 0) {
- return format("{0}+{1}i", real, imaginary);
- }
- else {
- return format("{0}{1}i", real, imaginary);
- }
- }
- private:
- double real;
- double imaginary;
- };
- //функция для возведения в любую степень
- double degr(double num, double N) {
- int degrNum = num;
- for (int i = 1; i < N; i++) {
- degrNum *= num;
- }
- return degrNum;
- }
- //Сложение комплексных чисел
- Complex complexSum(Complex num1, Complex num2) {
- double real = num1.getReal() + num2.getReal();
- double imaiginary = num1.getImaginary() + num2.getImaginary();
- Complex result(real, imaiginary);
- return result;
- }
- //Вычитание комплексных чисел
- Complex complexSub(Complex num1, Complex num2) {
- double real = num1.getReal() - num2.getReal();
- double imaginary = num1.getImaginary() - num2.getImaginary();
- Complex result(real, imaginary);
- return result;
- }
- //Умножение комплексных чисел
- Complex complexMult(Complex num1, Complex num2) {
- double real = (num1.getReal() * num2.getReal()) - (num1.getImaginary() * num2.getImaginary());
- double imaginary = (num1.getImaginary() * num2.getReal()) + (num1.getReal() * num2.getImaginary());
- Complex result(real, imaginary);
- return result;
- }
- //Деление комплексных чисел
- Complex complexDiv(Complex num1, Complex num2) {
- double real = ((num1.getReal() * num2.getReal()) + (num1.getImaginary() * num2.getImaginary())) /
- (degr(num2.getReal(), 2) + degr(num2.getImaginary(), 2));
- double imaginary = (num1.getImaginary() * num2.getReal() - num1.getReal() * num2.getImaginary()) /
- (degr(num2.getReal(), 2) + degr(num2.getImaginary(), 2));
- Complex result(real, imaginary);
- return result;
- }
- int main()
- {
- system("chcp 1251"); system("cls");
- cout << "Введите реальную и мнимую часть ПЕРВОГО числа через пробел(БЕЗ i): ";
- double num1A, num1B; cin >> num1A >> num1B;
- cout << "Введите реальную и мнимую часть ВТОРОГО числа через пробел(БЕЗ i): ";
- double num2A, num2B; cin >> num2A >> num2B;
- Complex num1(num1A, num1B);
- Complex num2(num2A, num2B);
- Complex num3 = complexSum(num1, num2);
- Complex num4 = complexSub(num1, num2);
- Complex num5 = complexMult(num1, num2);
- Complex num6 = complexDiv(num1, num2);
- cout << endl;
- cout << format("Результат сложения ({0}) + ({1}) \t= \t", num1.strForm(), num2.strForm()); num3.print();
- cout << format("Результат вычитания ({0}) - ({1}) \t= \t", num1.strForm(), num2.strForm()); num4.print();
- cout << format("Результат умножения ({0}) * ({1}) \t= \t", num1.strForm(), num2.strForm()); num5.print();
- cout << format("Результат деления ({0}) * ({1}) \t= \t", num1.strForm(), num2.strForm()); num6.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement