Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ComplexStruct.h
- #pragma once
- #include "string"
- using namespace std;
- class TComplexStruct
- {
- public:
- struct ComplexStruct
- {
- int vesh; // Вещественная
- int mnim; // Мнимая
- };
- void SetVesh(struct ComplexStruct*, int);
- void SetMnim(struct ComplexStruct*, int);
- int GetVesh(struct ComplexStruct*);
- int GetMnim(struct ComplexStruct*);
- };
- //ComplexStruct.cpp
- #include "stdafx.h"
- #include "ComplexStruct.h"
- //---------------------------------------------------
- void TComplexStruct::SetVesh(struct ComplexStruct* S, int _vesh)
- {
- S->vesh = _vesh;
- }
- void TComplexStruct::SetMnim(struct ComplexStruct* S, int _mnim)
- {
- S->mnim = _mnim;
- }
- int TComplexStruct::GetVesh(struct ComplexStruct* S)
- {
- return S->vesh;
- }
- int TComplexStruct::GetMnim(struct ComplexStruct* S)
- {
- return S->mnim;
- }
- //Complex.h
- #pragma once
- #include "string"
- using namespace std;
- class TComplex
- {
- public:
- int countValue = 0; // счетчик количества чисел
- int arrayValue[2][100]; //2x100
- void AddValue(int, int);// Добавление
- void Print(int); // Вывод на экран информации
- int GetVesh(int);
- int GetMnim(int);
- void Plus(int, int);// +
- void Minus(int, int);// -
- void Multiplication(int, int);// *
- void Division(int, int);// :
- };
- //Complex.cpp
- #include "stdafx.h"
- #include <iostream>
- #include "Complex.h"
- #include "ComplexStruct.h"
- //---------------------------------------------------
- void TComplex::AddValue(int v, int m)
- {
- arrayValue[0][countValue] = v;
- arrayValue[1][countValue] = m;
- cout << "Добавлено число " << arrayValue[0][countValue] << "." << arrayValue[1][countValue] << endl;
- countValue++;
- }
- int TComplex::GetVesh(int i)
- {
- return arrayValue[0][i];
- }
- int TComplex::GetMnim(int i)
- {
- return arrayValue[1][i];
- }
- void TComplex::Print(int i)
- {
- if (i == 0)
- {
- for (int i = 0; i < countValue; i++)
- {
- cout << "[" << i+1 << "] " << arrayValue[0][i] << "." << arrayValue[1][i] << endl;
- }
- }
- else
- {
- i-=1;
- cout << "[" << i + 1 << "] " << arrayValue[0][i] << "." << arrayValue[1][i] << endl;
- }
- }
- void TComplex::Plus(int one, int two)
- {
- double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
- double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
- cout << value1 << "+" << value2 << "=" << value1 + value2 << endl;
- }
- void TComplex::Minus(int one, int two)
- {
- double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
- double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
- cout << value1 << "-" << value2 << "=" << value1 - value2 << endl;
- }
- void TComplex::Multiplication(int one, int two)
- {
- double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
- double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
- cout << value1 << "*" << value2 << "=" << value1 * value2 << endl;
- }
- void TComplex::Division(int one, int two)
- {
- double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
- double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
- cout << value1 << "/" << value2 << "=" << value1 / value2 << endl;
- }
- //Lab_1.cpp
- #include "stdafx.h"
- #include <locale>
- #include <windows.h>
- #include <iostream>
- #include "Complex.h"
- #include "ComplexStruct.h"
- using namespace std;
- //Структура – Комплексное число.Структура должна включать соответствующие поля :
- //вещественную и мнимую часть числа.
- //Простейшие функции : сложение, вычитание, умножение, деление, вывод числа в удобной форме.
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int option = 0;
- int vesh = 0, mnim = 0, one = 0, two = 0;
- // Создаем указатель на cтруктуру ComplexStruct класса TComplexStruct
- TComplexStruct::ComplexStruct *CompS;
- // Выделяем динамически память под структуру
- // и присваиваем адрес структуры в указатель
- try
- {
- CompS = new TComplexStruct::ComplexStruct;
- }
- catch (...)
- {
- cout << "Ошибка: Память не выделена!";
- exit(0);
- };
- // Создаем объект класса TComplex
- TComplex* Comp;
- // Выделяем динамически память под объект
- Comp = new TComplex;
- while (1)
- {
- cout << "1.Ввод числа(вещественной и мнимой части)" << endl;
- cout << "2.Сложение" << endl;
- cout << "3.Вычитание" << endl;
- cout << "4.Умножение" << endl;
- cout << "5.Деление" << endl;
- cout << "6.Вывод числа" << endl;
- cout << "7.Выход" << endl;
- cin >> option;
- // Работа с классом
- switch (option)
- {
- case 1:
- {
- cout << "Введите вещественную часть: ";
- cin >> vesh;
- cout << "Введите мнимую часть: ";
- cin >> mnim;
- Comp->AddValue(vesh, mnim);
- break;
- }
- case 2:
- {
- cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
- cin >> one;
- cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
- cin >> two;
- Comp->Plus(one-1, two-1);
- break;
- }
- case 3:
- {
- cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
- cin >> one;
- cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
- cin >> two;
- Comp->Minus(one - 1, two - 1);
- break;
- }
- case 4:
- {
- cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
- cin >> one;
- cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
- cin >> two;
- Comp->Multiplication(one - 1, two - 1);
- break;
- }
- case 5:
- {
- cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
- cin >> one;
- cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
- cin >> two;
- Comp->Division(one - 1, two - 1);
- break;
- }
- case 6:
- {
- int opt = 0;
- cout << "Введите индекс числа для вывода, введите 0 что бы вывести все (1-" << Comp->countValue << ")";
- cin >> opt;
- Comp->Print(opt);
- break;
- }
- case 7:
- {
- exit(0);
- }
- default:
- cout << "Введите число от 1 до 7" << endl;
- }
- // Работа со структурой
- // Создаем объект класса TComplexStruct
- TComplexStruct* TCS;
- // Выделяем динамически память под объект
- TCS = new TComplexStruct;
- for (int i = 0; i < Comp->countValue; i++)
- {
- TCS->SetVesh(CompS, Comp->GetVesh(i));
- TCS->SetMnim(CompS, Comp->GetMnim(i));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment