Question

C++ Question Polynomial Calculator (Will Rate All Answers)

Hello, this question will be divided into many parts, this is the second part. Will quickly rate your answer! Thank you.

First part is here : https://www.chegg.com/homework-help/questions-and-answers/c-question-polynomial-calculator-rate-answers-hello-question-divided-many-parts-s-first-pa-q39525382

- Scalar Polynomial Multiplication: const Polynomial operator* (const Polynomial& P, double z) The scalar multiplication P *

This question has to do with operators and polynomials.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include<iostream>
#include <string>
#include <cstdlib>
#include<math.h>

using namespace std;

//Polynomial class
class Polynomial
{
//private members
   private:
       int *exp;
       double *coef;
       int n;
//public members
   public:
   Polynomial();
       Polynomial(string s);
       Polynomial(const Polynomial&);
       double& operator[](int n);
       double operator[](int n) const;
       double operator()(double x) const;
       friend const Polynomial operator* (const Polynomial&, double);
       friend istream& operator>>(istream&, Polynomial&);
       friend ostream& operator<<(ostream&, Polynomial&);
~Polynomial(){}
};

//default constructor
Polynomial::Polynomial()
{
n=0;
exp=NULL;
coef=NULL;
}

//constructor
Polynomial::Polynomial(string s)
{
try{
   n=1;
   for(int i=0; i<s.size(); i++)
   {
       if(s[i]=='+' || s[i]=='-')
           n++;
   }
   exp = new int[n];
   coef = new double[n];

   s.append("+");

   int j=0, k=0, l=0;
   for(int i=0; i<s.size(); i++)
   {
       if(s[i]=='x'){
           coef[k] = stod(s.substr(j,i-j));
           k++;
       }
       if(s[i]=='+' || s[i]=='-'){
           j=i;
       }
   }
   j=0;
   for(int i=0; i<s.size(); i++)
   {
   if(s[i]=='^'){
           j=i+1;
       }
       if(s[i]=='+' || s[i]=='-'){
           exp[l] = stoi(s.substr(j,i-j));
           l++;
       }
   }
}catch(...){
cout<<"Invalid polynomial"<<endl;
}
}

//copy constructor
Polynomial::Polynomial(const Polynomial& p)
{
n = p.n;
coef = new double[n];
exp = new int[n];
for(int i=0; i<n; i++)
{
coef[i] = p.coef[i];
exp[i] = p.exp[i];
}
}

//operator * overloading
const Polynomial operator* (const Polynomial& p, double z)
{   
Polynomial q;
q.n = p.n;
q.coef = new double[p.n];
q.exp = new int[p.n];
  
for(int i=0; i<p.n; i++)
{
q.coef[i] = z * p.coef[i];
q.exp[i] = p.exp[i];
}
return q;
}

//operator >> overlaoding
istream& operator>>(istream& in, Polynomial& p)
{
string s;
in>>s;

try{
   p.n=1;
   for(int i=0; i<s.size(); i++)
   {
       if(s[i]=='+' || s[i]=='-')
           p.n++;
   }
   p.exp = new int[p.n];
   p.coef = new double[p.n];

   s.append("+");

   int j=0, k=0, l=0;
   for(int i=0; i<s.size(); i++)
   {
       if(s[i]=='x'){
           p.coef[k] = stod(s.substr(j,i-j));
           k++;
       }
       if(s[i]=='+' || s[i]=='-'){
           j=i;
       }
   }
   j=0;
   for(int i=0; i<s.size(); i++)
   {
   if(s[i]=='^'){
           j=i+1;
       }
       if(s[i]=='+' || s[i]=='-'){
           p.exp[l] = stoi(s.substr(j,i-j));
           l++;
       }
   }
}catch(...){
cout<<"Invalid polynomial"<<endl;
}
return in;
}

//operator << overloading
ostream& operator<<(ostream& out, Polynomial& p)
{
for(int i=0; i<p.n; i++)
{
if(i!=0 && p.coef[i]>0) out<<"+";
out<<p.coef[i]<<"x^"<<p.exp[i];
}
out<<endl;
  
return out;
}

//operator [] lvalue overlaoding
double& Polynomial::operator[](int n)
{
if(n<0) throw "invalid index";
return coef[n];
}

//operator [] rvalue overlaoding
double Polynomial::operator[](int n) const
{
if(n<0) throw "invalid index";
return coef[n];
}

//operator () overlaoding
double Polynomial::operator()(double x) const
{
double d = 0;
for(int i=0; i<n; i++)
{
d = d + coef[i]*pow(x, exp[i]);
}
return d;
}


//main function
int main()
{
   string s1 = "4.5x^8+3.4x^3-6.4x^1+3.1x^0";
   string s2 = "2.1x^2+0.8x^1-1.5x^0";

   Polynomial p1(s1);
   Polynomial p2(s2);
   Polynomial p3;

   cout<<p1;
cout<<p2;
  
double d = 2;

cout<<"After multiplication:"<<endl;
   p3 = p1 * d;
cout<<p3;
  
d = p2(1.2);
cout<<"At x=1.2, value of the polynomial is "<<d<<endl;
d = p2(0);
cout<<"At x=0, value of the polynomial is "<<d<<endl;
  
cout<<p2;
d=p2[2];
cout<<"rvalue of the 2nd coefficient value is "<<d<<endl;
p2[2]=5;
d=p2[2];
cout<<"After changing rvalue of the 2nd coefficient value is "<<d<<endl;
  
Polynomial p4;
cin>>p4;
cout<<p4;
  
   return 0;
}

Output Screen

4.5x^8+3.4x^3 - 6.4x^1+3.1x^0 2.1x^2+0.8x^1-1.5x^0 After multiplication : |9x^8+6.8x^3-12.8x^1+6.2x^0 At x-1.2, value of the

N.B.Whether you face any problem then share with me.

Add a comment
Know the answer?
Add Answer to:
C++ Question Polynomial Calculator (Will Rate All Answers) Hello, this question will be divided into many...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • QUESTION 18 According to class materials, the program is allowed to overload operators only if at...

    QUESTION 18 According to class materials, the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type. 1. (a) True 2. (b) False QUESTION 19 According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in...

  • QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is...

    QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is no catch block handling the same data type as the exception thrown execution continues if the catch block handles the same data type as the exception thrown 10 points    QUESTION 2 An exception is handled using an if-else statement an assignment statement a loop statement a try-catch block 10 points    QUESTION 3 In program 9.4, what is the data type being thrown?...

  • please provide full answer with comments this is just begining course of c++ so don't use...

    please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank you main.cpp #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include <iostream> using std::cout; using std::endl; int main() { pic10a::polynomial p1; p1.setCoeff(0, 1.2); p1.setCoeff(3, 2.2); p1.setCoeff(7, -9.0); p1.setCoeff(7, 0.0); //degree of polynomial is now 3 cout << p1 <<...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • The task is to write Song.cpp to complete the implementation of the Song class, as defined...

    The task is to write Song.cpp to complete the implementation of the Song class, as defined in the provided header file, Song.h, and then to complete a program (called sales) that makes use of the class. As in Project 1, an input data file will be provided containing the song name and other information in the same format as in Program 1: Artist    Song_Title    Year    Sales    Medium As before, the program (sales) which are individual copies, will use this information to compute the gross...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT