求解方程-源程序.doc

上传人:scccc 文档编号:12714950 上传时间:2021-12-05 格式:DOC 页数:14 大小:33.50KB
返回 下载 相关 举报
求解方程-源程序.doc_第1页
第1页 / 共14页
求解方程-源程序.doc_第2页
第2页 / 共14页
求解方程-源程序.doc_第3页
第3页 / 共14页
求解方程-源程序.doc_第4页
第4页 / 共14页
求解方程-源程序.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《求解方程-源程序.doc》由会员分享,可在线阅读,更多相关《求解方程-源程序.doc(14页珍藏版)》请在三一文库上搜索。

1、#include "iostream.h" #include "stdio.h" #include "conio.h" /基本输入输出文件#include "string.h" #include "stdlib.h" /定义杂项函数及内存分配函数#include "ctype.h" /字符处理#include "math.h" char terms9920;class Equation /定义类Equation public:Equation() void

2、 jecfc(int,int,int); /解二次方程void getterm(char *,char *,int &); /存放方程各项void getyz(int *,int &,int); /得到因子void jc(char *,char *,float); /降次void gettermf(char *,char *,int); /得到指定幂的项int getxs(char *); /得到系数(整数)int getqium(char *); /求幂int yang(char *,float); /检验根是否正确int exp(int,int); /求某数的次方(整数)f

3、loat knyz(int *,int *,int,int,char *); /求所有可能的根float exp(float,int); /求某数的次方(实数)/返回equ的系数乘以val的equ的次数的次方的值float calcequval(char *,float); ;void main()cout<<"使用说明(允许 & 不允许):"<<endl; cout<<endl; cout<<"1. 在输入的方程后面不能输入 = ;"<<endl; cout<<"

4、2. 所输入的方程必须是从高次到低次依次输入; "<<endl; cout<<"3. 若某项系数为0,可以不输入或输入0作为系数;"<<endl; cout<<"4. 不要用分数作为系数且第一项系数绝对值不小于一;"<<endl; cout<<"5. 要用xn表示指数,x n一可以,但不能用x n表示。"<<endl; cout<<endl;Equation E;char *equ,*equ2;int loc=0,a=0,b=0,

5、c=0,numterms;int deg,f_a1000,f_k1000,num_a,num_k;int i;float result;equ=new char5000; equ2=new char5000; cout<<"请输入方程: "<<endl;gets(equ);if(strlen(equ)=0)cout<<"没有输入方程!"<<endl;for(i=0;i<99&&equloc!='='&&equloc!='0'&&a

6、mp;equloc!='n'i+)E.getterm(equ,termsi,loc); numterms=i;if(deg=E.getqium(terms0)<2)cout<<"无效输入,输入的方程最高次应大于等于2次"<<endl;return;cout<<"该方程的根为:"while(deg>2)E.getyz(f_a,num_a,E.getqium(terms0);for(i=1;i<numterms;i+)if( E.getqium(termsi)=0)c= E.getxs(t

7、ermsi);E.getyz(f_k,num_k,c);if(result= E.knyz(f_a,f_k,num_a,num_k,equ)=0)if(result<0.0f&&result>-0.05f)cout<<"错误!不能解得根。"<<endl;return;E.jc(equ,equ2,result);strcpy(equ,equ2);loc=0;for(i=0;i<99&&equloc!='='&&equloc!='0'&&eq

8、uloc!='n;i+)E.getterm(equ,termsi,loc);numterms=i;deg-;for(i=1;i<numterms;i+)if( E.getqium(termsi)=1)b= E.getxs(termsi);for(i=1;i<numterms;i+)if( E.getqium(termsi)=0)c= E.getxs(termsi);E.jecfc( E.getxs(terms0),b,c);void Equation:jecfc(int a,int b,int c) /解二次方程int r1,r2,r3;float r11,r12;if(b

9、*b-4*a*c<0)r1=b*-1;r2=(b*b-4*a*c)*-1;r3=2*a;cout<<"("<<r1<<"+i*sqrt("<<r2<<")/"<<r3;cout<<" , "cout<<"("<<r1<<"-i*sqrt("<<r2<<")/"<<r3;elseif(sqrt(b*

10、b-4*a*c)-(int)sqrt(b*b-4*a*c)=0)r11=(float)(-b+sqrt(b*b-4*a*c)/(2*a);r12=(float)(-b-sqrt(b*b-4*a*c)/(2*a);cout<<r11<<" , "<<r12;elser1=b*-1;r2=b*b-4*a*c;r3=2*a;cout<<"("<<r1<<"+sqrt("<<r2<<")/"<<r3;cout<

11、<" , "cout<<"("<<r1<<"-sqrt("<<r2<<")/"<<r3;cout<<endl;void Equation:getterm(char *src,char *dest,int &loc) /存放方程各项 int loc2=0;if(srcloc='-')destloc2='-'loc2+;for(;srcloc+loc2!='+'&&a

12、mp;srcloc+loc2!='-'&&srcloc+loc2!='n'&&srcloc+loc2!='0'&&srcloc+loc2!='='loc2+)destloc2=srcloc+loc2; destloc2='0'loc+=loc2;if(srcloc!='-'&&srcloc!='0')loc+; void Equation:getyz(int *yz_list,int &num,int val)

13、/得到因子int i,fcount=0;num=0;val=abs(val);for(i=1;i<=val;i+)if(float)(float)(float)val/(float)i)-(int)(float)val/(float)i)=0.0f)num+;yz_listfcount=i;fcount+;int Equation:getxs(char *term) /得到系数char *temp;int i=0,mult=1;temp=new charstrlen(term);if(term0='-')mult=-1;i+;for(;!isdigit(termi)&am

14、p;&!isalpha(termi);i+);if(termi='x')return mult;for(int j=0;isdigit(termi+j);j+)tempj=termi+j;return atoi(temp)*mult;int Equation:getqium(char *term) /求幂char temp10;if(strchr(term,'x')!=NULL)if(strchr(term,'')!=NULL)for(int i=0;termi!=''i+);i+;for(int j=0;isdigit(

15、termi);i+,j+)tempj=termi;tempj='0'return atoi(temp);elsereturn 1;elsereturn 0;int Equation:yang(char *equ,float root) /检验根是否正确char terms9940;float val99,totalval=0;int loc=0;for(int i=0;equloc!='='&&equloc!='0'&&equloc!='n'i+)getterm(equ,termsi,loc);in

16、t numterms=i;for(i=0;i<numterms;i+)vali= calcequval(termsi,root);totalval+=vali;if(totalval*0.99f>-0.002f&&totalval*0.99f<0.002f)return 0;elsereturn 1;int Equation: exp(int val,int e) /求某数的次方(整数)int v2=val;if(e=0) return 1;for(int i=1;i<e;i+) v2*=val;return v2;float Equation:exp(

17、float val,int e) /求某数的次方(实数)float v2=val;if(e=0) return 1.0f;for(int i=1;i<e;i+) v2*=val; return v2;/返回equ的系数乘以val的equ的次数的次方的值float Equation:calcequval(char *equ,float val) int coeff= getxs(equ);int power= getqium(equ);return coeff* exp(val,power);void Equation:jc(char *base,char *newequ,float yz

18、) /降次int synthterms99;int newterms99;int j=0,olddegree;char temp50="",exp10="",exp110=""char newequ120,newequ220; if( yang(base,yz)return;olddegree=getqium(terms0);for(int i=0;i<=olddegree;i+)gettermf(base,temp,i);synthtermsi=getxs(temp);newtermsolddegree-1=(int)synt

19、htermsolddegree;for(i=olddegree-1;i>=1;i-) newtermsi-1=(int)(float)(newtermsi*yz+synthtermsi);for(i=olddegree-1;newtermsi=0;i-);strcpy(newequ,"");for(;i>=0;i-)if(newtermsi!=0)switch(i)case 0:strcpy(exp,"");break;case 1:strcpy(exp,"x");break;default:itoa(i,exp1,10)

20、;strcpy(exp,"x");strcat(exp,exp1);break;if(newtermsi!=1)if(newtermsi<0)itoa(newtermsi,newequ1,10);strcat(newequ1,exp);elseif(i<olddegree-1)strcpy(newequ1,"+");itoa(newtermsi,newequ2,10);strcat(newequ1,newequ2);strcat(newequ1,exp);elseitoa(newtermsi,newequ1,10);strcat(newequ

21、1,exp);elseif(newtermsi<0)strcpy(newequ1,exp);elseif(i<olddegree-1)strcpy(newequ1,"+");strcat(newequ1,exp);elsestrcpy(newequ1,exp);if(newtermsi=1&&exp0='0')if(newtermsi>0)strcpy(newequ1,"+");itoa(newtermsi,newequ2,10);strcat(newequ1,newequ2);elseitoa(newt

22、ermsi,newequ1,10);strcat(newequ,newequ1);/得到指定幂的项void Equation:gettermf(char *equ,char *dest,int power) int t=getqium(terms0);for(int i=0;i<t+1;i+)if( getqium(termsi)=power)strcpy(dest,termsi);return;strcpy(dest,"0");/求所有可能的根float Equation:knyz(int *a,int *k,int n_a,int n_k,char *equ) f

23、or(int i=0;i<n_a;i+)for(int j=0;j<n_k;j+)if( yang(equ,(float)(float)kj/(float)ai)=0)int r=kj/ai;cout<<r<<" , "return (float)r;if( yang(equ,(float)(float)kj/(float)ai)*-1.0f)=0)int r=-kj/ai;cout<<r<<" , "return (float)r;if( yang(equ,0)=0)cout<<"0"<<" , "return 0;return NULL;推荐精选

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1