Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- #include <cstddef>
- using std::cin;
- using std::cout;
- using std::endl;
- using std::ostream;
- using std::istream;
- using std::exit;
- using std::pow;
- //ostream& operator << (ostream& out,const Term& object);
- class Term
- {
- public:
- //CONSTRUCTORS
- Term(int theA,int theN,Term* theLink) : a(theA),n(theN),link(theLink){};
- Term(int theA,int theN) : a(theA),n(theN),link(NULL){};
- //ACCESSORS
- int getA() const {return a;}
- int getN() const {return n;}
- Term* getLink() const {return link;}
- //MUTATORS
- void setA(int theA){a = theA;}
- void setN(int theN){n = theN;}
- void setPol(int theA,int theN){a = theA;n = theN;}
- void setLink(Term* theLink){link = theLink;}
- //EVALUATION
- double evaluate(double theX);
- //OVERLOADED OPERATORS
- Term operator + (const Term& object);
- Term operator - (const Term& object);
- Term operator * (const Term& object);
- friend ostream& operator << (ostream& out,const Term& object);
- friend istream& operator >> (istream& in,Term& object);
- //DESTRUCTORS
- //virtual ~Term();
- private: //a*x^n
- int a;
- int n;
- Term* link;
- };
- class Polynomial
- {
- public:
- Polynomial() : top(NULL){};
- Polynomial(int theA) {addNew(theA,0);};
- Polynomial(int theA,int theN) {addNew(theA,theN);}
- Polynomial(const Polynomial& object);
- void addNew(int theA,int theN);
- bool isEmpty() const;
- void simplify();
- void remove(Term* afterMe);
- double evaluate(double theX);
- //OVERLOADED OPERATORS
- Polynomial operator + (const Polynomial& object);
- Polynomial operator - (const Polynomial& object);
- Polynomial operator * (const Polynomial& object);
- friend ostream& operator << (ostream& out,const Polynomial& object);
- friend istream& operator >> (istream& in,Polynomial& object);
- private:
- Term* top;
- };
- int main()
- {
- cout << "PolynomialFULL - By Kudy" << endl;
- cout << "This is a full version and included all necessary tools for polynomials." << endl;
- //TEST 1 : TERM::+ ERROR
- Term left1(2,3,NULL),right1(5,6,NULL);
- cout << "left1 = " << left1 << endl;
- cout << "right1 = " << right1 << endl;
- Term sum1 = left1 + right1;
- cout << "sum1 = " << sum1 << endl;
- cout << "----------\n";
- //TEST 2 : TERM::+ SUCCESS
- Term left2(-2,3,NULL),right2(5,3,NULL);
- cout << "left2 = " << left2 << endl;
- cout << "right2 = " << right2 << endl;
- Term sum2 = left2 + right2;
- cout << "sum2 = " << sum2 << endl;
- cout << "----------\n";
- //TEST3 : TERM::- SUCCESS
- Term left3(-8,3,NULL),right3(-15,3,NULL);
- cout << "left3 = " << left3 << endl;
- cout << "right3 = " << right3 << endl;
- Term dif3 = left3 - right3;
- cout << "dif3 = " << dif3 << endl;
- cout << "----------\n";
- //TEST4 : TERM::* SUCCESS
- Term left4(-8,3,NULL),right4(-15,3,NULL);
- cout << "left4 = " << left4 << endl;
- cout << "right4 = " << right4 << endl;
- Term pro4 = left4 * right4;
- cout << "pro4 = " << pro4 << endl;
- cout << "----------\n";
- //TEST5 : POLY AUTO
- Polynomial poly;
- poly.addNew(2,3);
- poly.addNew(-4,2);
- poly.addNew(8,1);
- poly.addNew(-6,0);
- cout << "poly = " << poly << endl;
- cout << "----------\n";
- //TEST6 : POLY MANU
- Polynomial poly2;
- cin >> poly2;
- cout << "----------\n";
- cout << "poly2 = " << poly2 << endl;
- //TEST7 : SIMPLIFY MANU
- poly2.simplify();
- cout << "poly2 (simplified) = " << poly2 << endl;
- cout << "----------\n";
- //TEST8 : COPY CONSTRUSTOR
- cout << "Copy Poly2 :\n";
- Polynomial copy(poly2);
- cout << copy << endl;
- cout << "----------\n";
- //TEST9 : ADD POLY
- cout << "Addition :\n";
- Polynomial poly3;
- cin >> poly3;
- poly3.simplify();
- cout << "\n----------\n";
- Polynomial sum = poly2 + poly3;
- cout << "Factor 1 : " << poly2 << endl;
- cout << "Factor 2 : " << poly3 << endl;
- cout << "Result : \n" << sum;
- cout << "\n----------\n";
- //TEST10 : SUBTRACT POLY
- Polynomial dif = poly2 - poly3;
- cout << "Factor 1 : " << poly2 << endl;
- cout << "Factor 2 : " << poly3 << endl;
- cout << "Result : \n" << dif;
- cout << "\n----------\n";
- //TEST11 : EVALUATE POLY
- cout << "poly = " << poly << endl;
- cout << "x = 3" << endl;
- cout << "Result : " << poly.evaluate(3);
- cout << "\n----------\n";
- //TEST12 : MULTIPLICATION POLY
- Polynomial pro = poly2 * poly3;
- cout << "Factor 1 : " << poly2 << endl;
- cout << "Factor 2 : " << poly3 << endl;
- cout << "Result : \n" << pro;
- cout << "\n----------\n";
- return 0;
- }
- Polynomial Polynomial::operator* (const Polynomial& object)
- {
- Term* factor1 = this->top;
- Term* factor2 = object.top;
- Polynomial result;
- while(factor1 != NULL)
- {
- while(factor2 != NULL)
- {
- result.addNew(factor1->getA() * factor2->getA(),factor1->getN()+factor2->getN());
- factor2 = factor2->getLink();
- }
- factor2 = object.top;
- factor1 = factor1->getLink();
- }
- result.simplify();
- return result;
- }
- double Term::evaluate(double theX)
- {
- return (a * pow(theX,static_cast<double>(n)));
- }
- double Polynomial::evaluate(double theX)
- {
- double result = 0;
- Term *temp = top;
- while(temp != NULL)
- {
- result = result + temp->evaluate(theX);
- temp = temp->getLink();
- }
- return result;
- }
- Polynomial::Polynomial(const Polynomial& object)
- {
- Term *temp = object.top;
- top = NULL;
- if(object.isEmpty())
- {
- top = NULL;
- }
- while(temp != NULL)
- {
- addNew(temp->getA(),temp->getN());
- temp = temp->getLink();
- }
- }
- void Polynomial::addNew(int theA,int theN)
- {
- if(isEmpty())
- {
- top = new Term(theA,theN,NULL);
- }
- else
- {
- Term *temp = top;
- while(temp->getLink() != NULL)
- temp = temp->getLink();
- //When reach end !
- temp->setLink(new Term(theA,theN,NULL));
- }
- }
- Polynomial Polynomial::operator+ (const Polynomial& object)
- {
- Term *factor1 = this->top;
- Term *factor2 = object.top;
- Polynomial result;
- bool valAdded = false;
- if(factor2->getN() > factor1->getN())
- {
- Term *temp = factor1;
- factor1 = factor2;
- factor2 = temp;
- }
- Term *lowEx = factor2;
- //This will add all elements from factor1 & factor2 to result,then simplify.
- while(factor1 != NULL)
- {
- result.addNew(factor1->getA(),factor1->getN());
- factor1 = factor1->getLink();
- }//End 1
- while(factor2 != NULL)
- {
- result.addNew(factor2->getA(),factor2->getN());
- factor2 = factor2->getLink();
- }//End 2
- result.simplify();
- return result;
- }
- Polynomial Polynomial::operator- (const Polynomial& object)
- {
- Term *factor1 = this->top;
- Term *factor2 = object.top;
- Polynomial result;
- bool valAdded = false;
- //This will add all elements from factor1 & factor2(negatively) to result,then simplify.
- while(factor1 != NULL)
- {
- result.addNew(factor1->getA(),factor1->getN());
- factor1 = factor1->getLink();
- }//End 1
- while(factor2 != NULL)
- {
- result.addNew(-factor2->getA(),factor2->getN());
- factor2 = factor2->getLink();
- }//End 2
- result.simplify();
- return result;
- }
- ostream& operator << (ostream& out,const Polynomial& object)
- {
- Term *temp = object.top;
- if(temp == NULL)
- out << "0";
- else
- {
- while(temp != NULL)
- {
- out << *temp;
- temp = temp->getLink();
- }
- }
- return out;
- }
- void Polynomial::simplify()
- {
- Term *temp = top;
- int merge = 0;
- while(temp != NULL)
- {
- int expo = temp->getN();
- merge = temp->getA();
- Term * temp2 = temp;
- temp2 = temp2->getLink();
- while(temp2 != NULL)
- {
- if(temp2->getN() == temp->getN())
- {
- merge = merge + temp2->getA();
- temp2->setA(0);
- temp2->setN(1);
- }
- temp2 = temp2->getLink();
- }
- temp->setA(merge);
- temp = temp->getLink();
- merge = 0;
- }
- }
- void Polynomial::remove(Term* afterMe)
- {
- Term* discard = afterMe->getLink();
- afterMe->setLink(discard->getLink());
- delete discard;
- }
- istream& operator >> (istream& in,Polynomial& object)
- {
- cout << "Enter A Polynomial : " << endl;
- char next;
- int sign = 0;
- int theA,theN;
- cin.get(next);
- bool savedSign = false;
- while(next != '\n')
- {
- if(next != ' ')
- {
- if(next == '-')
- {
- if(sign == 0)
- sign = -1;
- else
- sign = -sign;
- cin.get(next);
- if(next != ' ')
- {
- cin.putback(next);
- cin.putback('-');
- savedSign = false;
- }
- else
- {
- cin.get(next);
- if(next != '+' && next != '-')
- cin.putback(next);
- else if(next == '-')
- {
- sign = -sign;
- savedSign = true;
- cin.putback(next);
- }
- else
- cin.putback(next);
- }
- cin.get(next);
- }
- else if(next == '+')
- {
- if(sign == 0)
- sign = 1;
- cin.get(next);
- if(next != ' ')
- {
- cin.putback(next);
- cin.putback('+');
- }
- cin.get(next);
- }
- if((next >= '0') && (next <= '9'))
- {
- cin.putback(next);
- cin >> theA;
- cin.get(next);//Take the word x
- if(next != 'x' && (next == ' ' || next == '\n'))
- {
- theN = 0;
- cin.putback(next);
- }
- else if(next == 'x')
- {
- cin.get(next);//Take '^' or ' '
- if(next == ' ' || next == '\n')
- {
- theN = 1;
- cin.putback(next);
- }
- else if(next = '^')
- {
- cin >> theN;
- }
- }
- if(sign >= 0)
- object.addNew(theA,theN);
- if(sign < 0)
- object.addNew(-theA,theN);
- savedSign = false;
- if(!savedSign)
- sign = 0;
- }
- }
- cin.get(next);
- }
- //CAUTION : THIS FUCTION HAS A BUG IF YOU ADD A MINUS LIKE A + (-B)
- //IT WORKS WELL FOR SUBTRACT A MINUS AND A PLUS.
- return in;
- }
- ostream& operator << (ostream& out,const Term& object)
- {
- if(object.getA() != 0)
- {
- if(object.getA() < 0)
- out << "- " << -object.getA() << "x^" << object.getN() << " ";
- else
- out << "+ " << object.getA() << "x^" << object.getN() << " ";
- }
- return out;
- }
- istream& operator >> (istream& in,Term& object)
- {
- int theA,theN;
- char theX;
- cin >> theA >> theX >> theN;
- if(theX != 'x' && theX != 'X')
- {
- cout << "Attempt to input wrong ! Exitting..." << endl;
- exit(1);
- }
- return in;
- }
- bool Polynomial::isEmpty() const
- {
- return(top == NULL);
- }
- Term Term::operator + (const Term& object)
- {
- cout << "Term::operator + : ";
- if(getN() != object.getN())
- {
- cout << "Error ! Adding 2 term with different exponent !" << endl;
- return object;
- }
- else
- {
- int sum = getA() + object.getA();
- return Term(sum,getN());
- }
- }
- Term Term::operator - (const Term& object)
- {
- cout << "Term::operator - : ";
- if(getN() != object.getN())
- {
- cout << "Error ! Subtracting 2 term with different exponent !" << endl;
- return object;
- }
- else
- {
- int dif = getA() - object.getA();
- return Term(dif,getN());
- }
- }
- Term Term::operator * (const Term& object)
- {
- cout << "Term::operator * : ";
- return Term(getA() * object.getA(),getN() + object.getN());
- }
Add Comment
Please, Sign In to add comment