解方程组的c++代码.doc

上传人:最*** 文档编号:1598994 上传时间:2019-10-29 格式:DOC 页数:15 大小:33.25KB
返回 下载 相关 举报
解方程组的c++代码.doc_第1页
第1页 / 共15页
解方程组的c++代码.doc_第2页
第2页 / 共15页
解方程组的c++代码.doc_第3页
第3页 / 共15页
点击查看更多>>
资源描述
_Cramer算法解方程组Gauss列主元解方程组Gauss全主元解方程组用Doolittle算法解方程组/解线性方程组#include#include#include/-全局变量定义区const int Number=15; /方程最大个数double aNumberNumber,bNumber,copy_aNumberNumber,copy_bNumber; /系数行列式int A_yNumber; /a中随着横坐标增加列坐标的排列顺序,如a00,a12,a21.则A_y=0,2,1.;int lenth,copy_lenth; /方程的个数double a_sum; /计算行列式的值char * x; /未知量a,b,c的载体/-函数声明区void input(); /输入方程组void print_menu(); /打印主菜单int choose (); /输入选择void cramer(); /Cramer算法解方程组void gauss_row(); /Gauss列主元解方程组void guass_all(); /Gauss全主元解方程组void Doolittle(); /用Doolittle算法解方程组int Doolittle_check(double aNumber,double bNumber); /判断是否行列式0,若是,调整为顺序主子式全0void xiaoqu_u_l(); /将行列式Doolittle分解void calculate_u_l(); /计算Doolittle结果 double & calculate_A(int n,int m); /计算行列式double quanpailie_A(); /根据列坐标的排列计算的值,如A_y=0,2,1,得sum=a0 A_y0 * a1 A_y1 * a2 A_y2 =a00*a12*a21;void exchange(int m,int i); /交换A_ym,A_yivoid exchange_lie(int j); /交换aj与b;void exchange_hang(int m,int n); /分别交换a和b中的m与n两行void gauss_row_xiaoqu(); /Gauss列主元消去法void gauss_all_xiaoqu(); /Gauss全主元消去法void gauss_calculate(); /根据Gauss消去法结果计算未知量的值void exchange_a_lie(int m,int n); /交换a中的m和n列void exchange_x(int m,int n); /交换x中的xm和xnvoid recovery(); /恢复数据/主函数void main() int flag=1; input(); /输入方程 while(flag) print_menu(); /打印主菜单 flag=choose(); /选择解答方式 /函数定义区void print_menu() system(cls); cout-方程系数和常数矩阵表示如下:n; for(int j=0;jlenth;j+) cout系数j+1 ; coutt常数; coutendl; for(int i=0;ilenth;i+) for(j=0;jlenth;j+) coutsetw(8)setiosflags(ios:left)aij; couttbiendl; cout-请选择方程解答的方案-; coutn 1. 克拉默(Cramer)法则; coutn 2. Gauss列主元消去法; coutn 3. Gauss全主元消去法; coutn 4. Doolittle分解法; coutn 5. 退出; coutn 输入你的选择:;void input() int i,j; coutlenth; if(lenthNumber) coutIt is too big.n; return; x=new charlenth; for(i=0;ilenth;i+) xi=a+i;/输入方程矩阵 /提示如何输入 cout=n; cout请在每个方程里输入lenth系数和一个常数:n; cout例:n方程:a; for(i=1;ilenth;i+) cout+i+1xi; cout=10n; cout应输入:; for(i=0;ilenth;i+) couti+1 ; cout10n; cout=n; /输入每个方程 for(i=0;ilenth;i+) cout输入方程i+1:; for(j=0;jaij; cinbi; /备份数据 for(i=0;ilenth;i+) for(j=0;jlenth;j+) copy_aij=aij; for(i=0;ichoice; switch(choice) case 1:cramer();break; case 2:gauss_row();break; case 3:guass_all();break; case 4:Doolittle();break; case 5:return 0; default:cout输入错误,请重新输入:; choose(); break; coutch; if(ch=n|ch=N) return 0; recovery(); coutnnn; return 1;/用克拉默法则求解方程.void cramer() int i,j;double sum,sum_x;char ch; /令第i行的列坐标为i cout用克拉默(Cramer)法则结果如下:n; for(i=0;ilenth;i+) A_yi=i; sum=calculate_A(lenth,0); if(sum!=0) cout系数行列式不为零,方程有唯一的解:; for(i=0;ilenth;i+) ch=a+i; a_sum=0; for(j=0;jlenth;j+) A_yj=j; exchange_lie(i); sum_x=calculate_A(lenth,0); coutendlch=sum_x/sum; exchange_lie(i); else cout系数行列式等于零,方程没有唯一的解.; coutn;double & calculate_A(int n,int m) /计算行列式 int i; if(n=1) a_sum+= quanpailie_A(); elsefor(i=0;in;i+) exchange(m,m+i); calculate_A(n-1,m+1); exchange(m,m+i); return a_sum;double quanpailie_A() /计算行列式中一种全排列的值 int i,j,l; double sum=0,p; for(i=0,l=0;ilenth;i+) for(j=0;A_yj!=i&ji) l+; for(p=1,i=0;ilenth;i+) p*=aiA_yi; sum+=p*(l%2=0)?(1):(-1); return sum;/高斯列主元排列求解方程void gauss_row() int i,j; gauss_row_xiaoqu(); /用高斯列主元消区法将系数矩阵变成一个上三角矩阵 for(i=0;ilenth;i+) for(j=0;jlenth;j+) coutsetw(10)setprecision(5)aij; coutsetw(10)biendl; if(alenth-1lenth-1!=0) cout系数行列式不为零,方程有唯一的解:n; gauss_calculate(); for(i=0;ilenth;i+) /输出结果 coutxi=bin; else cout系数行列式等于零,方程没有唯一的解.n;void gauss_row_xiaoqu() /高斯列主元消去法 int i,j,k,maxi;double lik; cout用Gauss列主元消去法结果如下:n; for(k=0;klenth-1;k+) j=k; for(maxi=i=k;iamaxij) maxi=i; if(maxi!=k) exchange_hang(k,maxi);/ for(i=k+1;ilenth;i+) lik=aik/akk; for(j=k;jlenth;j+) aij=aij-akj*lik; bi=bi-bk*lik; /高斯全主元排列求解方程void guass_all() int i,j; gauss_all_xiaoqu(); for(i=0;ilenth;i+) for(j=0;jlenth;j+) coutsetw(10)setprecision(5)aij; coutsetw(10)biendl; if(alenth-1lenth-1!=0) cout系数行列式不为零,方程有唯一的解:n; gauss_calculate(); for(i=0;ilenth;i+) /输出结果 for(j=0;xj!=a+i&jlenth;j+); coutxj=bjendl; else cout系数行列式等于零,方程没有唯一的解.n;void gauss_all_xiaoqu() /Gauss全主元消去法 int i,j,k,maxi,maxj;double lik; cout用Gauss全主元消去法结果如下:n; for(k=0;klenth-1;k+) for(maxi=maxj=i=k;ilenth;i+) for(j=k;jamaxi maxj) maxi=i; maxj=j; if(maxi!=k) exchange_hang(k,maxi); if(maxj!=k) exchange_a_lie(maxj,k); /交换两列 exchange_x(maxj,k); for(i=k+1;ilenth;i+) lik=aik/akk; for(j=k;j=0;i-) for(j=i+1,sum_ax=0;jlenth;j+) sum_ax+=aij*bj; bi=(bi-sum_ax)/aii; void Doolittle() /Doolittle消去法计算方程组 double temp_aNumberNumber,temp_bNumber;int i,j,flag; for(i=0;ilenth;i+) for(j=0;jlenth;j+) temp_aij=aij; flag=Doolittle_check(temp_a,temp_b); if(flag=0) coutn行列式为零.无法用Doolittle求解.; xiaoqu_u_l(); calculate_u_l(); cout用Doolittle方法求得结果如下:n; for(i=0;ilenth;i+) /输出结果 for(j=0;xj!=a+i&jlenth;j+); coutxj=bjendl; void calculate_u_l() /计算Doolittle结果 int i,j;double sum_ax=0; for(i=0;ilenth;i+) for(j=0,sum_ax=0;j=0;i-) for(j=i+1,sum_ax=0;jlenth;j+) sum_ax+=aij*bj; bi=(bi-sum_ax)/aii; void xiaoqu_u_l() /将行列式按Doolittle分解 int i,j,n,k;double temp; for(i=1,j=0;ilenth;i+) aij=aij/a00; for(n=1;nlenth;n+) /求第n+1层的上三角矩阵部分即U for(j=n;jlenth;j+) for(k=0,temp=0;kn;k+) temp+=ank*akj; anj-=temp; for(i=n+1;ilenth;i+) /求第n+1层的下三角矩阵部分即L for(k=0,temp=0;kn;k+) temp+=aik*akn; ain=(ain-temp)/ann; int Doolittle_check(double temp_aNumber,double temp_bNumber) /若行列式不为零,将系数矩阵调整为顺序主子式大于零 int i,j,k,maxi;double lik,temp; for(k=0;klenth-1;k+) j=k; for(maxi=i=k;itemp_amaxij) maxi=i; if(maxi!=k) exchange_hang(k,maxi); for(j=0;jlenth;j+) temp=temp_akj; temp_akj=temp_amaxij; temp_amaxij=temp; for(i=k+1;ilenth;i+) lik=temp_aik/temp_akk; for(j=k;jlenth;j+) temp_aij=temp_aij-temp_akj*lik; temp_bi=temp_bi-temp_bk*lik; if(temp_alenth-1lenth-1=0) return 0; return 1;void exchange_hang(int m,int n) /交换a中和b两行 int j; double temp; for(j=0;jlenth;j+) temp=amj; amj=anj; anj=temp; temp=bm; bm=bn; bn=temp;void exchange(int m,int i) /交换A_ym,A_yi int temp; temp=A_ym; A_ym=A_yi; A_yi=temp;void exchange_lie(int j) /交换未知量b和第i列 double temp;int i; for(i=0;ilenth;i+) temp=aij; aij=bi; bi=temp; void exchange_a_lie(int m,int n) /交换a中的两列 double temp;int i; for(i=0;ilenth;i+) temp=aim; aim=ain; ain=temp; void exchange_x(int m,int n) /交换未知量xm与xn char temp; temp=xm; xm=xn; xn=temp;void recovery() /用其中一种方法求解后恢复数据以便用其他方法求解 for(int i=0;ilenth;i+) for(int j=0;jlenth;j+) aij=copy_aij; for(i=0;ilenth;i+) bi=copy_bi; for(i=0;ilenth;i+) xi=a+i; a_sum=0; lenth=copy_lenth;THANKS !致力为企业和个人提供合同协议,策划案计划书,学习课件等等打造全网一站式需求欢迎您的下载,资料仅供参考-可编辑修改-
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!