Using malloc and overriding operators with a simple polynomial sorter, all written in C++

Running

Source Code

#include <iostream>
#include <sstream>
#include<stdlib.h>
#include<vector>
#include<math.h>
using namespace std;
class Polynomial
{
  public:
    Polynomial(vector<float> *p, vector<float> *q, int size);
    Polynomial operator+(Polynomial p);
    Polynomial operator-(Polynomial p);
    Polynomial operator*(Polynomial p);
    void display();
 private:
    int getMinimum(int x, int y);
    int getLength();
    Polynomial getNewPolynomial(Polynomial x);
    void sortPolynomial();
    int length;
    float *pol1;
    float *pol2;
};

Polynomial::Polynomial(vector<float> *p, vector<float> *q, int size)
{
  pol1 = (float *) malloc(size * sizeof(float));
  pol2 = (float *) malloc(size * sizeof(float));
  for (int x = 0; x < size; x++)
  {
    pol1[x] = p->at(x);
    pol2[x] = q->at(x);
  }
  length = size;
}

int Polynomial::getLength()
{ 
  return length;
}

int Polynomial::getMinimum(int x, int y)
{
  if (x < y) return x;
  else return y;
}

/*
sorts the current Polynomial and the Polynomial that's passed in, then combines them into another Polynomial and returns that
*/
Polynomial Polynomial::getNewPolynomial(Polynomial q)
{
  sortPolynomial();
  q.sortPolynomial();
  int minimum = getMinimum(getLength(), q.getLength());
  vector<float> p1;
  vector<float> p2;
  float p;
  int x;
  for (x = 0; x < minimum; x++)
  {
    p2.push_back(pol2[x]);
    p2.push_back(q.pol2[x]);
    p1.push_back(pol1[x]);
    p1.push_back(q.pol1[x]);
  }
  if (getMinimum(getLength(), q.getLength()) == getLength())
  {
    for (x = x; x < q.getLength(); x++)
    {
      p1.push_back(q.pol1[x]);
      p2.push_back(q.pol2[x]);
    }
  }
  else // accounts for when they're equal as well
  {
    for (x = x; x < getLength(); x++)
    {
      p1.push_back(pol1[x]);
      p2.push_back(pol2[x]);
    }
  }
  return Polynomial(&p1, &p2, p1.size());  
}

void Polynomial::sortPolynomial()
{
  bool swapped = true;
  int j = 0;
  float tmp1, tmp2;
  while (swapped)
  {
    swapped = false;
    j++;
    for (int i = 0; i < getLength() - j; i++)
    {                                       
      if (pol2[i] > pol2[i + 1])
      {
        tmp1 = pol2[i];
        tmp2 = pol1[i];
        pol2[i] = pol2[i + 1];
        pol1[i] = pol1[i + 1];
        pol2[i + 1] = tmp1;
        pol1[i + 1] = tmp2;
        swapped = true;
      }
    }
  }
}

/*
I think I may be overthinking this.  Something's not quite right.
*/
Polynomial Polynomial::operator+(Polynomial q)
{
  Polynomial p = getNewPolynomial(q);
  vector<float> p1;
  vector<float> p2;
  float f;
  int l = 0;
  bool once = false;
  for (int x = p.getLength(); x > 0; x--)
  {
    l += x;
  }
  for (int z = 0; z < l; z++)
  {
    for (int x = 0; x < p.getLength(); x++)
    {
      for (int y = 1; y < p.getLength(); y++)
      {
        if (p.pol2[x] == p.pol2[y - 1] && !once)
        {
          once = true;
          f = pol1[x] + q.pol1[x];
          p1.push_back(f);
          p2.push_back(pol2[x]);
        }
      }
      once = false;
    }
  }
  return Polynomial(&p1, &p2, p1.size());
}

Polynomial Polynomial::operator-(Polynomial q)
{
  Polynomial x = getNewPolynomial(q);
  return x;
}

Polynomial Polynomial::operator*(Polynomial q)
{
  Polynomial x = getNewPolynomial(q);
  return x;
}

/*
will only show its unsorted first if this is called before any of the overloaded operator functions
*/
void Polynomial::display()
{
  cout << endl;
  for (int i = 0; i < getLength(); i++)
  {
    cout << pol1[i] << "(x^" << pol2[i] << ")+";
  }
  cout << "0 = ";
  sortPolynomial();
  for (int i = 0; i < getLength(); i++)
  {
    cout << pol1[i] << "(x^" << pol2[i] << ")+";
  }
  cout << "0";
}

int main(int argc, char * argv[])
{
  vector<float> p1;
  p1.push_back(2);
  p1.push_back(4);
  p1.push_back(3);
  p1.push_back(5);
  p1.push_back(1);
  vector<float> q1;
  q1.push_back(7);
  q1.push_back(6);
  q1.push_back(3);
  q1.push_back(4);
  q1.push_back(5);
  vector<float> p2;
  p2.push_back(3);
  p2.push_back(8);
  p2.push_back(9);
  p2.push_back(12);
  vector<float> q2;
  q2.push_back(7);
  q2.push_back(2);
  q2.push_back(1);
  q2.push_back(0);
  Polynomial poly1(&p1, &q1, p1.size());
  Polynomial poly2(&p2, &q2, p2.size());
  poly1.display();
  poly2.display();
  Polynomial poly3 = poly1 + poly2;
}