in C++ Start with the polynomial.cpp file provided. This is a basic outline of t
ID: 3714895 • Letter: I
Question
in C++ Start with the polynomial.cpp file provided. This is a basic outline of the class, which reads the in the polynomial from the keyboard. You must overload the multiplication operator to multiply
polynomials together (i.e., FOIL arbitrary dimension polynomials1). Also overload for cout and make sure you follow exactly the formatting shown below.The program provided has no memory leaks, and your submission should not leak memory as well. In other words, if you create dynamic memory you also need to delete it somewhere in the code. [Hint: What is the relationship to the index and the power of “x”?]
Example 1 (user input is underlined):
What are the coefficients of first polynomial (lowest power first)?
1 2 3
What are the coefficients of second polynomial (lowest power first)?
4 5 6
(1) + (2)x^1 + (3)x^2
... X ...
(4) + (5)x^1 + (6)x^2
------------
(4) + (13)x^1 + (28)x^2 + (27)x^3 + (18)x^4
Example 2 (user input is underlined):
What are the coefficients of first polynomial (lowest power first)? 1
-2 1
What are the coefficients of second polynomial (lowest power first)?
-1
(1) + (-2)x^1 + (1)x^2
... X ...
(-1)
------------
(-1) + (2)x^1 + (-1)x^2
Example 3 (user input is underlined):
What are the coefficients of first polynomial (lowest power first)?
1 2 3 4 5
What are the coefficients of second polynomial (lowest power first)?
1 -2 1 -4
(1) + (2)x^1 + (3)x^2 + (4)x^3 + (5)x^4
... X ...
(1) + (-2)x^1 + (1)x^2 + (-4)x^3
------------
(1) + (0)x^1 + (0)x^2 + (-4)x^3 + (-8)x^4 + (-18)x^5 + (-11)x^6 + (-20)x^7
Polynomial cpp
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
class poly{
private:
int* coef;
int length;
public:
poly();
poly(int* c, int l);
~poly();
poly operator=(const poly& p);
poly(const poly& p);
friend istream& operator>>(istream& in, poly& p);
friend ostream& operator<<(ostream& out, poly p);
poly operator*(poly p);
};
int main()
{
poly p1, p2;
cout << "What are the coefficients of first polynomial (lowest power first)? ";
cin >> p1;
cout << "What are the coefficients of second polynomial (lowest power first)? ";
cin >> p2;
cout << p1 << endl; // MAKE THIS
cout << "... times... " << endl;
cout << p2 << endl; // MAKE THIS
cout << "------------ ";
poly p3 = p1 * p2; // MAKE THIS
cout << p3 << endl; // MAKE THIS TOO!
return 0;
}
poly::poly()
{
coef = new int[1];
coef[0] = 0;
length = 1;
}
poly::poly(int* c, int l)
{
length = l;
coef = new int[l];
for(int i=0; i < l; i++)
{
coef[i] = c[i];
}
}
poly poly::operator=(const poly& p)
{
length = p.length;
delete [] coef;
coef = new int[length];
for(int i=0; i < length; i++)
{
coef[i] = p.coef[i];
}
poly result(p.coef, p.length);
return result;
}
poly::poly(const poly& p)
{
coef = new int[p.length];
//*this = p; // this line was causing errors for some people
length = p.length;
for(int i=0; i < length; i++)
{
coef[i] = p.coef[i];
}
}
poly::~poly()
{
delete [] coef;
}
istream& operator>>(istream& in, poly& p)
{
string s;
getline(in, s);
int numberCount = 0;
bool>
// first loop counts amount of numbers
for(int i=0; i < static_cast<int>(s.length()); i++)
{
if(isdigit(s[i]) && !onNumeric)
{
numberCount++;
onNumeric=true;
}
if(!isdigit(s[i]) && onNumeric)
{
onNumeric=false;
}
}
// delete old poly & replace with correct size
delete [] p.coef;
p.length = numberCount;
if(numberCount == 0) // case if nothing entered (don't want create 0 length array...)
{
p.coef = new int[1];
p.coef[0] = 0;
return in;
}
p.coef = new int[numberCount];
// loop through again to fill array (similar logic to last loop)
onNumeric = false;
int previousIndex = 0; // used to find negative signs
int lastIndex=0;
int coefIndex=0;
for(int i=0; i < static_cast<int>(s.length()); i++)
{
if(isdigit(s[i]) && !onNumeric)
{
previousIndex=lastIndex;
lastIndex=i;
onNumeric = true;
}
if(!isdigit(s[i]) && onNumeric)
{
string currentNumber = s.substr(lastIndex, i-lastIndex);
p.coef[coefIndex] = atoi(currentNumber.c_str());
string before = s.substr(previousIndex, lastIndex-previousIndex);
if(static_cast<int>(before.find('-')) != -1)
{
p.coef[coefIndex] *= -1;
}
coefIndex++;
onNumeric = false;
}
}
// case if last character is part of the digit
if(onNumeric)
{
string currentNumber = s.substr(lastIndex);
p.coef[coefIndex] = atoi(currentNumber.c_str());
string before = s.substr(previousIndex, lastIndex-previousIndex);
if(static_cast<int>(before.find('-')) != -1)
{
p.coef[coefIndex] *= -1;
}
}
return in;
}
Explanation / Answer
EXECUTABLE CODE
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
struct polynomial{
int coefficient;
int power;
poly *next;
};
class multiply2polynomial
{
polynomial poly1, poly2, *poly3;
public:
muliply2polynomial(){poly1=poly2=poly3=NULL;}
void multiplypoly();
void displaypplynomials();
};
void muliply2polynomial :: multiplypoly(){
int i,p;
polynomial *newl=NULL,*end=NULL;
cout<<"Enter highest power for x ";
cin>>p;
cout<<" Please enter the first Polynomial ";
for(i=p;i>=0;i--)
{
newl=new polynomial;
newl->power=i;
cout<<"now lets enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coefficient;
newl->next=NULL;
if(poly1==NULL)
poly1=newl;
else
end->next=newl;
end=newl;
}
cout<<" Please enter the second Polynomial ";
end=NULL;
for(i=p;i>=0;i--)
{
newl=new polynomial;
newl->power=i;
cout<<"now lets enter Co-efficient for degree"<<i<<":: ";
cin>>newl->coefficient;
newl->next=NULL;
if(poly2==NULL)
poly2=newl;
else
end->next=newl;
end=newl;
}
polynomial *p1=poly1,*p2=poly2;
int flag;
end=NULL;
while(p1 !=NULL){
p2=poly2;
while(p2!=NULL){
//if(p1->power == p2->power){
newl=new polynomial;
newl->power=p1->power + p2->power;
newl->coefficient=p1->coefficient * p2->coefficient;
newl->next=NULL;
if(poly3==NULL)
poly3=newl;
else{
flag=1;
polynomial *temp=poly3;
while(temp!=NULL){
if(temp->power==newl->power){
temp->coefficient += newl->coefficient;
flag=0;
}
temp=temp->next;
}
end->next=newl;
}
end=newl;
p2=p2->next;
}
p1=p1->next;
}
}
void multiply2polynomial :: displaypolynomials(){
poly *z=poly3;
cout<<" Answer after polynomial multiplication is is : ";
while(z!=NULL){
cout.setf(ios::showpos);
cout<<z->coeffient;
cout.unsetf(ios::showpos);
cout<<"X"<<z->power;
z=z->next;
}
}
void main(){
clrscr();
multiply2polynomial obj1;
obj1.multiplypoly();
obj1.displaypolynomials();
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.