Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <string>
- #include <iostream>
- //todo init, add comments.
- using namespace std;
- class poly
- {
- double coefficient[3];
- public:
- poly();
- poly(const poly& other);
- const poly& operator=(const poly&);
- bool operator==(const poly&);
- bool operator!=(const poly&);
- bool operator<(const poly&);
- bool operator>(const poly&); //surely there's a better way to implement those
- bool operator<=(const poly&);
- bool operator>=(const poly&);
- friend ostream& operator<<(ostream& os, const poly& vec)
- {
- for(int i=0;i<3;i++)
- {
- os << "poly=" << round(vec.coefficient[i]) << "x^:" << i<<"*";
- return os;
- }
- }
- poly& operator+(const poly&);
- poly& operator-(const poly & p1);
- poly operator*(const double);
- poly operator/(const double);
- ~poly();
- };
- poly::poly()
- {
- for (int i = 0; i < 3; i++)
- {
- coefficient[i] = 0;
- }
- }
- poly::~poly()
- {
- delete[] coefficient; //dont think it's neccesary as there are no pointers, might need to delete this before hand-in
- }
- poly::poly(const poly& other)
- {
- for (int i = 0; i < 3; i++)
- {
- if (other.coefficient[i] != NULL)
- {
- this->coefficient[i] = other.coefficient[i];
- }
- if (other.coefficient[i] == NULL)
- {
- this->coefficient[i] = 0; //will come in handy in case we try to evaluate 1st and/or 2nd degree polynomials
- }
- }
- }
- const poly& poly::operator=(const poly& other)
- {
- for (int i = 0; i < 3; i++)
- {
- if (other.coefficient[i] != NULL)
- {
- this->coefficient[i] = other.coefficient[i];
- }
- if (other.coefficient[i] == NULL)
- {
- this->coefficient[i] = 0;
- }
- }
- return *this;
- }
- bool poly::operator==(const poly& other)
- {
- int trigger = 0; //if all 3 slots are equal then then polynomials are equal as well
- for (int i = 0; i < 3; i++)
- {
- if (this->coefficient[i] == other.coefficient[i])
- {
- trigger++;
- if (trigger <= 3 && i == 3)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- }
- bool poly::operator!=(const poly& other)
- {
- for (int i = 0; i < 3; i++)
- {
- if (this->coefficient[i] != other.coefficient[i])
- {
- return 1;
- }
- else { return 0; };
- }
- }
- bool poly::operator<(const poly& other)
- {
- for (int i = 0; i < 3; i++)
- {
- if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
- {
- return 1;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
- {
- return 0;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
- {
- if (this->coefficient[i] > other.coefficient[i])
- {
- return 0;
- }
- if (this->coefficient[i] < other.coefficient[i])
- {
- return 1;
- }
- }
- }
- }
- bool poly::operator<=(const poly& other)
- {
- for (int i = 0; i < 3; i++)
- {
- if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
- {
- return 1;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
- {
- return 0;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
- {
- if (this->coefficient[i] >= other.coefficient[i])
- {
- return 0;
- }
- if (this->coefficient[i] <= other.coefficient[i])
- {
- return 1;
- }
- }
- }
- }
- bool poly::operator>(const poly& other)
- {
- for (int i = 0; i < 3; i++)
- {
- if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
- {
- return 0;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
- {
- return 1;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
- {
- if (this->coefficient[i] > other.coefficient[i])
- {
- return 1;
- }
- if (this->coefficient[i] < other.coefficient[i])
- {
- return 0;
- }
- }
- }
- }
- bool poly::operator>=(const poly& other)
- {
- for (int i = 0; i < 3; i++)
- {
- if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
- {
- return 0;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
- {
- return 1;
- }
- if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
- {
- if (this->coefficient[i] >= other.coefficient[i])
- {
- return 1;
- }
- if (this->coefficient[i] <= other.coefficient[i])
- {
- return 0;
- }
- }
- }
- }
- //poly& poly::init(char s[50]) //a3*x ^ 3 + a2*x ^ 2 + a1*x ^ 1 + a0*x ^ 0
- //{
- // for (int i = 0; i < 50; i++)
- // {
- //
- // }
- //}
- poly& poly::operator+(const poly& p1)
- {
- poly temp;
- for (int i = 0; i < 3; i++)
- {
- temp.coefficient[i]=p1.coefficient[i] + this->coefficient[i];
- }
- return temp;
- }
- poly& poly::operator-(const poly& p1)
- {
- poly temp;
- for (int i = 0; i < 3; i++)
- {
- temp.coefficient[i]=p1.coefficient[i] - this->coefficient[i];
- }
- return temp;
- }
- poly poly::operator*(const double d)
- {
- poly pp = *this;
- for (int i = 0; i < 3; i++)
- {
- pp.coefficient[i] *= d; //todo other way around something,see instructions
- }
- return pp;
- }
- poly poly::operator/(const double d)
- {
- if (d != 0)
- {
- poly pp = *this;
- for (int i = 0; i < 3; i++)
- {
- pp.coefficient[i] /= d; //todo other way around something,see instructions
- }
- return pp;
- }
- else
- {
- cout << "are you trying to fuck up my computer?";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement