```
#include "POLY.h"
#include <iostream>
using namespace std;
istream& operator>> (istream& stream, Polynomial& pnomial)
{
term t;
bool symbol = true;
pnomial.poly.erase (pnomial.poly.begin(), pnomial.poly.end());
while (stream) // (while not EOF)
{
if (stream.peek() == ';') // are we at the end of the poly?
{ // yes
stream.get(); // read the ';'
break; // end the loop
}
else
{
if (stream.peek() == '+') stream.get(); // skip any '+'s
stream >> t.co; // read the coefficient
if (stream.peek() =='x')
stream >> t.var; // read the 'x'
if (stream.peek() =='^')
{
symbol = true;
stream.get(); // read the '^'
}
if (symbol = true)
{
stream >> t.expo; //read the exponent if there is a '^' before it;
}
pnomial.poly.push_back(t);
}
}
return stream;
}
ostream& operator<< (ostream& stream, Polynomial& pnomial)
{
term t;
for (int i = 0; i < pnomial.poly.size(); i++)
{
if (pnomial.poly[i].expo == 0)
stream << pnomial.poly[i].co << "\t"; // print coefficient only if exponent is 0
if (pnomial.poly[i].expo == 1)
stream << pnomial.poly[i].co+"+"<<pnomial.poly[i].var<< "\t"; // print coefficient plus variable if exponent = 1
else
stream << pnomial.poly[i].co+"+"<<pnomial.poly[i].var+"^"<<pnomial.poly[i].expo<< "\t"; // print format 7x^3
}
return stream;
}
Polynomial::Polynomial()
{
reset(); //default
}
void Polynomial::reset()
{
poly.clear(); //clear polynomial
}
int Polynomial::number_of_terms()
{
return poly.size(); //return number of elements in vector or number of terms in polynomial
}
Polynomial operator+( const Polynomial& p1, const Polynomial& p2 )
{
Polynomial result;
term r;
vector<term>::const_iterator it;
it = p1.poly.begin();
vector<term>::const_iterator it2;
it2 = p2.poly.begin();
do
{
for(int i = 0; i<p1.poly.size(); i++)
{
for (int j=0; j<p2.poly.size(); j++)
{
if(it->expo == it2->expo && it->var == it2->var)
{
r.co = it->co + it2->co;
r.var = it->var;
r.expo = it->expo;
result.poly.push_back(r);
}
else
{
result.poly.push_back(p1.poly[i]);
result.poly.push_back(p2.poly[j]);
}
}
it++;
it2++;
}
}while (it !=p1.poly.end() || it2 !=p2.poly.end());
return result;
}
void Polynomial::sort()
{
poly.begin(), poly.end();
}
bool term::operator< (const term& other_term) const
{
return expo < other_term.expo;
}
```

```
#ifndef POLY_H
#define POLY_H
#include <iostream>
#include <vector>
using namespace std;
struct term
{
int co;
int expo;
char var;
bool operator< (const term& other_term) const;
};
class Polynomial
{
friend istream& operator>> (istream& stream, Polynomial& pnomial);
friend ostream& operator<< (ostream& stream, Polynomial& pnomial);
public:
// The list of terms in the polynomial
// (not necessarily ordered)
vector<term> poly;
// default constructor
Polynomial();
// empties the polynomial of all terms
void reset();
// sort the terms from the highest to lowest exponent
void sort();
// add two polynomials
friend Polynomial operator+( const Polynomial& p1, const Polynomial& p2);
// returns the number of terms in the polynomial
int number_of_terms();
};
#endif
```

```
#include <iostream>
#include <vector>
#include "POLY.h"
using namespace std;
void menu (void)
{ cout<<endl;
cout<<"1. Enter a polynomial"<<endl;
cout<<"2. Reset the polynomial"<<endl;
cout<<"3. Add"<<endl;
cout<<"4. Number of Terms" << endl;
cout<<"5. Print Polynomial"<<endl;
cout<<"6. Quit"<<endl;
cout<<"Enter selection>";
}
void add(Polynomial& P)
{
Polynomial p2, result;
p2.reset();
result.reset();
//enter values for second polynomial
cout<<"Enter terms for second polynomial. Enter ; to stop."<<endl;
cin>>p2;
result = P + p2;
cout<<"Result for added polynomial is:"<<endl;
cout<<"First Polynomial + Second Polynomial: "<<result<<endl;
cout<<"Number of Terms: "<<result.number_of_terms()<<endl;
}
int main (void)
{
int choice;
//declare a Polynomial
Polynomial p;
p.reset();
do {
//print menu
menu();
cin>>choice;
switch (choice) {
case 1 :cout<<endl<<"Enter polynomial(Input must be of the form: 4x^2+3x+6).Enter ; to stop."<<endl;
cin>>p;
cout<<endl;
break;
case 2 : p.reset();
cout<<endl<<"Polynomial reset"<<endl;
break;
case 3: add(p);
break;
case 4: cout<<endl<<"Number of Terms is "<<p.number_of_terms()<<endl;
break;
case 5: cout<< "The polynomial is: " << p << endl;
break;
case 6: cout<<endl<<"Thanks and goodbye."<<endl;
break;
default : cout<<endl<<"Invalid command. Try again."<<endl;
}
} while (choice != 6);
}
```