关闭

求解方程Ax=b的通用程序

标签: AxbVC++
241人阅读 评论(0) 收藏 举报
分类:
在cpp所在的文件夹中创建一个txt文件,并且将条件这个输入:%矩阵行数9%矩阵列数9%矩阵元素31 -13 0 0 0 -10 0 0 0-13 35 -9 0 -11 0 0 0 00 -9 31 -10 0 0 0 0 00 0 -10 79 -30 0 0 0 -90 0 0 -30 57 -7 0 -5 00 0 0 0 -7 47 -30 0 00 0 0 0 0 -30 41 0 00 0 0 0 0 -5 0 0 27 -20 0 0 -9 0 0 0 -2 29 %对应的右侧的元素:-15 27 -23 0 -20 12 -7 7 10 #include#include#include#define R 50#define C 50using namespace std;float Mar[R][C];int InputToTxt(float a,float b);int sort(float a[],int n);void getGauss(float bk[],float num);float jidu(float a);int exchangeMar(int m,int curpos);int chude(int num,int curpos);float sum(float xx[],float yy[],int num);void solve(int curpos);float row,col;int main(void){FILE *stream;char msg[40]; float bb[R];stream=fopen("data.txt","r"); fseek(stream,0,SEEK_SET);fgets(msg,20,stream);printf("%s",msg);float a,b;fscanf(stream,"%f",&a);cout<<a<<endl;fseek(stream,2,1); fgets(msg,20,stream);printf("%s",msg);fscanf(stream,"%f",&b);cout<<b<<endl;row=a;col=b+1;fseek(stream,2,1); fgets(msg,20,stream); printf("%s",msg); for(int i=0;i<a;i++){for(int j=0;j<b;j++){fscanf(stream,"%f",&Mar[i][j]);cout<<Mar[i][j]<<" ";}cout<<endl;}fseek(stream,4,1);fgets(msg,40,stream);printf("%s",msg);for(int k=0;k<a;k++){ fscanf(stream,"%f",&bb[k]); cout<<bb[k]<<" ";};cout<<endl;//得到增广矩阵,并且将增广矩阵放到文件中去for(int ii=0;ii<a;ii++){Mar[ii][int(b)]=bb[ii];}fclose(stream);int curpos1=InputToTxt(a,b+1); //现在我要将里面的值进行交换 int curpos2=exchangeMar(0,curpos1); int curpos4=curpos2; //现在我要用第二行去消去第三行 for(int i5=1;i5<a-1;i5++){ curpos4=chude(i5,curpos4);} //现在要做的是解方程 solve(curpos4); return 0;}//将增广矩阵输入到文件中 int InputToTxt(float a,float b) {FILE *stream=fopen("data.txt","a+");char str[]="%增广矩阵:"; fprintf(stream,"%s",str); fprintf(stream,"\n");int curpos=ftell(stream);for(int i=0;i<a;i++){for(int j=0;j<b;j++){fprintf(stream,"%f",Mar[i][j]);fprintf(stream,"%c"," ");} fprintf(stream,"\n"); }fclose(stream);return curpos; } //从矩阵中获取一列的元素int sort(float a[],int n){float max=a[0];int k=0;for(int i=1;i<n;i++){ if(max<a[i]){max=a[i];k=i;}}//此时返回的是数组对应的元素的下标return k;}//这个函数是得到一个gauss变换的矩阵并且将这个矩阵在写文件上int exchangeMar(int m,int curpos){float G1[50][50];char ch; FILE *stream;stream=fopen("data.txt","r");fseek(stream,curpos,0);cout<<"增广矩阵"<<endl;cout<<"-----------------------------------------------------------"<<endl; for(int i=0;i<row;i++){for(int j=0;j<col;j++){ fscanf(stream,"%f",&G1[i][j]); fscanf(stream,"%c",&ch); cout<<G1[i][j]<<" ";}cout<<endl;}cout<<"-----------------------------------------------------------"<<endl;//我要第一列进行Gauss消去float ga=-G1[1][0]*1.0/G1[0][0]; for(int i1=0;i1<col;i1++){ if(G1[0][i1]!=0){ G1[1][i1]=G1[1][i1]+G1[0][i1]*ga;}}for(int i4=1;i4<row;i4++){if(G1[i4][0]!=0){ga=-G1[i4][0]*1.0/G1[0][0]; for(int j4=0;j4<col;j4++){ G1[i4][j4]=G1[i4][j4]+G1[0][j4]*ga;}}}cout<<"第一次初等行变换之后:"<<endl; cout<<"-------------------------------------------------------------"<<endl; for(int i2=0;i2<row;i2++){for(int j2=0;j2<col;j2++){ if(jidu(G1[i2][j2])<0.0001) { G1[i2][j2]=0; } cout<<G1[i2][j2]<<" ";}cout<<endl;}cout<<"--------------------------------------------------------------"<<endl;fclose(stream);//现在要将这个矩阵写入到文件中 stream=fopen("data.txt","a+");char msg[]="第一次初等行变换之后:";fprintf(stream,"%s",msg);fprintf(stream,"\n");int curpos1=ftell(stream);for(int i3=0;i3<row;i3++){for(int j3=0;j3<col;j3++){fprintf(stream,"%f",G1[i3][j3]);fprintf(stream,"%c"," ");}fprintf(stream,"\n");}fclose(stream);return curpos1;}float jidu(float a){if(a<0)return -a;elsereturn a;}int chude(int num,int curpos){//获取上一次的矩阵 float bk[50][50];char ch;FILE *stream=fopen("data.txt","r");fseek(stream,curpos,0);//获取第一次行变换之后的矩阵for(int i=0;i<row;i++){for(int j=0;j<col;j++){fscanf(stream,"%f",&bk[i][j]);fscanf(stream,"%c",&ch);}}fclose(stream);//用第二行去消去第三行float aa=-bk[num+1][num]*1.0/bk[num][num];for(int i1=0;i1<col;i1++){ if(bk[num][i1]!=0){ bk[num+1][i1]=bk[num+1][i1]+bk[num][i1]*aa;}} for(int i4=num+1;i4<row;i4++){if(bk[i4][num]!=0){aa=-bk[i4][num]*1.0/bk[num][num]; for(int j4=0;j4<col;j4++){ bk[i4][j4]=bk[i4][j4]+bk[num][j4]*aa;}}}cout<<"第"<<num+1<<"初等行变换之后的矩阵:"<<endl;cout<<"-----------------------------------------------"<<endl;for(int i2=0;i2<row;i2++){for(int j2=0;j2<col;j2++){if(jidu(bk[i2][j2])<0.0001){ bk[i2][j2]=0;}cout<<bk[i2][j2]<<" ";}cout<<endl;}cout<<"------------------------------------------------"<<endl;//现在要做的是将这个矩阵写到文件中去 stream=fopen("data.txt","a+"); char msg[]="然后再初等变换之后的矩阵:"; fprintf(stream,"%s",msg); fprintf(stream,"\n"); curpos=ftell(stream); for(int i3=0;i3<row;i3++) { for(int j3=0;j3<col;j3++) { fprintf(stream,"%f",bk[i3][j3]); fprintf(stream,"%c"," "); } fprintf(stream,"\n"); } fclose(stream); return curpos;}void solve(int curpos){//首先是从文件中获取到这个矩阵FILE *stream=fopen("data.txt","r");float xx[50];float bk[50][50];char ch;fseek(stream,curpos,0);for(int i=0;i<row;i++){for(int j=0;j<col;j++){fscanf(stream,"%f",&bk[i][j]);fscanf(stream,"%c",&ch);}}cout<<"方程组Ax=b的解:"<<endl; cout<<"--------------------------------------------"<=0;i1--){ xx[i1]=(bk[i1][9]-sum(xx,bk[i1],i1))/bk[i1][i1];}for(int i2=0;i2<row;i2++){cout<<"X"<<i2+1<<"= "<<xx[i2]<<endl;} }float sum(float xx[],float yy[],int num){float summ=0.0;for(int i=1;i<row-num;i++){summ+=yy[num+i]*xx[num+i];}return summ;}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:147603次
    • 积分:2939
    • 等级:
    • 排名:第12448名
    • 原创:157篇
    • 转载:15篇
    • 译文:22篇
    • 评论:4条
    文章分类
    最新评论