Advertisement
SteelK

Untitled

Sep 30th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 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 void corrOutPol(double cocoefMas, size_t size);
  11.         //friend polinom operator+ (polinom pol1, polinom pol2);
  12.     public:
  13.         polinom() {
  14.             size = 0;
  15.             coefMas = NULL;
  16.         }
  17.         //Конструктор многочлена нужной степени.
  18.         polinom(size_t inSize) {
  19.             if (!(inSize)){
  20.                 size = 0;
  21.                 coefMas = NULL;
  22.                 return;
  23.             }
  24.             size = inSize;
  25.             coefMas = new double[size];
  26.             cout << "Specify the coefficients of the terms of the polynomial, starting with a greater degree: " << endl;
  27.             size_t i = size - 1, z = 0;
  28.             for (i, z; z <= i; z++) {
  29.                 cout << "(x^" << i-z << "): ";
  30.                 cin >> coefMas[i-z];
  31.             }
  32.             displayPol();
  33.             cout << endl;
  34.         }
  35.  
  36.         void getSize(size_t *size) {
  37.             *size = this->size;
  38.         }
  39.  
  40.         //Вывод многочлена в консоль.
  41.         void displayPol() {
  42.             if (!(size)) {
  43.                 cout << "This polynomial is empty." << endl;
  44.                 return;
  45.             }
  46.             size_t i = size - 1;
  47.             for (i; i > 0; i--)
  48.                 corrOutPol(coefMas[i], i);
  49.  
  50.             if (!(coefMas[0])) {
  51.                 cout << "=0" << endl;
  52.                 return;
  53.             }
  54.             if (coefMas[0] >= 0)
  55.                 cout << "+(" << coefMas[0] << ")";
  56.             else
  57.                 cout << "-(" << -coefMas[0] << ")";
  58.             cout << "=0" << endl;
  59.         }
  60.  
  61.         void copyInPol(polinom inPol) {
  62.             //if (inPol.size)
  63.                 inPol.clearPol();
  64.  
  65.             inPol.displayPol();
  66.             cout << "!!" << inPol.size << endl;
  67.             inPol.size = this->size;
  68.             cout << "!!" << inPol.size << endl;
  69.             inPol.coefMas = new double[size];
  70.             for (size_t i = 0; i < size; i++)
  71.                 inPol.coefMas[i] = this->coefMas[i];
  72.         }
  73.  
  74.         void clearPol(){
  75.             for (size_t i = 0; i < size; i++)
  76.                 coefMas[i] = NULL;
  77.             delete[]coefMas;
  78.             size = 0;
  79.             coefMas = NULL;
  80.         }
  81.  
  82.         //Деструктор объектов.
  83.         ~polinom() {
  84.             if (!(coefMas))
  85.                 return;
  86.             cout << "The polynomial ";
  87.             displayPol();
  88.             cout << "   was deleted." << endl;
  89.             delete []coefMas;
  90.         }
  91. };
  92.  
  93.  
  94. void corrOutPol(double coef, size_t i) {
  95.     if (coef == 1)
  96.         cout << "+(x^" << i << ")";
  97.     else if (coef == -1)
  98.         cout << "-(x^" << i << ")";
  99.         else if (coef > 0)
  100.             cout << "+(" << coef << "x^" << i << ")";
  101.             else if (coef < 0)
  102.                 cout << "-(" << -coef << "x^" << i << ")";
  103. }
  104.  
  105.  
  106. int main() {
  107.     cout << "What is the highest degree of a polynomial?" << endl;
  108.     size_t size;
  109.     cin >> size;
  110.     polinom pol1(size);
  111.     //pol1.displayPol();
  112.  
  113.     polinom newPol(3);
  114.     pol1.copyInPol(newPol);
  115.     cout << "!!!" << endl;
  116.     newPol.displayPol();
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement