Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "poly.h"
- #include <algorithm> //max min
- using namespace std;
- poly::poly() {
- this->wsp = new double[1];
- this->wsp[0] = 0.0;
- this->size = 1;
- }
- poly::poly(double a) {
- this->wsp = new double[1];
- this->wsp[0] = a;
- this->size = 1;
- }
- bool operator==(const poly& a, const poly& b) {
- return (a.size == b.size && a.wsp == b.wsp);
- }
- poly& poly::operator=(const poly& a) {
- if (*this == a || a.wsp == NULL)
- return *this;
- delete[] wsp;
- this->wsp = new double[a.size];
- if (wsp == NULL)
- exit(2);
- size = a.size;
- memcpy(this->wsp, a.wsp, size * sizeof(double));
- return *this;
- }
- double& poly::operator[](int index) {
- if (index < size)
- return this->wsp[index];
- else {
- double * realok=new double[index+1];
- if (realok == NULL) {
- delete[] this->wsp;
- abort();
- }
- /*for (int i = 0; i < this->size; i++) {
- realok[i] = 0;
- }*/
- memcpy(realok, this->wsp,(this->size)*sizeof(double));
- /*for (int i = 0; i < this->size; i++) {
- realok[i] = this->wsp[i];
- }*/
- delete[] this->wsp;
- this->wsp = realok;
- this->size = index + 1;
- return this->wsp[index];
- }
- }
- poly operator+(const poly& a, const poly& b) {
- poly wynik;
- wynik.wsp = new double[max(a.size, b.size)];
- wynik.size = max(a.size, b.size);
- //wynik.wsp.resize(max(a.wsp.size(), b.wsp.size()), 0.0);
- auto i = 0;
- for (i = 0; i != max(a.size, b.size); i++) {
- wynik.wsp[i] = a.wsp[i] + b.wsp[i];
- }
- if (wynik.size == a.size) {
- for (auto j = i; j != a.size; j++)
- wynik.wsp[j] = a.wsp[j];
- }
- else {
- for (auto j = i; j != b.size; j++)
- wynik.wsp[j] = b.wsp[j];
- }
- return wynik;
- }
- poly operator*(const poly & a, const poly & b) {
- poly wynik;
- wynik.wsp = new double[a.size + b.size - 1];
- wynik.size = a.size + b.size - 1;
- for (auto i = 0; i != wynik.size; i++) wynik[i] = 0;
- for (auto i = 0; i < a.size; i++) {
- for (auto j = 0; j < b.size; j++) {
- wynik[i + j] += a.wsp[i] * b.wsp[j];
- }
- }
- return wynik;
- }
- poly operator*(const double& a, const poly & b) {
- poly wynik;
- wynik.wsp= new double[b.size];
- wynik.size = b.size;
- for (auto i = 0; i != b.size; i++) {
- wynik.wsp[i] = a * b.wsp[i];
- }
- return wynik;
- }
- double poly::operator()(const double& a) {
- double suma = 0.0;
- for (auto i = size; i >= 0; i--) {
- suma += this->wsp[i];
- if (i - 1 != this->wsp[0])
- suma *= a;
- }
- return suma;
- }
- ostream& operator<<(ostream & s, const poly & a) {
- auto potega = a.size - 1;
- if (potega == -1) {
- s << 0;
- return s;
- }
- for (auto i = a.size-1; i != 0; i--, potega--) {
- double index = a.wsp[i];
- if (index != 0) {
- if (index > 0 && potega != a.size - 1)
- s << " + ";
- }
- if (potega == 0) s << index;
- else if (index == 1) s << "x";
- else if (index == -1) s << "-x";
- else if (index == 0) s << " + 0x";
- else s << index << "x";
- if (potega > 1) s << "^" << potega;
- }
- return s;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement