#include "stdafx.h" #include using namespace std; class polinom { private: size_t size; double *coefMas; friend void corrOutPol(double cocoefMas, size_t size); //friend polinom operator+ (polinom pol1, polinom pol2); public: polinom() { size = 0; coefMas = NULL; } //Конструктор многочлена нужной степени. polinom(size_t inSize) { if (!(inSize)){ size = 0; coefMas = NULL; return; } size = inSize; coefMas = new double[size]; cout << "Specify the coefficients of the terms of the polynomial, starting with a greater degree: " << endl; size_t i = size - 1, z = 0; for (i, z; z <= i; z++) { cout << "(x^" << i-z << "): "; cin >> coefMas[i-z]; } displayPol(); cout << endl; } void getSize(size_t *size) { *size = this->size; } //Вывод многочлена в консоль. void displayPol() { if (!(size)) { cout << "This polynomial is empty." << endl; return; } size_t i = size - 1; for (i; i > 0; i--) corrOutPol(coefMas[i], i); if (!(coefMas[0])) { cout << "=0" << endl; return; } if (coefMas[0] >= 0) cout << "+(" << coefMas[0] << ")"; else cout << "-(" << -coefMas[0] << ")"; cout << "=0" << endl; } void copyInPol(polinom inPol) { //if (inPol.size) inPol.clearPol(); inPol.displayPol(); cout << "!!" << inPol.size << endl; inPol.size = this->size; cout << "!!" << inPol.size << endl; inPol.coefMas = new double[size]; for (size_t i = 0; i < size; i++) inPol.coefMas[i] = this->coefMas[i]; } void clearPol(){ for (size_t i = 0; i < size; i++) coefMas[i] = NULL; delete[]coefMas; size = 0; coefMas = NULL; } //Деструктор объектов. ~polinom() { if (!(coefMas)) return; cout << "The polynomial "; displayPol(); cout << " was deleted." << endl; delete []coefMas; } }; void corrOutPol(double coef, size_t i) { if (coef == 1) cout << "+(x^" << i << ")"; else if (coef == -1) cout << "-(x^" << i << ")"; else if (coef > 0) cout << "+(" << coef << "x^" << i << ")"; else if (coef < 0) cout << "-(" << -coef << "x^" << i << ")"; } int main() { cout << "What is the highest degree of a polynomial?" << endl; size_t size; cin >> size; polinom pol1(size); //pol1.displayPol(); polinom newPol(3); pol1.copyInPol(newPol); cout << "!!!" << endl; newPol.displayPol(); return 0; }