Divide method in the polynomial.cpp
In C++, I only need help with the Divide method in the polynomial.cpp file.
this is polynomial cpp file
#include "polynomial.h"
#include
#include
#include
#include
#include
using std::istream;
using std::ostream;
using std::string;
using std::stringstream;
using std::fixed;
using std::setprecision;
using std::showpos;
Polynomial::Polynomial(size_t degree) : _degree(degree){
_coefficients = new float[_degree + 1];
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] = 0.0;
}
}
Polynomial::Polynomial(size_t degree, const float* coefficients): _degree(degree){
_coefficients = new float[_degree + 1];
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] = coefficients[i];
}
}
Polynomial::Polynomial(const Polynomial& polynomial): _degree(polynomial._degree){
_coefficients = new float[_degree + 1];
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] = polynomial._coefficients[i];
}
}
Polynomial::~Polynomial(){
// DO THIS FIRST TO PREVENT MEMORY LEAKS!
}
const Polynomial Polynomial::Sum(const Polynomial& rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Subtract(const Polynomial& rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Minus()const{
Polynomial retVal(*this);
for (size_t i = 0; i < _degree + 1; i++) {
retVal._coefficients[i] *= -1;
}
return retVal;
}
const Polynomial Polynomial::Multiply(const Polynomial& rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Divide(const Polynomial& rhs)const{
return Polynomial(0);
}
const Polynomial Polynomial::Derive()const{
return Polynomial(0);
}
float Polynomial::Evaluate(float x)const{
return FLT_MAX;
}
float Polynomial::Integrate(float start, float end)const{
return FLT_MAX;
}
const Polynomial& Polynomial::operator=(const Polynomial& rhs){
if (&rhs == this){
return *this;
}
if (_degree != rhs._degree){
if (_coefficients){
delete[] _coefficients;
}
_degree = rhs._degree;
_coefficients = new float[_degree + 1];
}
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] = rhs._coefficients[i];
}
return *this;
}
bool Polynomial::Equals(const Polynomial& rhs)const{
if (_degree != rhs._degree){
return false;
}
for (size_t i=0; i < _degree; i++){
if (abs(_coefficients[i] - rhs._coefficients[i]) > 0.0001){
return false;
}
}
return true;
}
string Polynomial::ToString()const{
stringstream ss;
for (size_t i = _degree; i > 0; i--) {
ss << showpos << fixed << setprecision(2) << _coefficients[i] << "x^" << i << " ";
}
ss << showpos << fixed << setprecision(2) << _coefficients[0];
return ss.str();
}
ostream& Polynomial::Write(ostream& output)const{
output << _degree << " ";
for (size_t i = 0; i < _degree + 1; i++) {
output << _coefficients[i] << " ";
}
return output;
}
istream& Polynomial::Read(istream& input){
size_t degree;
input >> degree;
if (input.fail()){
return input;
}
float* coefficients = new float[degree + 1];
for (size_t i = 0; i < degree + 1; i++) {
input >> coefficients[i];
if (input.fail()){
delete[] coefficients;
return input;
}
}
if (degree != _degree){
if (_coefficients){
delete[] _coefficients;
}
_degree = degree;
_coefficients = coefficients;
}else{
for (size_t i = 0; i < _degree + 1; i++) {
_coefficients[i] = coefficients[i];
}
delete[] coefficients;
}
return input;
}
----------------------------------
this is the test file cpp. we must test the code on this
#include "polynomial.h"
#include
#include
using std::cout;
using std::endl;
using std::stringstream;
int main(int argc, char* argv[]){
stringstream data[5];
data[0].str("3 -1 2 0 -2.5");
data[1].str("4 -1 -2 0 0 3");
data[2].str("2 -1 0 4");
data[3].str("3 -6 -5 2 1");
data[4].str("1 -2 1");
stringstream answers[6];
answers[0].str("4 -2 0 0 -2.5 3"); // a + b
answers[1].str("7 1 0 -4 2.5 2 6 0 -7.5"); // a * b
answers[2].str("2 3 4 1"); // d / e
answers[3].str("2 1 0 -4"); // -c
answers[4].str("4 0 -4 0 2.5 3"); // b - a
answers[5].str("2 2 0 -7.5"); // d/dx a
Polynomial a(0), b(0), c(0), d(3), e(1), r(0);
a.Read(data[0]);
b.Read(data[1]);
c.Read(data[2]);
d.Read(data[3]);
e.Read(data[4]);
Polynomial s(0), t(0), u(0), v(0), w(0), x(0);
s.Read(answers[0]);
t.Read(answers[1]);
u.Read(answers[2]);
v.Read(answers[3]);
w.Read(answers[4]);
x.Read(answers[5]);
float total = 0;
if (d.Divide(e).Equals(u)){
cout << "\tDIVIDE PASSED " << ++total << endl;
}else{
cout << "\tDIVIDE FAILED" << endl;
}
return 0;
}
-----------------------------------------------
Step by step
Solved in 2 steps