Question: c++ polynomial.cpp #include #include #include #include using namespace std; class poly{ private: int* coef; int length; public: poly(); poly(int* c, int l); ~poly(); poly operator=(const

c++

c++ polynomial.cpp #include #include #include #include using namespace std; class poly{ private:int* coef; int length; public: poly(); poly(int* c, int l); ~poly(); poly

polynomial.cpp

#include  #include  #include  #include  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> p1; cout > p2; cout >(istream& in, poly& p) { string s; getline(in, s); int numberCount = 0; bool onNumeric = false; // first loop counts amount of numbers for(int i=0; i (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 (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(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(before.find('-')) != -1) { p.coef[coefIndex] *= -1; } } return in; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!