版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
多項式四則運算//多項式加減乘除-02.cpp:定義控制臺應用程序的入口點。//#include"stdafx.h"int_tmain(intargc,_TCHAR*argv[]){n}#include<stdio.h>#include<malloc.h>typedefstructPolynomial{floatcoef;nstructPolynomial*next;}*Polyn,Polynomial;voidInsert(Polynp,Polynh){if(p->coef==0)free(p);{lynqqqhq2=h->next;while(q2&&p->expn<q2->expn){q1=q2;q2=q2->next;}if(q2&&p->expn==q2->expn){q2->coef+=p->coef;free(p);if(!q2->coef){q1->next=q2->next;free(q2);}}{pnextq;q1->next=p;}}}PolynCreatePolyn(Polynhead,intm){p=head=(Polyn)malloc(sizeof(structPolynomial));head->next=NULL;for(i=0;i<m;i++){p=(Polyn)malloc(sizeof(structPolynomial));printf("請輸入第%d項的系數與指數:",i+1);scanf_s("%f%d",&p->coef,&p->expn);Insert(p,head);}returnhead;}voidDestroyPolyn(Polynp){lynqqq1=p->next;q2=q1->next;while(q1->next){free(q1);q1=q2;q2=q2->next;}}voidPrintPolyn(PolynP){Pnextintflag1;{putchar0');printfn");}while(q){if(q->coef>0&&flag!=1)putchar+');if(q->coef!=1&&q->coef!=-1){printf("%g",q->coef);if(q->expn==1)putchar('X');ifq->expn)printf("X^%d",q->expn);}{if(q->coef==1){if(!q->expn)putchar1');elseif(q->expn==1)putcharX');elseprintf("X^%d",q->expn);}if(q->coef==-1){if(!q->expn)printf("-1");elseif(q->expn==1)printfX");elseprintf("-X^%d",q->expn);}}q=q->next;flag++;}printfn");}intcompare(Polyna,Polynb){ifa&b){returnreturn-1;n}elseif(!a&&b)return-1;return}PolynAddPolyn(Polynpa,Polynpb){Polynqa=pa->next;Polynqb=pb->next;Polynheadc,hc,qc;hc=(Polyn)malloc(sizeof(structPolynomial));hc->next=NULL;adchc{qc=(Polyn)malloc(sizeof(structPolynomial));switch(compare(qa,qb)){case1:{qc->coef=qa->coef;qc->expn=qa->expn;qa=qa->next;}case0:{qc->coef=qa->coef+qb->coef;qc->expn=qa->expn;qa=qa->next;qb=qb->next;}case-1:{qc->coef=qb->coef;qc->expn=qb->expn;qb=qb->next;}}if(qc->coef!=0){qc->next=hc->next;hc>next=qc;}elsefree(qc);}returnheadc;}PolynSubtractPolyn(Polynpa,Polynpb){ynhpbPolynp=pb->next;while(p){p->coef*=-1;ppnext}pd=AddPolyn(pa,h);for(p=h->next;p;p=p->next)p->coef*=-1;rnpd}floatValuePolyn(Polynhead,floatx){floatsum=0,t;for(p=head->next;p;p=p->next){t=1;for(i=p->expn;i!=0;){{t/=x;}{t*=x;}}sum+=p->coef*t;}nsum}PolynMultiplyPolyn(Polynpa,Polynpb){Polynqa=pa->next;Polynqb=pb->next;hf=(Polyn)malloc(sizeof(structPolynomial));extNULLfor(;qa;qa=qa->next){for(qb=pb->next;qb;qb=qb->next){pf=(Polyn)malloc(sizeof(structPolynomial));pf->coef=qa->coef*qb->coef;pf->expn=qa->expn+qb->expn;}}rnhf}#include<stdlib.h>#include<stdio.h>#include<malloc.h>structlist{tzhishuhustructlist*next;typedefstructlistnode;typedefnode*link;voidfree_list(linkhead){kpointerwhile(head!=NULL){pointerheadhead=head->next;free(pointer);}}linkrev(linkhead){linkp1,p2;p1=head->next;p2=p1->next;p1->next=NULL;while(p2!=NULL){head->next=p2;pp2->next;head->next->next=p1;p1=head->next;}returnhead}linkfind_list(linkhead,intkey){pp0=head->next;while(p0!=NULL){if(p0->zhishu==key)turnpppnext}turnp}linkfind2_list(linkhead,intkey){padwhile(p0!=NULL){if((p0->next->zhishu==key)&&(p0->next->xishu!=0))turnpppnext}turnp}linkclearzero(linkhead){linkppp;adwhile(p0!=NULL){if((p0->next->xishu==0)&&(p0->next->zhishu!=0)){ppp0->next;p0->next=pp->next;free(pp);}pp0->next;}returnhead}intmax_list(linkhead1,linkhead2){pinttemp=-10000;p1=head1->next;p2=head2->next;while(p1!=NULL){if(p1->zhishu>temp)temp=p1->zhishu;p1=p1->next;while(p2!=NULL){if(p2->zhishu>temp)temp=p2->zhishu;p2=p2->next;temp}linkadd_list(linkhead1,linkhead2){pointerpointerpointernewnewinkheadhead3=(link)malloc(sizeof(node));if(head3==NULL){printf("MemoryallocateFailure!\n");}{head3->next=NULL;head3->zhishu=0;head3->xishu=0;pointer1=head1->next;pointer2=head2->next;pointer3=head3;if((pointer1->zhishu)>=(pointer2->zhishu)){i=pointer2->zhishu;}{i=pointer1->zhishu;while(1){if(i>max_list(head1,head2))if(find_list(head1,i)!=NULL){new2=find_list(head1,i);a=a+new2->xishu;if(find_list(head2,i)!=NULL){new2=find_list(head2,i);a=a+new2->xishu;{new1=(link)malloc(sizeof(node));new1->zhishu=i;new1->xishu=a;new1->next=NULL;pointer3->next=new1;pointernewreturnhead3;}linkinit_list(linkhead){head=(link)malloc(sizeof(node));if(head==NULL)printf("MemoryallocateFailure!\n");{head->next=NULL;head->zhishu=0;head->xishu=0;}urnhead}linkjianfa(linkhead11,linkhead22){pp2=head22->next;while(p2!=NULL){p2->xishu=(-1)*(p2->xishu);p2=p2->next;}returnadd_list(head11,head22);}linkchufa(linkhead1,linkhead2){linkhead3=NULL,tp,new1,p3,p2,new2;linktemp=NULL;head3=(link)malloc(sizeof(node));head3->next=NULL;head3->zhishu=0;head3->xishu=0;temp=(link)malloc(sizeof(node));temp->next=NULL;temp->zhishu=0;temp->xishu=10;init_list(head3);init_list(temp);phead3;while(1){if((head1->next->zhishu)<(head2->next->zhishu)){p3->next=head1;new1=(link)malloc(sizeof(node));new1->zhishu=(head1->next->zhishu)-(head2->next->zhishu);new1->xishu=(head1->next->xishu)/(head2->next->xishu);new1->next=NULL;p3->next=new1;p3=new1;tp=temp;p2=head2->next;while(p2!=NULL){if((p2->xishu==0)&&(p2->zhishu==0)){new2=(link)malloc(sizeof(node));new2->zhishu=p2->zhishu+p3->zhishu;new2->xishu=(p2->xishu)*(p3->xishu);new2->next=NULL;tp->next=new2;tp=new2;p2=p2->next;head1=jianfa(rev(head1),rev(temp));head1=rev(head1);free_list(temp->next);temp->next=NULL;returnhead3;}linkchengfa(linkhead1,linkhead2){linkp1,p2,p3,new1,p,tp,p31;linkhead3=NULL;head3=(link)malloc(sizeof(node));if(head3==NULL){printf("MemoryallocateFailure!\n");}{head3->next=NULL;head3->zhishu=0;head3->xishu=0;p1=head1->next;p2=head2->next;phead3;while(p1!=NULL){p2=head2->next;while(p2!=NULL){new1=(link)malloc(sizeof(node));new1->zhishu=p1->zhishu+p2->zhishu;new1->xishu=(p1->xishu)*(p2->xishu);new1->next=NULL;p3->next=new1;ewp2=p2->next;}p1=p1->next;}p3=head3->next;while(p3!=NULL){pwhile(p31!=NULL){if(find2_list(p31,p->zhishu)!=NULL){new1=find2_list(p31,p->zhishu);p->xishu=p->xishu+new1->next->xishu;tp=new1->next;new1->next=tp->next;free(tp);p31=p31->next;p3=p3->next;returnclearzero(head3);}voidprint_list(linkhead){kpointerpointer=head->next;printf("");while(pointer!=NULL){printf("%dx^%d",pointer->xishu,pointer->zhishu);if(pointer->next!=NULL)printf("+");pointerpointernext;}printfn");}/*****************************************************//*voidprint_list(linkhead){linkpointer;pointer=head->next;printf("(zhishu,xishu)\n");while(pointer!=NULL){printf("%d,%d\n",pointer->zhishu,pointer->xishu);pointerpointernext;}printf("\n");}/*****************************************************/linkcreate_list(linkhead){inknewkpointerhead=(link)malloc(sizeof(node));if(head==NULL)printf("MemoryallocateFailure!\n");{head->next=NULL;head->zhishu=0;head->xishu=0;pointerheadwhile(1){new1=(link)malloc(sizeof(node));printf("輸入指數和系數:");scanf_s("%d%d",&(new1->zhishu),&(new1->xishu));if(new1->xishu==0){new1->next=NULL;pointer->next=new1;pointer=new1;}/*while*/}/*else*/returnhead;}void
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年四川電影電視學院單招職業(yè)技能考試備考試題含詳細答案解析
- 2026年山西華澳商貿職業(yè)學院單招綜合素質考試備考試題含詳細答案解析
- 2026年廣東建設職業(yè)技術學院單招綜合素質筆試參考題庫含詳細答案解析
- 2026四川自貢市沿灘區(qū)九洪鄉(xiāng)衛(wèi)生院第一批面向社會招聘4人考試重點題庫及答案解析
- 2026年錫林郭勒職業(yè)學院高職單招職業(yè)適應性測試備考題庫及答案詳細解析
- 2026年常州機電職業(yè)技術學院單招綜合素質考試模擬試題含詳細答案解析
- 2026年寧夏葡萄酒與防沙治沙職業(yè)技術學院單招綜合素質考試備考試題含詳細答案解析
- 2026福建泉州黎大國有資產經營有限公司職員招聘1人考試重點題庫及答案解析
- 2026年上海戲劇學院單招職業(yè)技能考試備考試題含詳細答案解析
- 2026年無錫城市職業(yè)技術學院單招綜合素質筆試備考題庫含詳細答案解析
- 文旅智慧景區(qū)項目分析方案
- 心血管介入手術臨床操作規(guī)范
- 合同主體變更說明函范文4篇
- T-ZZB 2440-2021 通信電纜用鋁塑復合箔
- 鞘膜積液的護理
- 高中英語(人教版)必修二詞匯表默寫
- 2025年急性創(chuàng)傷的救治流程與規(guī)范
- 廣東省交通建設工程從業(yè)人員實名制管理系統(tǒng)
- 代簽手術免責協(xié)議書范本
- 新安全生產法2025全文
- 人教版六年級數學下冊課件第二單元人教新課標版
評論
0/150
提交評論