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; // Массив коэф. многочлена.
- 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];
- }
- displayPol();
- cout << 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 < size; i++)
- outPol.coefMas[i] = -(outPol.coefMas[i]) + coefMas[i];
- }
- return outPol;
- }
- // Метод получения размера многочлена в переданную переменную.
- void getSize(size_t *size) {
- *size = this->size;
- }
- // Вывод многочлена в консоль.
- void displayPol() {
- // Если вдруг попытаемся вывести пустой многочлен (size = 0).
- 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";
- }
- // Метод очистки многочлена.
- void clearPol() {
- for (size_t i = 0; i < size; i++)
- coefMas[i] = NULL;
- delete[]coefMas;
- size = 0;
- coefMas = NULL;
- }
- 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 << ")";
- }
- // Деструктор объектов.
- ~polinom() {
- // Если многочлен пустой, удалять нечего.
- if (!(coefMas))
- return;
- cout << "The polynomial ";
- displayPol();
- cout << " was deleted. [" << coefMas << "]" << endl;
- delete[]coefMas;
- }
- };
- int main() {
- cout << "What is the highest degree of a polynomial(1)?" << endl;
- size_t size1;
- cin >> size1;
- polinom pol1(size1);
- cout << "What is the highest degree of a polynomial(2)?" << endl;
- size_t size2;
- cin >> size2;
- polinom pol2(size2);
- polinom sum1, sum2, raz1, raz2;
- sum1 = pol1 + pol2;
- sum2 = pol2 + pol1;
- raz1 = pol1 - pol2;
- raz2 = pol1 - pol2;
- cout << endl << endl << "pol1:"; pol1.displayPol(); cout << endl;
- cout << "pol2:"; pol2.displayPol(); cout << endl;
- cout << "-------------------------------" << endl;
- cout << "pol1 + pol2: "; sum1.displayPol(); cout << endl;
- cout << "pol2 + pol1:"; sum2.displayPol(); cout << endl;
- cout << "pol1 - pol2:"; raz1.displayPol(); cout << endl;
- cout << "pol2 - pol1:"; raz2.displayPol(); cout << endl;
- cout << endl << endl << "Exiting..." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement