Hello c++! I'm here cause i need some help with my term proyect. This is my proyect description...
Design and implant a Polynomial class, in which a polynomial is implemented as a linked list of coefficients. Each node will have an integer to hold the exponent of the term and a real number to hold the coefficient the term. For example, 5.6x3- 6x2+3.5 will be in the linked list as (3, 5.6) (2, ‐6) (1, 0) (0, 3.5)
Overload All the usual polynomial operators (+, –, *), Input and output operators The polynomial value when x value is given. I.e. f(x)= 5.6x3- 6x2+3.5 for x=1.5.
But instead of the linked list, I am using arrays. I have my header file, my implementation and my demo_program. It is running, it ask the user to enter 2 polynomial and its size, but here is where i need help. I need a function or a code to ask them if they want to add, substract or multiply those 2 polynomials. Can some one help me out to finish it please!? :C
Header file:
//Polynomial.h
//
#ifndef polynomial_H
#define polynomial_H
#include <iostream>
#include <cstdlib>
usingnamespace std;
class Polynomial
{
private:
double *Ncoefficients; //It is the array where we store the coefficients
int Pdegree; //It is the degree of the polynomial ( one less then the length of the array of coefficients)
public:
Polynomial(); //the default constructor to initialize a polynomial equal to 0
Polynomial(double[], int); //the constructor to initialize a polynomial with the given coefficient array and degree
Polynomial(Polynomial&); //the copy constructor
Polynomial(double); //the constructor to initialize a polynomial equal to the given constant
double evaluateAt(double x);
void display(void);
~Polynomial() { delete Ncoefficients; } //the destructor to clear up the allocated memory
//the operators to define for the Polynomial class
friend Polynomial operator+(Polynomial & p1, Polynomial & p2)
{
return Polynomial(p1.Ncoefficients + p2.Ncoefficients, p1.exp);
}
Polynomial operator-(Polynomial p) const;
Polynomial operator*(Polynomial p) const;
};
here is my implementation file!:
// Polynomial implementation file
#include <iostream>
#include <cstdlib>
#include "polynomial.h"
usingnamespace std;
void Polynomial();
Polynomial::Polynomial(double Coeffs[], int N_terms)
{
Pdegree = N_terms;
Ncoefficients = newdouble[Pdegree];// allocate an array to hold the coefficient values
for (int i = 0; i<Pdegree; i++)
Ncoefficients[i] = Coeffs[i];// assign in straight order
}
double Polynomial::evaluateAt(double x)
{
double sum = 0.0;
double xPow = 1.0;
if (Ncoefficients)
for (int i = 0; i<Pdegree; i++)
{
sum += xPow*Ncoefficients[i];// add up the terms
xPow *= x;// build up the power of x from term to term
}
return sum;
}
// display it
void Polynomial::display(void)
{
// 1st term
cout << Ncoefficients[Pdegree - 1] << "x^" << Pdegree - 1;
// remaining terms
for (int i = Pdegree - 2; i >= 0; i--)
cout << " + " << Ncoefficients[i] << "x^" << i;
return ;
}
and here is my program main;
// Term Proyect Polynomial
// Demo_program
#include <iostream>
#include <string>
#include "polynomial.h"
usingnamespace std;
int main()
{
int size;
string answer;
Polynomial num3;
cout << "What is going to be the size?" << endl;
cin >> size;
double *array;
array = newdouble[size];
for (int i = 0; i < size; i++)
{
cout << "Enter the coeff of x^ " << i << " : ";
cin >> array[i];
}
Polynomial num1(array, size);
delete array;
cin.ignore();
size = 0;
cout << "What is going to be the size for p2?" << endl;
cin >> size;
double *array2;
array2 = newdouble[size];
for (int i = 0; i < size; i++)
{
cout << "Enter the coeff of x^ " << i << " : ";
cin >> array2[i];
}
Polynomial num2(array2, size);
delete array2;
cout << "What do you want to do? (add, substract or multiply)" << endl;
cin >> answer;
if (answer == "add")
{
int *sum = add()
}
cout << "num = ";
num3.display();
cout << "\n num(2.0) = " << num1.evaluateAt(2.0) << endl;
system("pause");
return 0;
}
Any help is welcome! Thanks guys! I would really appreciate your help :,)
given what you have I have to say you should be able to make a menu to ask the user what to do, so I will skip that.
You need to match powers. Generally one would start at the lowest power, which is zero, and work up through the powers, creating the new terms at each level.
start at the lowest term, the 0th power
are we done? No. do we have any? Yes... so
4 + 0 = 4. new lowest term is just 4, which is power zero, coefficient 4
are we done? no, next is power 1, do we have any? Yes
2x + -2x is zero! We won't store this term!
are we done? No. Next power is 2
3x^2 + -2x^2 is x^2, store (coefficient 1, power 2)
and so on in a loop until done, which you need a way to understand. You could have something silly like
3x^1000
-
2x^2
how do you know when to stop? You have to keep a counter of the # of terms in each poly!
Multiply will be more challenging, and division as well, but see if you can do add and subtract first. Baby steps...