Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
HomeWork04
You must use the three classes Node, ListIterator, and List (should be modified to adapt the change to circular structure)coveredinclasstodothejob: You can modify and/or simplify the specific details of these three classes, but you must adhere to the principlesofhidingNodefromthepublicandaccessingvialistiteratorsinsteadofexplicitnodepointers. Inparticular,NodemustbehiddenfromyourPolynomialclass. Notethatitisagoodprogrammingstyletodestroynodeswhentheyarenolongerneeded.
P a g e |1
UmairBabar,PUCIT.Lahore.
DataStructuresAndAlgorithms
HomeWork04
TheADTPolynomialshouldsupportthefollowingoperations.Notethatsomeoperatorsshouldbeoverloadedto make your code more readable. You may add any other public/private member functions that you think are necessary. a. Polynomial() Createthezeropolynomial,thatisP(x)=0.Polynomial()istheclassdefaultconstructor. b. friend istream& operator>>(istream&, const Polynomial&); Readinapolynomialfromcin.Eachpolynomialhasthefollowingform: c1 e1 c2 e2 cm em 0 1 whereciandeiareintegersdenotingthecoefficientandexponentoftheithterm,respectively.Thelastpair0 1denotestheendofpolynomial. Youcanassumethattheexponentsareindecreasingorder;thatise1>e2>...>em0,andthereisnozero coefficientintheinput;thatisci0foralli. c. friend ostream& operator<<(ostream&, const Polynomial&); Output the polynomial to cout. The output format should be the same as the input format. That is, the exponentsshouldbeindecreasingorderandallcoefficientsarenonzero.Alsoitshouldendwiththepair0 1. d. friend Polynomial& operator+(const Polynomial& p1, const Polynomial& p2); Addthetwopolynomialsp1andp2andreturntheresult.
e.
f. friend Polynomial& operator*(const Polynomial& p1, const Polynomial& p2); Multiplythetwopolynomialsp1andp2andreturntheresult. Inputandoutput.Youneedtowriteamainprogramwhichobtainsaninputlinefromusers.Theendofinputis signaled by the CtrlZ character (EOF character in VC++) which your program should detect. Each input line is a sequenceofintegersthatareseparatedbyblanksandhaveoneofthefollowingthreepossibleformats: 1<polynomial><polynomial> 2<polynomial><polynomial> 3<polynomial><polynomial> where<polynomial>representsonepolynomial.1meansthatthetwopolynomialsaretobeadded;2meansthat thesecondpolynomialshouldbesubtractedfromthefirstpolynomial;and3meansthatthetwopolynomialsare tobemultiplied. For each input line, your program should output to cout one separate line containing the result using the <polynomial>format. Note that you should make use of the overloaded input and output operator functions to get polynomials and output the result respectively. Similarity, you should use the overloaded arithmetic operators to calculate the results. NOTE:NosubmissionwillbeacceptedaftertheDUETIMEandDATE. B E S T O F L U C K
P a g e |2
UmairBabar,PUCIT.Lahore.