Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "poly.h"
- using namespace std;
- //Constructors/Destructor
- //----------------------------Poly()------------------------------------------//
- // Base constructor. 0x^0
- //----------------------------------------------------------------------------//
- Poly::Poly() {
- size = 1;
- polyArray = new int[size];
- polyArray[0] = 0;
- }
- //----------------------------Poly(int coeff)---------------------------------//
- // Constructor using a poly of (coeff)x^0.
- //----------------------------------------------------------------------------//
- Poly::Poly(int coeff) {
- size = 1;
- polyArray = new int[1];
- polyArray[0] = coeff;
- }
- //----------------------------Poly(int coeff)---------------------------------//
- // Constructor using a poly of (coeff)x^(poly).
- //----------------------------------------------------------------------------//
- Poly::Poly(int coeff, int pow) {
- size = pow + 1;
- polyArray = new int[size];
- for (int i = 0; i < size - 1; i++) {
- polyArray[i] = 0;
- }
- polyArray[pow] = coeff;
- }
- Poly::Poly(const Poly& insertPoly) {
- size = insertPoly.size;
- polyArray = new int[size];
- for (int i = 0; i < size; i++) {
- polyArray[i] = insertPoly.polyArray[i];
- }
- }
- Poly::~Poly() {
- size = 0;
- delete[] polyArray;
- polyArray = NULL;
- }
- //Getters/Setters
- int Poly::getCoeff(int pow) {
- if (pow > size || pow < 0) return 0;
- return polyArray[pow];
- }
- void Poly::setCoeff(int coeff, int pow) {
- //put in protection for negative powers
- polyArray[pow] = coeff;
- }
- //Binary Math operator overloads
- Poly Poly::operator+(const Poly& input) const {
- if (size < input.size) {
- Poly tempPoly(input);
- // Since the size is smaller, we only copy
- for (int i = 0; i < size; i++) {
- tempPoly.polyArray[i] = polyArray[i] + input.polyArray[i];
- }
- return tempPoly;
- }
- else {
- Poly tempPoly(*this);
- for (int i = 0; i < input.size; i++) {
- tempPoly.polyArray[i] = polyArray[i] + input.polyArray[i];
- }
- return tempPoly;
- }
- }
- //Bolean Operator Overloads
- bool Poly::operator==(const Poly& input) const {
- if (size == input.size) return true;
- for (int i = 0; i < input.size; i++) { // Check each coeff if they're the same.
- if (polyArray[i] != input.polyArray[i]) return false;
- }
- return true;
- }
- bool Poly::operator!=(const Poly& input) const {
- return !(*this == input);
- }
- //IO Operstors
Advertisement
Add Comment
Please, Sign In to add comment