Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab1_oop.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <math.h>
- class Complex {
- private:
- double realPart;
- double imaginaryPart;
- public:
- Complex(double realPart, double imaginaryPart)
- {
- this->realPart = realPart;
- this->imaginaryPart = imaginaryPart;
- }
- void display()
- {
- if (realPart != 0)
- {
- if (imaginaryPart > 0) {
- std::cout << realPart << " + " << imaginaryPart << "i";
- return;
- }
- if (imaginaryPart < 0) {
- std::cout << realPart << " - " << abs(imaginaryPart) << "i";
- return;
- }
- if (imaginaryPart == 0)
- {
- std::cout << realPart;
- return;
- }
- }
- else {
- if (imaginaryPart > 0) {
- std::cout << imaginaryPart << "i";
- return;
- }
- if (imaginaryPart < 0) {
- std::cout << "- " << abs(imaginaryPart) << "i";
- return;
- }
- if (imaginaryPart == 0)
- {
- std::cout << 0;
- return;
- }
- }
- }
- Complex operator + (Complex c) const
- {
- return Complex(this->realPart + c.realPart, this->imaginaryPart + c.imaginaryPart);
- }
- Complex operator - (Complex c)
- {
- return Complex(this->realPart - c.realPart, this->imaginaryPart - c.imaginaryPart);
- }
- Complex operator * (Complex c)
- {
- return Complex(this->realPart * c.realPart - this->imaginaryPart * c.imaginaryPart, this->realPart*c.imaginaryPart + c.realPart * this->imaginaryPart);
- }
- };
- void sum() {
- double firstReal;
- double secondReal;
- double firstImag;
- double secondImag;
- std::cout << "Введите вещественную часть первого числа: ";
- std::cin >> firstReal;
- std::cout << "Введите мнимую часть первого числа: ";
- std::cin >> firstImag;
- std::cout << "Введите вещественную часть второго числа: ";
- std::cin >> secondReal;
- std::cout << "Введите мнимую часть второго числа: ";
- std::cin >> secondImag;
- Complex c1(firstReal, firstImag);
- Complex c2(secondReal, secondImag);
- Complex c3 = c1 + c2;
- std::cout << "Результат операции: ";
- c3.display();
- std::cout << std::endl;
- }
- void subtract() {
- double firstReal;
- double secondReal;
- double firstImag;
- double secondImag;
- std::cout << "Введите вещественную часть первого числа: ";
- std::cin >> firstReal;
- std::cout << "Введите мнимую часть первого числа: ";
- std::cin >> firstImag;
- std::cout << "Введите вещественную часть второго числа: ";
- std::cin >> secondReal;
- std::cout << "Введите мнимую часть второго числа: ";
- std::cin >> secondImag;
- Complex c1(firstReal, firstImag);
- Complex c2(secondReal, secondImag);
- Complex c3 = c1 - c2;
- std::cout << "Результат операции: ";
- c3.display();
- std::cout << std::endl;
- }
- void multiplication() {
- double firstReal;
- double secondReal;
- double firstImag;
- double secondImag;
- std::cout << "Введите вещественную часть первого числа: ";
- std::cin >> firstReal;
- std::cout << "Введите мнимую часть первого числа: ";
- std::cin >> firstImag;
- std::cout << "Введите вещественную часть второго числа: ";
- std::cin >> secondReal;
- std::cout << "Введите мнимую часть второго числа: ";
- std::cin >> secondImag;
- Complex c1(firstReal, firstImag);
- Complex c2(secondReal, secondImag);
- Complex c3 = c1 * c2;
- std::cout << "Результат операции: ";
- c3.display();
- std::cout << std::endl;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int flag = 0;
- while (flag != 4)
- {
- std::cout << "1. Проверка функции сложения комплексных чисел" << std::endl;
- std::cout << "2. Проверка функции вычитания комплексных чисел" << std::endl;
- std::cout << "3. Проверка функции умножения комплексных чисел" << std::endl;
- std::cout << "4. Выход" << std::endl;
- std::cin >> flag;
- system("cls");
- switch (flag)
- {
- case 1: sum(); system("pause"); break;
- case 2: subtract(); system("pause"); break;
- case 3: multiplication(); system("pause"); break;
- case 4: break;
- default:
- break;
- }
- system("cls");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement