您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 一元多项式的各种运算实现(c++,c)
一元多项式各种运算本程序可完成一元多项式的加,减,乘,积分,微分运算输入可按习惯书写形式输入各项,输出按指数降序//头文件:#includewindows.h#includemmsystem.h#includetime.h#includeiostream.h#includestring.h#includeiostream.h#includeiomanip.h#includectype.h#includemalloc.h#includelimits.h#includestdio.h#includestdlib.h#includeio.h#includemath.h#includesys/timeb.h#includestdarg.h#defineTRUE1#defineFALSE0#defineOK1#defineERROR0typedefintstatus;typedeffloattype;typedefintfuhaosign;结构体类型定义:typedefstructlist{typedata;intzhi;structlist*next;}list;typedefstructnode{intlength;list*next;}node;用到的函数:注:各函数参数如是结构体指针,则此指针传入函数之前必将其赋值为NULL否则可能出现错误。voidshuoming(){cout*******************************************************************************endl;cout*各项可以用平时书写习惯输入,若有未知数,必须用x表示,且各项之间必须用空格分隔,支*endl;cout*持:系数是小数,指数是负数的情况,但指数不能是小数,功能选择项:*endl;cout*1:加法;2:减法;3:乘法;4:积分;5:微分;6:求值;7:原样输出多项式*endl;cout*******************************************************************************endl;cout举例:3x39.3x-3-x.....endl;}//求未知数指数是-k时的值floatfumi(intk){floata=1.0;for(inti=1;i=k;i++)a=a*1e-1;returna;}//赋值,参数是指向头结点的指针voidfuzhi(node*&toup){floatfumi(intk);voidinit(node*&toup);if(!toup)init(toup);if(toup-next)init(toup);//保证赋值前链表有且仅有一个头结点//初始化各变量charc,ch='';//c接受字符,ch记录上一字符,用于检错intk2=0;//k2标志着+-号前面是否有数值,0时没有,1为有fuhaosignk1=1;//k1是符号位正负的标志,0为负,1为正intk3=0;//小数点的个数intkx=0;//标志前面是否有xlist*p=toup-next,*t;typedat=0.0;//输入c=getchar();inti=0;while(c!='\n'){if(!p){p=(list*)malloc(sizeof(list));if(!p){cout溢出错误endl;exit(0);}if(i==0){toup-next=p;toup-length++;i++;p-next=NULL;t=p;}else{t-next=p;t=p;p-next=NULL;toup-length++;}}if((ch=='+'||ch=='-')&&(c=='+'||c=='-')){cout未按说明输入,请下次输入前仔细阅读endl;exit(-1);}//检验两次输入是否都是运算符号,若是则退出ch=c;switch(c){case'-':k1=-1;k2=0;k3=0;break;case'+':k1=1;k2=0;k3=0;break;case'x':if(k2==0)p-data=k1*1;elsep-data=k1*dat;dat=0.0;k1=1;k2=0;k3=0;kx=1;break;case'.':k3=1;break;case'':if(k2==1){if(kx==1){p-zhi=k1*(int)dat;dat=0.0;p=p-next;}else{p-data=k1*dat;p-zhi=0;dat=0.0;p=p-next;}}elseif(1==kx){p-zhi=1;p=p-next;}k1=1;k2=0;k3=0;kx=0;break;default:if(!k3)dat=c-48+10*dat;//k3=0,即前面没有小数点时else{dat=dat+(c-48)*fumi(k3);k3++;}k2=1;}//switchc=getchar();}//whileif(ch=='+'||ch=='-'||ch=='.'){cout输入的多项式不完整,请重新输入;exit(-1);}if(0==kx){p-data=k1*dat;p-zhi=0;}else{if(k2==0)p-zhi=k1*1;elsep-zhi=k1*(int)dat;}}//初始化,生成单链表表头,参数是指向头结点的指针voidinit(node*&toup){statusdestory(node*&toup);if(toup)destory(toup);//假如有头结点或元素节点,销毁结点toup=(node*)malloc(sizeof(node));if(!toup){cout溢出错误endl;exit(0);}toup-length=0;toup-next=NULL;}//销毁全部,包括头结点statusdestory(node*&toup){if(toup){list*p=toup-next;list*t;while(p){t=p-next;free(p);p=t;}//whilefree(toup);toup=NULL;}//ifreturnOK;}//找到指数与m相等的元素,返回前一元素的指针,参数是元素指针statusequalmi(list*&element,intm,list*&prep){prep=element;list*t=element-next;while(t&&t-zhi!=m){prep=t;t=t-next;}//whileif(t)return1;//找到了elsereturn0;}//将toup2原样赋给toup1voidcopy(node*&toup1,node*&toup2){if(!toup2){cout初始化未成功endl;exit(0);}init(toup1);list*t2=toup2-next,*t1=toup1-next;while(t2){list*temp=(list*)malloc(sizeof(list));if(!t1){toup1-next=temp;toup1-length++;temp-data=t2-data;temp-zhi=t2-zhi;t2=t2-next;t1=temp;}else{t1-next=temp;toup1-length++;temp-data=t2-data;temp-zhi=t2-zhi;t2=t2-next;t1=temp;}}if(t1)t1-next=NULL;}//按指数绝对值从小到大存放,交换元素值法voidpaixu(node*&toup){if(!toup){cout初始化失败endl;exit(0);}list*a=toup-next;inti=1;while(itoup-length){list*temp=a;list*p=a;list*t=p-next;while(t){if(abs(p-zhi)abs(t-zhi)){p=t;t=t-next;}elset=t-next;}//whilea=a-next;if(p!=temp){floattemp1=temp-data;inttemp2=temp-zhi;temp-data=p-data;temp-zhi=p-zhi;p-data=temp1;p-zhi=temp2;}i++;}//while}//把指数相等的项合并,也可实现多项式加法voidneat(node*&toup){paixu(toup);//先排序list*p=toup-next;list*pre;while(p){if(equalmi(p,p-zhi,pre)){p-data=pre-next-data+p-data;list*temp=pre-next;pre-next=pre-next-next;free(temp);toup-length--;}//ifelsep=p-next;}//while,合并幂相等的项p=toup-next;list*pr=NULL;list*tem=NULL;while(p){if(p-data==0){if(!pr){tem=p;toup-next=p-next;p=p-next;toup-length--;free(tem);tem=NULL;}else{tem=p;pr-next=p-next;toup-length--;p=p-next;free(tem);tem=NULL;}}//ifelse{pr=p;p=p-next;}//else}//while,将数据为0的项删除pr=NULL;}//得到第i个元素的指数/*statusget(node*&toup,inti){if(!toup){cout错误:没有头结点endl;exit(-1);}if(i=0||i(int)toup-data){cout错误:i=0或i大于元素个数endl;exit(-1);}list*t=toup;for(;i0;i--)t=t-next;return(t-zhi);}*///把两个多项式连接起来,用newtoup返回voidconcat(node*&atoup,node*&btoup,node*&newtoup){if(!atoup||!btoup){cout链初始化失败;exit(0);}voidinit(node*&toup);init(newtoup);list*ap=atoup-next;list*bp=btoup-next;list*temp=NULL;inti=1;while(ap){list*t=(list*)malloc(sizeof(list));if(i==1){newtoup-next=t;newtoup-length++;t-data=ap-data;t-zhi=ap-zhi;ap=ap-next;temp=t;i++;}else{temp-next=t;newtoup-length++;t-data=ap-data;t-zhi=ap-zhi;temp=t;ap=ap-next;}}while(bp){list*t=(list*)malloc(sizeof(list));if(temp)temp-next=t;elsetemp=t;newtoup-length++;t-data=bp-data;t-zhi=bp-zhi;bp=bp-next;temp=t;}if(temp)temp-next=NUL
本文标题:一元多项式的各种运算实现(c++,c)
链接地址:https://www.777doc.com/doc-3369171 .html