一元多项式计算器代码.doc
一元多项式计算器代码INCLUDEINCLUDEINCLUDETYPEDEFSTRUCTPOLYNODE{FLOATCOEF//系数INTEXP//指数STRUCTPOLYNODENEXT}POLY,POLYNODE//POLY为结点指针类型VOIDINSERTPOLYP,POLYHEAD{IFPCOEF0//系数为0时释放结点FREEPELSE{POLYQ1,Q2Q1HEADQ2HEADNEXTWHILEQ2Q2Q2NEXT}IFQ2FREEPIFQ2COEF//系数为0时释放结点{Q1NEXTQ2NEXTFREEQ2}}ELSE{PNEXTQ2Q1NEXTP}}}//INSERTPOLYCREATELISTPOLYHEAD,INTM{//建立一个头指针为HEAD、项数为M的一元多项式INTIPOLYPPOLYNODEQPHEADPOLYMALLOCSIZEOFSTRUCTPOLYNODEHEADNEXTNULLFORI0IPCOEFPEXPINSERTP,HEAD//调用INSERT函数插入结点}QHEADNEXTWHILEQNULL{COUTCOEFEXPNEXT}RETURNHEAD}//CREATEPOLYVOIDDESTROYLISTPOLYP{//销毁多项式PPOLYQ1,Q2IFPNEXTNULL{Q1PNEXTQ2Q1NEXTWHILEQ1NEXT{FREEQ1Q1Q2//指针后移Q2Q2NEXT}}}INTOUTPUTLISTPOLYP{//输出多项式POLYQPNEXTINTFLAG1//项数计数器IFQ{//若多项式为空,输出0COUTCOEF0IFQEXP1COUTEXPCOUTEXP}ELSE{IFQCOEF1{IFQEXPCOUTEXP1COUTEXPCOUTEXP}IFQCOEF1{IFQEXPCOUTEXP1COUTEXP}}QQNEXTFLAG}//WHILECOUTEXPBEXPRETURN1ELSEIFA||AEXPEXPRETURN1ELSERETURN0}ELSEIFA//A多项式已空,但B多项式非空ELSERETURN1//B多项式已空,但A多项式非空}//COMPAREPOLYADDPOLYPOLYPA,POLYPB{//求解并建立和多项式AB,返回其头指针POLYQAPANEXTPOLYQBPBNEXTPOLYHEADC,HC,QCHCPOLYMALLOCSIZEOFSTRUCTPOLYNODE//建立头结点HCNEXTNULLHEADCHCWHILEQA||QB{QCPOLYMALLOCSIZEOFSTRUCTPOLYNODESWITCHCOMPAREQA,QB{CASE1{QCCOEFQACOEFQCEXPQAEXPQAQANEXTBREAK}CASE0{QCCOEFQACOEFQBCOEFQCEXPQAEXPQAQANEXTQBQBNEXTBREAK}CASE1{QCCOEFQBCOEFQCEXPQBEXPQBQBNEXTBREAK}}//SWITCHIFQCCOEF0{QCNEXTHCNEXTHCNEXTQCHCQC}ELSEFREEQC//当相加系数为0时,释放该结点}//WHILERETURNHEADC}//ADDPOLYPOLYSUBTRACTPOLYPOLYPA,POLYPB{//求解并建立和多项式AB,返回其头指针POLYQAPANEXTPOLYQBPBNEXTPOLYHEADC,HC,QCHCPOLYMALLOCSIZEOFSTRUCTPOLYNODE//建立头结点HCNEXTNULLHEADCHCWHILEQA||QB{QCPOLYMALLOCSIZEOFSTRUCTPOLYNODESWITCHCOMPAREQA,QB{CASE1{QCCOEFQACOEFQCEXPQAEXPQAQANEXTBREAK}CASE0{QCCOEFQACOEFQBCOEFQCEXPQAEXPQAQANEXTQBQBNEXTBREAK}CASE1{QCCOEFQBCOEFQCEXPQBEXPQBQBNEXTBREAK}}//SWITCHIFQCCOEF0{QCNEXTHCNEXTHCNEXTQCHCQC}ELSEFREEQC//当相减系数为0时,释放该结点}//WHILERETURNHEADC}//ADDPOLYPOLYMULTIPLYPOLYPOLYPA,POLYPB{//求解并建立积多项式AB,返回其头指针POLYHF,PFPOLYQAPANEXTPOLYQBPBNEXTHFPOLYMALLOCSIZEOFSTRUCTPOLYNODE//建立头结点HFNEXTNULLFORQAPANEXTQAQAQANEXT{FORQBPBNEXTQBQBQBNEXT{PFPOLYMALLOCSIZEOFSTRUCTPOLYNODEPFCOEFQACOEFQBCOEFPFEXPQAEXPQBEXPINSERTPF,HF//调用INSERT函数以合并指数相同的项}}RETURNHF}//MULTIPLYPOLYVOIDDEVICEPOLYPOLYPA,POLYPB{//求解并建立商多项式A/B,返回其头指针POLYHF,PF,TEMP1,TEMP2POLYQAPANEXTPOLYQBPBNEXTHFPOLYMALLOCSIZEOFSTRUCTPOLYNODE//建立头结点,存储商HFNEXTNULLPFPOLYMALLOCSIZEOFSTRUCTPOLYNODE//建立头结点,存储余数PFNEXTNULLTEMP1POLYMALLOCSIZEOFSTRUCTPOLYNODETEMP1NEXTNULLTEMP2POLYMALLOCSIZEOFSTRUCTPOLYNODETEMP2NEXTNULLTEMP1ADDPOLYTEMP1,PAWHILEQANULLTEMP2NEXTCOEFQACOEF/QBCOEFTEMP2NEXTEXPQAEXPQBEXPINSERTTEMP2NEXT,HFPASUBTRACTPOLYPA,MULTIPLYPOLYPB,TEMP2QAPANEXTTEMP2NEXTNULL}PFSUBTRACTPOLYTEMP1,MULTIPLYPOLYHF,PBCOUTNEXTPPPNEXT{T1FORIPEXPI0{IFICOEFT}RETURNSUM}//VALUEPOLYPOLYDERIVATIVEPOLYHEAD{//求解并建立A的导函数多项式,并返回其头指针POLYQHEADNEXT,P1,P2,HDHDP1POLYMALLOCSIZEOFSTRUCTPOLYNODE//建立头结点HDNEXTNULLWHILEQ{IFQEXP0{//该项不是常数项时P2POLYMALLOCSIZEOFSTRUCTPOLYNODEP2COEFQCOEFQEXPP2EXPQEXP1P2NEXTP1NEXT//尾插法插入结点P1NEXTP2P1P2}ELSEIFQEXP0//该项为常数项BREAKQQNEXT}RETURNHD}//DERVATIVEINTMAIN{INTM,N,FLAG0FLOATXPOLYPA0,PB0,PC,PD,PE,PF//定义各