Advertisement
SteelK

Untitled

Oct 1st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class polinom {
  7. private:
  8.     size_t size; // Размер многочлена.
  9.     double *coefMas; // Массив коэф. многочлена.
  10. public:
  11.     // Конструктор по умолчанию.
  12.     polinom() {
  13.         size = 0;
  14.         coefMas = NULL;
  15.     }
  16.  
  17.     // Конструктор многочлена нужной степени.
  18.     polinom(size_t inSize) {
  19.         // Если будет введен размер = 0.
  20.         if (!(inSize)) {
  21.             size = 0;
  22.             coefMas = NULL;
  23.             return;
  24.         }
  25.  
  26.         size = inSize;
  27.         coefMas = new double[size];
  28.         cout << "Specify the coefficients of the terms of the polynomial,";
  29.         cout << " starting with a greater degree: " << endl;
  30.         // Такие итерации, т.к. заполнение начинается с конца.
  31.         for (size_t i = size, z = 1; z <= i; z++) {
  32.             cout << "(x^" << i - z << "): ";
  33.             cin >> coefMas[i - z];
  34.         }
  35.         displayPol();
  36.         cout << endl << endl;
  37.     }
  38.  
  39.     // Перегрузка =, т.к. ходим, что бы многочлены нормально приравнивались друг другу.
  40.     void operator= (polinom &inPol) {
  41.         // Если многочлен не пустой (размер != 0), очищаем его.
  42.         if (size)
  43.             clearPol();
  44.  
  45.         size = inPol.size;
  46.         coefMas = new double[size];
  47.         for (size_t i = 0; i < size; i++)
  48.             coefMas[i] = inPol.coefMas[i];
  49.     }
  50.  
  51.     // Перегрузка + для многочленов.
  52.     polinom operator+ (polinom &inPol) {
  53.         polinom outPol;
  54.         if (size >= inPol.size) {
  55.             outPol = *this;
  56.             for (size_t i = 0; i < inPol.size; i++)
  57.                 outPol.coefMas[i] += inPol.coefMas[i];
  58.         }
  59.         else {
  60.             outPol = inPol;
  61.             for (size_t i = 0; i < size; i++)
  62.                 outPol.coefMas[i] += coefMas[i];
  63.         }
  64.         return outPol;
  65.     }
  66.  
  67.     // Перегрузка - для многочленов.
  68.     polinom operator- (polinom &inPol) {
  69.         polinom outPol;
  70.         if (size >= inPol.size) {
  71.             outPol = *this;
  72.             for (size_t i = 0; i < inPol.size; i++)
  73.                 outPol.coefMas[i] -= inPol.coefMas[i];
  74.         }
  75.         else {
  76.             outPol = inPol;
  77.             for (size_t i = 0; i < size; i++)
  78.                 outPol.coefMas[i] = -(outPol.coefMas[i]) + coefMas[i];
  79.         }
  80.         return outPol;
  81.     }
  82.  
  83.  
  84.     // Метод получения размера многочлена в переданную переменную.
  85.     void getSize(size_t *size) {
  86.         *size = this->size;
  87.     }
  88.  
  89.     // Вывод многочлена в консоль.
  90.     void displayPol() {
  91.         // Если вдруг попытаемся вывести пустой многочлен (size = 0).
  92.         if (!(size)) {
  93.             cout << "This polynomial is empty." << endl;
  94.             return;
  95.         }
  96.  
  97.         // Что бы не начать читать с границы массива.
  98.         size_t i = size - 1;
  99.         for (i; i > 0; i--)
  100.             corrOutPol(coefMas[i], i);
  101.  
  102.         // Отдельный вывод последнего коэф.
  103.         if (!(coefMas[0])) {
  104.             cout << "=0" << endl;
  105.             return;
  106.         }
  107.         if (coefMas[0] >= 0)
  108.             cout << "+(" << coefMas[0] << ")";
  109.         else
  110.             cout << "-(" << -coefMas[0] << ")";
  111.         cout << "=0";
  112.     }
  113.  
  114.     // Метод очистки многочлена.
  115.     void clearPol() {
  116.         for (size_t i = 0; i < size; i++)
  117.             coefMas[i] = NULL;
  118.         delete[]coefMas;
  119.         size = 0;
  120.         coefMas = NULL;
  121.     }
  122.  
  123.     void corrOutPol(double coef, size_t i) {
  124.         if (coef == 1)
  125.             cout << "+(x^" << i << ")";
  126.         else if (coef == -1)
  127.             cout << "-(x^" << i << ")";
  128.         else if (coef > 0)
  129.             cout << "+(" << coef << "x^" << i << ")";
  130.         else if (coef < 0)
  131.             cout << "-(" << -coef << "x^" << i << ")";
  132.     }
  133.  
  134.     // Деструктор объектов.
  135.     ~polinom() {
  136.         // Если многочлен пустой, удалять нечего.
  137.         if (!(coefMas))
  138.             return;
  139.  
  140.         cout << "The polynomial ";
  141.         displayPol();
  142.         cout << " was deleted. [" << coefMas << "]" << endl;
  143.         delete[]coefMas;
  144.     }
  145. };
  146.  
  147.  
  148.  
  149. int main() {
  150.     cout << "What is the highest degree of a polynomial(1)?" << endl;
  151.     size_t size1;
  152.     cin >> size1;
  153.     polinom pol1(size1);
  154.  
  155.     cout << "What is the highest degree of a polynomial(2)?" << endl;
  156.     size_t size2;
  157.     cin >> size2;
  158.     polinom pol2(size2);
  159.  
  160.     polinom sum1, sum2, raz1, raz2;
  161.     sum1 = pol1 + pol2;
  162.     sum2 = pol2 + pol1;
  163.     raz1 = pol1 - pol2;
  164.     raz2 = pol1 - pol2;
  165.  
  166.     cout << endl << endl << "pol1:"; pol1.displayPol(); cout << endl;
  167.     cout << "pol2:"; pol2.displayPol(); cout << endl;
  168.     cout << "-------------------------------" << endl;
  169.     cout << "pol1 + pol2: "; sum1.displayPol(); cout << endl;
  170.     cout << "pol2 + pol1:"; sum2.displayPol(); cout << endl;
  171.     cout << "pol1 - pol2:"; raz1.displayPol(); cout << endl;
  172.     cout << "pol2 - pol1:"; raz2.displayPol(); cout << endl;
  173.  
  174.     cout << endl << endl << "Exiting..." << endl;
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement