Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class polinom {
- private:
- size_t size; // Размер многочлена.
- double *coefMas; // Массив коэф. многочлена.
- friend ostream& operator<< (ostream &out, const polinom &inPol);
- public:
- // Конструктор по умолчанию.
- polinom() {
- size = 0;
- coefMas = NULL;
- }
- // Конструктор многочлена нужной степени.
- polinom(size_t inSize) {
- // Если будет введен размер = 0.
- if (!(inSize)) {
- size = 0;
- coefMas = NULL;
- return;
- }
- size = inSize;
- coefMas = new double[size];
- cout << "Specify the coefficients of the terms of the polynomial,";
- cout << " starting with a greater degree: " << endl;
- // Такие итерации, т.к. заполнение начинается с конца.
- for (size_t i = size, z = 1; z <= i; z++) {
- cout << "(x^" << i - z << "): ";
- cin >> coefMas[i - z];
- }
- cout << *this << endl << endl;
- }
- // Перегрузка приравнивания для многочлена.
- void operator= (polinom &inPol) {
- // Если многочлен не пустой (размер != 0), очищаем его.
- if (size)
- clearPol();
- size = inPol.size;
- coefMas = new double[size];
- for (size_t i = 0; i < size; i++)
- coefMas[i] = inPol.coefMas[i];
- }
- // Перегрузка плюса для многочленов.
- polinom operator+ (polinom &inPol) {
- polinom outPol;
- if (size >= inPol.size) {
- outPol = *this;
- for (size_t i = 0; i < inPol.size; i++)
- outPol.coefMas[i] += inPol.coefMas[i];
- }
- else {
- outPol = inPol;
- for (size_t i = 0; i < size; i++)
- outPol.coefMas[i] += coefMas[i];
- }
- return outPol;
- }
- // Перегрузка минуса для многочленов.
- polinom operator- (polinom &inPol) {
- polinom outPol;
- if (size >= inPol.size) {
- outPol = *this;
- for (size_t i = 0; i < inPol.size; i++)
- outPol.coefMas[i] -= inPol.coefMas[i];
- }
- else {
- outPol = inPol;
- for (size_t i = 0; i < outPol.size; i++)
- outPol.coefMas[i] = -outPol.coefMas[i];
- for (size_t i = 0; i < size; i++)
- outPol.coefMas[i] += coefMas[i];
- }
- return outPol;
- }
- // Перегрузка умножения для многочленов.
- polinom operator* (polinom &inPol) {
- polinom outPol;
- // Именно таким будет размер нового многочлена после умножения двух других.
- size_t bigSize = inPol.size + size;
- outPol.coefMas = new double[bigSize];
- outPol.size = bigSize;
- for (size_t i = 0; i < outPol.size; i++)
- outPol.coefMas[i] = 0;
- // [i+j] сложение степеней при умножении.
- for (size_t i = 0; i < size; i++)
- for (size_t j = 0; j < inPol.size; j++)
- outPol.coefMas[i+j] += coefMas[i] * inPol.coefMas[j];
- return outPol;
- }
- // Метод взятия производной.
- void diff() {
- for (size_t i = 0, j = 1; j < size; i++, j++)
- coefMas[i] = j * coefMas[j];
- // В процессе мы не перезапишем последний элемент. Фикс.
- coefMas[size-1] = 0;
- }
- // Метод получения размера многочлена в переданную переменную.
- void getSize(size_t *size) {
- *size = this->size;
- }
- // Метод очистки многочлена.
- void clearPol() {
- delete[]coefMas;
- size = 0;
- coefMas = NULL;
- }
- // Деструктор объектов.
- ~polinom() {
- // Если многочлен пустой, удалять нечего.
- if (!(size))
- return;
- cout << "The polynomial " << *this << " is destroyed. [" << coefMas << "]" << endl;
- delete[]coefMas;
- }
- };
- // Переопределение потока вывода.
- ostream& operator<< (ostream &out, const polinom &inPol) {
- if (!(inPol.size)) {
- out << "This polynomial is empty." << endl;
- return out;
- }
- // Что бы не начать читать с границы массива.
- size_t i = inPol.size - 1;
- for (; i > 0; i--)
- if (inPol.coefMas[i] == 1)
- out << "+(x^" << i << ")";
- else if (inPol.coefMas[i] == -1)
- out << "-(x^" << i << ")";
- else if (inPol.coefMas[i] > 0)
- out << "+(" << inPol.coefMas[i] << "x^" << i << ")";
- else if (inPol.coefMas[i] < 0)
- out << "-(" << -inPol.coefMas[i] << "x^" << i << ")";;
- // Отдельный вывод последнего коэф.
- if (!(inPol.coefMas[0]))
- out << "=0" << endl;
- else if (inPol.coefMas[0] > 0)
- out << "+(" << inPol.coefMas[0] << ")";
- else
- out << "-(" << -inPol.coefMas[0] << ")";
- out << "=0";
- return out;
- }
- int main() {
- size_t size1, size2;
- cout << "What is the highest degree of a polynomial Num1?" << endl;
- cin >> size1;
- polinom pol1(size1);
- cout << "What is the highest degree of a polynomial Num2?" << endl;
- cin >> size2;
- polinom pol2(size2);
- polinom sum1, sum2, raz1, raz2, mul1, mul2;
- sum1 = pol1 + pol2;
- sum2 = pol2 + pol1;
- raz1 = pol1 - pol2;
- raz2 = pol2 - pol1;
- mul1 = pol1 * pol2;
- mul2 = pol2 * pol1;
- cout << endl;
- cout << "pol1: " << pol1 << endl;
- cout << "pol2: " << pol2 << endl;
- cout << "-------------------------------------------------------------" << endl;
- cout << "pol1 + pol2: " << sum1 << endl;
- cout << "pol2 + pol1: " << sum2 << endl;
- cout << "-------------------------------------------------------------" << endl;
- cout << "pol1 - pol2: " << raz1 << endl;
- cout << "pol2 - pol1: " << raz2 << endl;
- cout << "-------------------------------------------------------------" << endl;
- cout << "pol1 * pol2: " << mul1 << endl;
- cout << "pol2 * pol1: " << mul2 << endl;
- pol1.diff();
- pol2.diff();
- cout << "-------------------------------------------------------------" << endl;
- cout << "diff(pol1): " << pol1 << endl;
- cout << "diff(pol2): " << pol2 << endl;
- cout << endl << "Exiting.........................." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement