Sei sulla pagina 1di 2

DataStructuresAndAlgorithms

HomeWork04

DATA STRUCTURES AND ALGORITHMS


Home Work 04 Marks 100 Instructions Workonthishomeworkindividually.AbsolutelyNOcollaborationisallowed.Anytracesofplagiarismwouldresult inaZEROmarksinthishomeworkandpossibledisciplinaryaction. DueDate Pastethesolution(s)folderoftheproblems(sourcecode.cppfilesonly)labeledwithyourcompleterollnumberin DSAMHW04andDSAAHW04foldersformorningandafternoonsectionsrespectivelyonThursday,May19, 2011before04:00PM.Thesefoldersareavailableat\\printsrv\TeacherData\UmairBabar\Students Whatyouhavetodo Design, implement and test an ADT, Polynomial, that provides some of the basic operations for univariate polynomials.Forexample,P(x)=3x47x+18issuchapolynomial. YourADTPolynomialshouldhaveadatamemberbelongingtotheclassCircularListwhichkeepstheterms(term consistsofcoefficientandexponent)inthepolynomialinacircularlinkedlist.Thecircularlistrepresentationofa polynomial has one node for each term that has nonzero coefficient. The terms are in decreasing order of exponentandtheheadnodehasitscoefficientandexponentfieldequalto0and1respectively.Thefollowing figuregivessomeexamples.

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.

friend Polynomial& operator-(const Polynomial& p1, const Polynomial& p2); Subtractthefirstpolynomialp1fromthesecondpolynomialp2andreturntheresult.

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.

Potrebbero piacerti anche