Click here to Skip to main content
15,915,719 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
hi i need to submit my assignment(due 9/26/2011) on polynomial in C++ with all header(hpp),cpp.I have already given main file and u can find it below
C++
/*
  Polynomial_s.cpp
 
  Syntax checking of the interface of a Polynomial class

  
*/

#include "Polynomial.hpp"
#include <iostream>
#include <cassert>
#include <vector>

int main() {

  Polynomial q;
  Polynomial q2 = q;
  Polynomial q3(q);

  q2 = q;

  // constant constructor
  Polynomial c(5);

  // output
  std::cout << c << '\n';

  // equality
  c == c;

  // inequality
  c != c;

  // addition
  Polynomial a1(2);
  Polynomial a2(3);
  Polynomial a3(5);

  a1 += a2;

  assert(a1 == a3);
  assert(a1 == 5);

  // access coefficients based on degree of term
  a3.coefficient(0);

  // linear constructor
  Polynomial l(2, -3);
  assert(l.coefficient(0) == 2);
  assert(l.coefficient(1) == -3);

  // quadratic constructor
  Polynomial quad(1, 0, -1);
  assert(quad.coefficient(0) == 1);
  assert(quad.coefficient(1) == 0);
  assert(quad.coefficient(2) == -1);

  // vector constructor
  std::vector<double> coeffs(4);
  coeffs[0] = 1; coeffs[1] = 0; coeffs[2] = -1;
  Polynomial poly(coeffs);

  // c-array constructor
  double acoeffs[] = { 1, 0, -1 };
  Polynomial polya(acoeffs, 3);

  // factoring
  std::vector<polynomial> factors = polya.factor();

  return 0;
}
Posted
Updated 23-Jun-23 19:36pm
v3
Comments
Sergey Alexandrovich Kryukov 24-Sep-11 22:01pm    
Not a question. We do not work on your assignments. You need to do your work by yourself to learn something.
--SA
Richard MacCutchan 25-Sep-11 4:05am    
What help, you have not asked a question? See below.

On reflection, and at a guess, you are expecting someone to write the implementation of this class for you so you can hand it to your professor tomorrow, claiming it is all your own work. Sorry, but that just will not happen. You need to ask yourself how you got to a situation to only leave yourself a single weekend to complete this assignment. Maybe because you spent too much time on this[^].
 
Share this answer
 
v2
C++

#include <iostream>
#include <cmath>

using namespace std;

class Polynomial {
private:
int degree;
int* coefficients;

public:
Polynomial(int deg, int* coeffs) : degree(deg), coefficients(coeffs) {}

~Polynomial() {
delete[] coefficients;
}

void printPolynomial() const {
for (int i = degree; i >= 0; i--) {
if (coefficients[i] != 0) {
if (i == degree)
cout << coefficients[i] << "x^" << i;
else {
cout << " ";
if (coefficients[i] > 0)
cout << "+";
cout << coefficients[i] << "x^" << i;
}
}
}
cout << endl;
}
};

int main() {
int degree = 4;
int* coeffs = new int[degree + 1];
coeffs[0] = 5;
coeffs[1] = 0;
coeffs[2] = 2;
coeffs[3] = -1;
coeffs[4] = 3;

Polynomial poly(degree, coeffs);

cout << "Polynomial: ";
poly.printPolynomial();

delete[] coeffs;

return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900