版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計(jì)(10)王麗蘋lipingwang@4/21/20231.應(yīng)用:多項(xiàng)式求解后序波蘭式的求解abc-d*(1)如果a,b,c為整數(shù)如何求解。(2)如果a,b,c為多項(xiàng)式如何求解。AH=7x14+2x8+-10x6+1
BH=4x18+8x14-3x10+10x6-x44/21/20232.多項(xiàng)式及其相加在多項(xiàng)式的鏈表表示中每個(gè)結(jié)點(diǎn)增加了一個(gè)數(shù)據(jù)成員link,作為鏈接指針。優(yōu)點(diǎn)是:多項(xiàng)式的項(xiàng)數(shù)可以動(dòng)態(tài)地增長(zhǎng),不存在存儲(chǔ)溢出問(wèn)題。插入、刪除方便,不移動(dòng)元素。鏈接隊(duì)列和棧的綜合應(yīng)用4/21/20233.多項(xiàng)式鏈表的相加AH=7x14+2x8+-10x6+1
BH=4x18+8x14-3x10+10x6-x471428-106110^418814-310106-14^4181514-31028-14110^AH.firstBH.firstCH.first4/21/20234.鏈接隊(duì)列和棧的綜合應(yīng)用目錄Polynomial下例程4/21/20235.鏈接隊(duì)列和棧的綜合應(yīng)用top_nodePolynomialnextPolynomialnextclassStack{protected:Node*top_node;};structNode{Polynomialentry;Node*next;};4/21/20236.鏈接隊(duì)列和棧的綜合應(yīng)用PolynomialTermnextTermnextclassQueue{protected:SmallNode*front,*rear;};classSmallNode{public:Termentry;SmallNode*next;};Termnextfrontrear4/21/20237.鏈接隊(duì)列和棧的綜合應(yīng)用classTerm{public:intdegree;doublecoefficient;};degreecoefficientEg:3x^2degree=2coefficient=34/21/20238.鏈接隊(duì)列和棧的綜合應(yīng)用--TermclassTerm{public:Term(intexponent=0,doublescalar=0);intdegree;doublecoefficient;};4/21/20239.鏈接隊(duì)列和棧的綜合應(yīng)用--Term#include"Term.h"Term::Term(intexponent,doublescalar)/*Post:TheTermisinitializedwiththegivencoefficientandexponent,orwithdefaultparametervaluesof0.*/{degree=exponent;coefficient=scalar;}4/21/202310.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialtypedefTermQueue_entry;typedefTermSmallNode_entry;classSmallNode{//datamemberspublic: SmallNode_entryentry; SmallNode*next;//constructors SmallNode(); SmallNode(SmallNode_entryitem,SmallNode*add_on=0);};4/21/202311.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialclassQueue{public://standardQueuemethods Queue(); boolempty()const; Error_codeappend(constQueue_entry&item); Error_codeserve(); Error_coderetrieve(Queue_entry&item)const;//safetyfeaturesforlinkedstructures ~Queue();protected: SmallNode*front,*rear;};//Queue表示一個(gè)多項(xiàng)式4/21/202312.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialclassExtended_queue:publicQueue{public:intsize()const;voidclear();Error_codeserve_and_retrieve(Queue_entry&item);};4/21/202313.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialclassPolynomial:privateExtended_queue{//Useprivateinheritance.public:Polynomial();Polynomial::Polynomial(constPolynomial&original);voidoperator=(constPolynomial&original);voidread();voidprint()const;voidequals_sum(Polynomialp,Polynomialq);intdegree()const;};//用Polynomial來(lái)封裝Queue,Polynomial表示一個(gè)多項(xiàng)式4/21/202314.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialPolynomial::Polynomial(){front=NULL;rear=NULL;}4/21/202315.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialPolynomial::Polynomial(constPolynomial&original){SmallNode*new_front,*new_copy,*original_front=original.front,*new_rear;if(original_front==NULL){ new_front=NULL; new_rear=NULL;}else{//Duplicatethelinkednodesnew_copy=new_front=newSmallNode(original_front->entry);while(original_front->next!=0){original_front=original_front->next;new_copy->next=newSmallNode(original_front->entry);new_copy=new_copy->next;}new_rear=new_copy;}front=new_front;rear=new_rear;}4/21/202316.LinkedQueues--Queue
original_frontnew_frontnew_copynew_frontnew_copy4/21/202317.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialvoidPolynomial::operator=(constPolynomial&original){SmallNode*new_front,*new_copy,*original_front=original.front,*new_rear;if(original_front==NULL){ new_front=NULL; new_rear=NULL;}else{//Duplicatethelinkednodesnew_copy=new_front=newSmallNode(original_front->entry);while(original_front->next!=0) {original_front=original_front->next;new_copy->next=newSmallNode(original_front->entry);new_copy=new_copy->next;} new_rear=new_copy;}while(!empty())//CleanoutoldQueueentriesserve();front=new_front;rear=new_rear;}4/21/202318.鏈接隊(duì)列和棧的綜合應(yīng)用—Polynomial
p147voidPolynomial::print()const/*Post:ThePolynomialisprintedtocout.*/{SmallNode*print_node=front;boolfirst_term=true;while(print_node!=NULL){Term&print_term=print_node->entry;if(first_term){//Inthiscase,suppressprintinganinitial'+'.first_term=false;if(print_term.coefficient<0)cout<<"-";}elseif(print_term.coefficient<0)cout<<"-";elsecout<<"+";doubler=(print_term.coefficient>=0)?print_term.coefficient:-(print_term.coefficient);if(r!=1)cout<<r;if(print_term.degree>1)cout<<"X^"<<print_term.degree;if(print_term.degree==1)cout<<"X";if(r==1&&print_term.degree==0)cout<<"1";print_node=print_node->next;}if(first_term)cout<<"0";//Print0foranemptyPolynomial.cout<<endl;}4/21/202319.LinkedQueues--Queue
print_node4/21/202320.鏈接隊(duì)列和棧的綜合應(yīng)用—Polynomial
p148voidPolynomial::read(){clear();doublecoefficient;intlast_exponent,exponent;boolfirst_term=true;cout<<"Enterthecoefficientsandexponentsforthepolynomial,onepairperline."<<endl<<"Exponentsmustbeindescendingorder."<<endl<<"Enteracoefficientof0oranexponentof0toterminate."<<endl;do{cout<<"coefficient?"<<flush;cin>>coefficient;if(coefficient!=0.0){cout<<"exponent?"<<flush;cin>>exponent;if((!first_term&&exponent>=last_exponent)||exponent<0){exponent=0;cout<<"Badexponent:Polynomialterminateswithoutitslastterm."<<endl;}else{Termnew_term(exponent,coefficient);append(new_term);first_term=false;}last_exponent=exponent;}}while(coefficient!=0.0&&exponent!=0);}4/21/202321.鏈接隊(duì)列和棧的綜合應(yīng)用--PolynomialintPolynomial::degree()const/*Post:IfthePolynomialisidentically0,aresultof-1isreturned.OtherwisethedegreeofthePolynomialisreturned.*/{if(empty())return-1;Termlead;retrieve(lead);returnlead.degree;}4/21/202322.鏈接隊(duì)列和棧的綜合應(yīng)用—Polynomial
p149voidPolynomial::equals_sum(Polynomialp,Polynomialq){clear();while(!p.empty()||!q.empty()){Termp_term,q_term;if(p.degree()>q.degree()){p.serve_and_retrieve(p_term);append(p_term);}elseif(q.degree()>p.degree()){q.serve_and_retrieve(q_term);append(q_term);}else{p.serve_and_retrieve(p_term);q.serve_and_retrieve(q_term);if(p_term.coefficient+q_term.coefficient!=0){Termanswer_term(p_term.degree,p_term.coefficient+q_term.coefficient);append(answer_term);}}}}4/21/202323.鏈接隊(duì)列和棧的綜合應(yīng)用—LinkStacktypedefPolynomialNode_entry;structNode{//datamembersNode_entryentry;Node*next;//constructorsNode();Node(constNode_entryitem,Node*add_on=0);};4/21/202324.鏈接隊(duì)列和棧的綜合應(yīng)用—LinkStackclassStack{public://StandardStackmethodsStack();boolempty()const;Error_codepush(constNode_entry&item);Error_codepop();Error_codetop(Node_entry&item)const;//Safetyfeaturesforlinkedstructures~Stack();protected:Node*top_node;};4/21/202325.鏈接隊(duì)列和棧的綜合應(yīng)用—Mainvoidmain()/*Post:Theprogramhasexecutedsimplepolynomialarithmeticcommandsenteredbytheuser.Uses:TheclassesStackandPolynomialandthefunctionsintroduction,instructions,do_command,andget_command.*/{Stackstored_polynomials;voidintroduction();voidinstructions();booldo_command(charcommand,Stack&stored_polynomials);charget_command();chartolower(charcommand);introduction();instructions();while(do_command(get_command(),stored_polynomials));}4/21/202326.鏈接隊(duì)列和棧的綜合應(yīng)用—Mainvoidintroduction(){cout<<"Thisisapolynomialsprogram."<<endl;}voidinstructions(){cout<<"Pleaseenteravalidcommand:"<<endl <<"[?]ReadaPolynomial"<<endl <<"[=]ReturnTopPolynomial"<<endl<<"[+]SumtwoPolynomial"<<endl<<"[q]Quit."<<endl;}4/21/202327.鏈接隊(duì)列和棧的綜合應(yīng)用—Main
p143booldo_command(charcommand,Stack&stored_polynomials)/*Pre:Thefirstparameterspecifiesavalidcalculatorcommand.Post:ThecommandspecifiedbythefirstparameterhasbeenappliedtotheStackofPolynomialobjectsgivenbythesecondparameter.Aresultoftrueisreturnedunlesscommand=='q'.Uses:TheclassesStackandPolynomial.*/{Polynomialp,q,r;
switch(command){
4/21/202328.鏈接隊(duì)列和棧的綜合應(yīng)用—Main
case'?':p.read();if(stored_polynomials.push(p)==overflow)cout<<"Warning:Stackfull,lostpolynomial"<<endl;break;case'=':if(stored_polynomials.empty())cout<<"Stackempty"<<endl;else{stored_polynomials.top(p);p.print();}break;4/21/202329.鏈接隊(duì)列和棧的綜合應(yīng)用—Main
case'+':if(stored_polynomials.empty())cout<<"Stackempty"<<endl;else{stored_polynomials.top(p);stored_polynomials.pop();if(stored_polynomials.empty()){cout<<"Stackhasjustonepolynomial"<<endl;stor
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 學(xué)習(xí)行為數(shù)據(jù)分析-洞察分析
- 醫(yī)學(xué)影像三維重建技術(shù)-洞察分析
- 音樂(lè)人才市場(chǎng)需求與培養(yǎng)模式研究-洞察分析
- 藥理作用機(jī)制分析-洞察分析
- 遙感與GIS集成研究-洞察分析
- 云計(jì)算下的智能交通信號(hào)燈匹配算法設(shè)計(jì)-洞察分析
- 鐵路客運(yùn)產(chǎn)業(yè)融合發(fā)展-洞察分析
- 《市場(chǎng)預(yù)測(cè)與對(duì)策》課件
- 2024年格爾木市人民醫(yī)院高層次衛(wèi)技人才招聘筆試歷年參考題庫(kù)頻考點(diǎn)附帶答案
- 2024年楊浦區(qū)老年醫(yī)院高層次衛(wèi)技人才招聘筆試歷年參考題庫(kù)頻考點(diǎn)附帶答案
- Unit 5 Dinner's ready Read and write(說(shuō)課稿)-2024-2025學(xué)年人教PEP版英語(yǔ)四年級(jí)上冊(cè)
- 第3章智能網(wǎng)聯(lián)汽車高精度地圖與定位技術(shù)
- 2018年國(guó)家公務(wù)員行測(cè)考試真題-省級(jí)(含答案)
- 2024中華人民共和國(guó)學(xué)前教育法學(xué)習(xí)解讀課件
- 計(jì)量經(jīng)濟(jì)學(xué)復(fù)習(xí)資料-概念和問(wèn)答
- 2024年廣東省公務(wù)員錄用考試《行測(cè)》真題及答案解析
- 2024年秋新人教PEP版3年級(jí)上冊(cè)英語(yǔ)教學(xué)課件 Unit 4 第4課時(shí) Part B Let's talk
- 2024新版(外研版三起孫有中)三年級(jí)英語(yǔ)上冊(cè)單詞帶音標(biāo)
- 期末試卷(試題)-2024-2025學(xué)年三年級(jí)上冊(cè)數(shù)學(xué)蘇教版
- 2023年員工手冊(cè)范本(適用于公司全體員工手冊(cè))
- 2025屆安徽省合肥市一六八中高二數(shù)學(xué)第一學(xué)期期末經(jīng)典試題含解析
評(píng)論
0/150
提交評(píng)論