Advertisement
SteelK

Polinom

Oct 6th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.02 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.     friend ostream& operator<< (ostream &out, const polinom &inPol);
  11. public:
  12.     // Конструктор по умолчанию.
  13.     polinom() {
  14.         size = 0;
  15.         coefMas = NULL;
  16.     }
  17.  
  18.     // Конструктор многочлена нужной степени.
  19.     polinom(size_t inSize) {
  20.         // Если будет введен размер = 0.
  21.         if (!(inSize)) {
  22.             size = 0;
  23.             coefMas = NULL;
  24.             return;
  25.         }
  26.  
  27.         size = inSize;
  28.         coefMas = new double[size];
  29.         cout << "Specify the coefficients of the terms of the polynomial,";
  30.         cout << " starting with a greater degree: " << endl;
  31.         // Такие итерации, т.к. заполнение начинается с конца.
  32.         for (size_t i = size, z = 1; z <= i; z++) {
  33.             cout << "(x^" << i - z << "): ";
  34.             cin >> coefMas[i - z];
  35.         }
  36.         cout << *this << 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 < outPol.size; i++)
  78.                 outPol.coefMas[i] = -outPol.coefMas[i];
  79.             for (size_t i = 0; i < size; i++)
  80.                 outPol.coefMas[i] += coefMas[i];
  81.         }
  82.         return outPol;
  83.     }
  84.  
  85.     // Перегрузка умножения для многочленов.
  86.     polinom operator* (polinom &inPol) {
  87.         polinom outPol;
  88.         // Именно таким будет размер нового многочлена после умножения двух других.
  89.         size_t bigSize = inPol.size + size;
  90.         outPol.coefMas = new double[bigSize];
  91.         outPol.size = bigSize;
  92.  
  93.         for (size_t i = 0; i < outPol.size; i++)
  94.             outPol.coefMas[i] = 0;
  95.        
  96.         // [i+j] сложение степеней при умножении.
  97.         for (size_t i = 0; i < size; i++)
  98.             for (size_t j = 0; j < inPol.size; j++)
  99.                 outPol.coefMas[i+j] += coefMas[i] * inPol.coefMas[j];
  100.         return outPol;
  101.     }
  102.  
  103.     // Метод взятия производной.
  104.     void diff() {
  105.         for (size_t i = 0, j = 1; j < size; i++, j++)
  106.             coefMas[i] = j * coefMas[j];
  107.         // В процессе мы не перезапишем последний элемент. Фикс.
  108.         coefMas[size-1] = 0;
  109.     }
  110.  
  111.     // Метод получения размера многочлена в переданную переменную.
  112.     void getSize(size_t *size) {
  113.         *size = this->size;
  114.     }
  115.  
  116.     // Метод очистки многочлена.
  117.     void clearPol() {
  118.         delete[]coefMas;
  119.         size = 0;
  120.         coefMas = NULL;
  121.     }
  122.  
  123.     // Деструктор объектов.
  124.     ~polinom() {
  125.         // Если многочлен пустой, удалять нечего.
  126.         if (!(size))
  127.             return;
  128.  
  129.         cout << "The polynomial " << *this << " is destroyed. [" << coefMas << "]" << endl;
  130.         delete[]coefMas;
  131.     }
  132. };
  133.  
  134. // Переопределение потока вывода.
  135. ostream& operator<< (ostream &out, const polinom &inPol) {
  136.     if (!(inPol.size)) {
  137.         out << "This polynomial is empty." << endl;
  138.         return out;
  139.     }
  140.  
  141.     // Что бы не начать читать с границы массива.
  142.     size_t i = inPol.size - 1;
  143.     for (; i > 0; i--)
  144.         if (inPol.coefMas[i] == 1)
  145.             out << "+(x^" << i << ")";
  146.         else if (inPol.coefMas[i] == -1)
  147.             out << "-(x^" << i << ")";
  148.         else if (inPol.coefMas[i] > 0)
  149.             out << "+(" << inPol.coefMas[i] << "x^" << i << ")";
  150.         else if (inPol.coefMas[i] < 0)
  151.             out << "-(" << -inPol.coefMas[i] << "x^" << i << ")";;
  152.  
  153.     // Отдельный вывод последнего коэф.
  154.     if (!(inPol.coefMas[0]))
  155.         out << "=0" << endl;
  156.     else if (inPol.coefMas[0] > 0)
  157.         out << "+(" << inPol.coefMas[0] << ")";
  158.     else
  159.         out << "-(" << -inPol.coefMas[0] << ")";
  160.     out << "=0";
  161.     return out;
  162. }
  163.  
  164.  
  165. int main() {
  166.     size_t size1, size2;
  167.     cout << "What is the highest degree of a polynomial Num1?" << endl;
  168.     cin >> size1;
  169.     polinom pol1(size1);
  170.  
  171.     cout << "What is the highest degree of a polynomial Num2?" << endl;
  172.     cin >> size2;
  173.     polinom pol2(size2);
  174.  
  175.     polinom sum1, sum2, raz1, raz2, mul1, mul2;
  176.  
  177.     sum1 = pol1 + pol2;
  178.     sum2 = pol2 + pol1;
  179.  
  180.     raz1 = pol1 - pol2;
  181.     raz2 = pol2 - pol1;
  182.  
  183.     mul1 = pol1 * pol2;
  184.     mul2 = pol2 * pol1;
  185.  
  186.     cout << endl;
  187.     cout << "pol1: " << pol1 << endl;
  188.     cout << "pol2: " << pol2 << endl;
  189.     cout << "-------------------------------------------------------------" << endl;
  190.     cout << "pol1 + pol2: " << sum1 << endl;
  191.     cout << "pol2 + pol1: " << sum2 << endl;
  192.     cout << "-------------------------------------------------------------" << endl;
  193.     cout << "pol1 - pol2: " << raz1 << endl;
  194.     cout << "pol2 - pol1: " << raz2 << endl;
  195.     cout << "-------------------------------------------------------------" << endl;
  196.     cout << "pol1 * pol2: " << mul1 << endl;
  197.     cout << "pol2 * pol1: " << mul2 << endl;
  198.    
  199.     pol1.diff();
  200.     pol2.diff();
  201.  
  202.     cout << "-------------------------------------------------------------" << endl;
  203.     cout << "diff(pol1): " << pol1 << endl;
  204.     cout << "diff(pol2): " << pol2 << endl;
  205.  
  206.     cout << endl << "Exiting.........................." << endl;
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement