Xom9ik

Lab_1/8var (lll semester)

Oct 8th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.07 KB | None | 0 0
  1. //ComplexStruct.h
  2. #pragma once
  3. #include "string"
  4. using namespace std;
  5.  
  6. class TComplexStruct
  7. {
  8. public:
  9.     struct ComplexStruct
  10.     {
  11.         int vesh; // Вещественная
  12.         int mnim; // Мнимая
  13.     };
  14.     void SetVesh(struct ComplexStruct*, int);
  15.     void SetMnim(struct ComplexStruct*, int);
  16.     int GetVesh(struct ComplexStruct*);
  17.     int GetMnim(struct ComplexStruct*);
  18. };
  19. //ComplexStruct.cpp
  20. #include "stdafx.h"
  21. #include "ComplexStruct.h"
  22. //---------------------------------------------------
  23. void TComplexStruct::SetVesh(struct ComplexStruct* S, int _vesh)
  24. {
  25.     S->vesh = _vesh;
  26. }
  27. void TComplexStruct::SetMnim(struct ComplexStruct* S, int _mnim)
  28. {
  29.     S->mnim = _mnim;
  30. }
  31. int TComplexStruct::GetVesh(struct ComplexStruct* S)
  32. {
  33.     return S->vesh;
  34. }
  35. int TComplexStruct::GetMnim(struct ComplexStruct* S)
  36. {
  37.     return S->mnim;
  38. }
  39. //Complex.h
  40. #pragma once
  41. #include "string"
  42. using namespace std;
  43.  
  44. class TComplex
  45. {
  46. public:
  47.     int countValue = 0; // счетчик количества чисел
  48.     int arrayValue[2][100]; //2x100
  49.     void AddValue(int, int);// Добавление
  50.     void Print(int); // Вывод на экран информации
  51.     int GetVesh(int);
  52.     int GetMnim(int);
  53.     void Plus(int, int);// +
  54.     void Minus(int, int);// -
  55.     void Multiplication(int, int);// *
  56.     void Division(int, int);// :
  57. };
  58. //Complex.cpp
  59. #include "stdafx.h"
  60. #include <iostream>
  61. #include "Complex.h"
  62. #include "ComplexStruct.h"
  63. //---------------------------------------------------
  64. void TComplex::AddValue(int v, int m)
  65. {
  66.     arrayValue[0][countValue] = v;
  67.     arrayValue[1][countValue] = m;
  68.     cout << "Добавлено число " << arrayValue[0][countValue] << "." << arrayValue[1][countValue] << endl;
  69.     countValue++;
  70. }
  71. int TComplex::GetVesh(int i)
  72. {
  73.     return arrayValue[0][i];
  74. }
  75. int TComplex::GetMnim(int i)
  76. {
  77.     return arrayValue[1][i];
  78. }
  79. void TComplex::Print(int i)
  80. {
  81.     if (i == 0)
  82.     {
  83.         for (int i = 0; i < countValue; i++)
  84.         {
  85.             cout << "[" << i+1 << "] " << arrayValue[0][i] << "." << arrayValue[1][i] << endl;
  86.         }
  87.     }
  88.     else
  89.     {
  90.         i-=1;
  91.         cout << "[" << i + 1 << "] " << arrayValue[0][i] << "." << arrayValue[1][i] << endl;
  92.     }
  93. }
  94. void TComplex::Plus(int one, int two)
  95. {  
  96.     double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
  97.     double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
  98.     cout << value1 << "+" << value2 << "=" << value1 + value2 << endl;
  99. }
  100. void TComplex::Minus(int one, int two)
  101. {
  102.     double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
  103.     double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
  104.     cout << value1 << "-" << value2 << "=" << value1 - value2 << endl;
  105. }
  106. void TComplex::Multiplication(int one, int two)
  107. {
  108.     double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
  109.     double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
  110.     cout << value1 << "*" << value2 << "=" << value1 * value2 << endl;
  111. }
  112. void TComplex::Division(int one, int two)
  113. {
  114.     double value1 = double(arrayValue[0][one]) + double(arrayValue[1][one]) / 100.;
  115.     double value2 = (double)arrayValue[0][two] + (double)arrayValue[1][two] / 100.;
  116.     cout << value1 << "/" << value2 << "=" << value1 / value2 << endl;
  117. }
  118. //Lab_1.cpp
  119. #include "stdafx.h"
  120. #include <locale>
  121. #include <windows.h>
  122. #include <iostream>
  123.  
  124. #include "Complex.h"
  125. #include "ComplexStruct.h"
  126.  
  127. using namespace std;
  128. //Структура – Комплексное число.Структура должна включать соответствующие поля :
  129. //вещественную и мнимую часть числа.
  130. //Простейшие функции : сложение, вычитание, умножение, деление, вывод числа в удобной форме.
  131. int main()
  132. {
  133.     SetConsoleCP(1251);
  134.     SetConsoleOutputCP(1251);
  135.     int option = 0;
  136.     int vesh = 0, mnim = 0, one = 0, two = 0;
  137.     // Создаем указатель на cтруктуру ComplexStruct класса TComplexStruct
  138.     TComplexStruct::ComplexStruct *CompS;
  139.     // Выделяем динамически память под структуру
  140.     // и присваиваем адрес структуры в указатель
  141.     try
  142.     {
  143.         CompS = new TComplexStruct::ComplexStruct;
  144.     }
  145.     catch (...)
  146.     {
  147.         cout << "Ошибка: Память не выделена!";
  148.         exit(0);
  149.     };
  150.     // Создаем объект класса TComplex
  151.     TComplex* Comp;
  152.     // Выделяем динамически память под объект
  153.     Comp = new TComplex;
  154.     while (1)
  155.     {
  156.         cout << "1.Ввод числа(вещественной и мнимой части)" << endl;
  157.         cout << "2.Сложение" << endl;
  158.         cout << "3.Вычитание" << endl;
  159.         cout << "4.Умножение" << endl;
  160.         cout << "5.Деление" << endl;
  161.         cout << "6.Вывод числа" << endl;
  162.         cout << "7.Выход" << endl;
  163.         cin >> option;
  164.         // Работа с классом
  165.         switch (option)
  166.         {
  167.         case 1:
  168.         {
  169.             cout << "Введите вещественную часть: ";
  170.             cin >> vesh;
  171.             cout << "Введите мнимую часть: ";
  172.             cin >> mnim;
  173.             Comp->AddValue(vesh, mnim);
  174.             break;
  175.         }
  176.         case 2:
  177.         {
  178.             cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
  179.             cin >> one;
  180.             cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
  181.             cin >> two;
  182.             Comp->Plus(one-1, two-1);
  183.             break;
  184.         }
  185.         case 3:
  186.         {
  187.             cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
  188.             cin >> one;
  189.             cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
  190.             cin >> two;
  191.             Comp->Minus(one - 1, two - 1);
  192.             break;
  193.         }
  194.         case 4:
  195.         {
  196.             cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
  197.             cin >> one;
  198.             cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
  199.             cin >> two;
  200.             Comp->Multiplication(one - 1, two - 1);
  201.             break;
  202.         }
  203.         case 5:
  204.         {
  205.             cout << "Введите индекс первого числа(1-" << Comp->countValue << ")";
  206.             cin >> one;
  207.             cout << "Введите индекс второго числа(1-" << Comp->countValue << ")";
  208.             cin >> two;
  209.             Comp->Division(one - 1, two - 1);
  210.             break;
  211.         }
  212.         case 6:
  213.         {
  214.             int opt = 0;
  215.             cout << "Введите индекс числа для вывода, введите 0 что бы вывести все (1-" << Comp->countValue << ")";
  216.             cin >> opt;
  217.             Comp->Print(opt);
  218.             break;
  219.         }
  220.         case 7:
  221.         {
  222.             exit(0);
  223.         }
  224.         default:
  225.             cout << "Введите число от 1 до 7" << endl;
  226.         }
  227.         // Работа со структурой
  228.         // Создаем объект класса TComplexStruct
  229.         TComplexStruct* TCS;
  230.         // Выделяем динамически память под объект
  231.         TCS = new TComplexStruct;
  232.         for (int i = 0; i < Comp->countValue; i++)
  233.         {
  234.             TCS->SetVesh(CompS, Comp->GetVesh(i));
  235.             TCS->SetMnim(CompS, Comp->GetMnim(i));
  236.         }
  237.     }
  238.     return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment