⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usefultool.cpp

📁 矩阵求逆的c++程序,用来算大规模带分数形式的逆矩阵的,文件输入
💻 CPP
字号:

/*
   矩阵求逆cpp1.cpp

*/
#include<math.h>
#include<iostream.h>
#include<fstream.h>
void out();
long gys(long i,long j)
{   long r;
	long f=(i>=j?i:j);	
    long g=(i>=j?j:i);
	if ((i!=0)&(j!=0))	
	{while (f%g!=0)
	{ r=f%g,f=g,g=r;
		}
  return(g);
	}
	else return(1);

}
class Qtype
{
public:
   int ch;
   long a,b;
   void init(Qtype t)
   {
    ch=t.ch,a=t.a,b=t.b;
      }
   void init(int i,long j,long k)
   {
    ch=i,a=j,b=k;
      }
   void init(double f)
   {
	   if (f>=0)  ch=1;
       else ch=-1;
	   if (f!=0)
	   {   
           f=f*ch;
 		  long x=f;
		  long y=1;
          while ((f-x)>1e-7) //seting算到哪一位
		  {   y=y*10;
			 f=f*10;
		     x=f;
					 } 
		  int d=gys(x,y);
		  a=x/d , b=y/d; 
	   }
	   else {a=0,b=1;}
   }
private: ;

};
Qtype arr[10][20];
int n;
 void readin()
{  
     
    double f;
	ifstream fin("in.txt",ios::nocreate);
	fin>>n;//wait
   int i,j;
   for (i=1;i<=n;i++)
	  {
          for (j=1;j<=n;j++)
		  {  fin>>f; 
		   arr[i][j].init(f);
           arr[i][n+j].init(0);
		  }
		  arr[i][n+i].init(1);
          
   };
 
};
//四种运算
Qtype add(Qtype x,Qtype y)
{ long a,b,k;
  int ch;
  Qtype t;
  a=x.a*y.b*x.ch+y.a*x.b*y.ch;
  b=x.b*y.b;
  ch=(a>=0?1:-1);
  a=a*ch;
  k=gys(a,b);
  a=a/k,b=b/k;
  t.init(ch,a,b);
  return(t);

};
Qtype mut(Qtype x,Qtype y)
{
  Qtype t;
  long a,b,k;
    a=x.a*y.a;
  b=x.b*y.b;
  k=gys(a,b);
  a=a/k,b=b/k;
  t.init(x.ch*y.ch,a,b);
  return(t);

};

Qtype sub(Qtype x,Qtype y)
{
  Qtype t;
  t.init(-1*y.ch,y.a,y.b);
  t=add(x,t);
  return(t);

};
Qtype div(Qtype x,Qtype y)
{
  Qtype t;
  t.init(y.ch,y.b,y.a);
  t=mut(x,t);
  return(t);

};
//四则运算部分结束
 Qtype t;
 void tansfer(int x,int y)
 {Qtype t;
 if (x!=y)
 {
	 for (int j=1;j<=n*2;j++)
	{t.init(arr[x][j]);
     arr[x][j].init(arr[y][j]);
     arr[y][j].init(t);}
 }
  
  }
 void ka(Qtype k,int ri)
{
  for (int j=1;j<=n*2;j++)
	if (arr[ri][j].a!=0) 
		arr[ri][j].init(mut(k,arr[ri][j])); 
}

 void ka_to_b(Qtype k,int x,int y)
{
  Qtype t;
  for (int j=1;j<=n*2;j++)
  {t.init(mut(k,arr[x][j]));
  arr[y][j].init(add(t,arr[y][j])); };
 
}
 void change()
 {  
	 Qtype k;
	int ci,rj;  
	 for (ci=1;ci<=n;ci++)
	 {
		for (rj=ci;rj<=n;rj++)
	   if (arr[rj][ci].a!=0)
	   {  tansfer(ci,rj);
	      k.init(1,1,1);
	      k.init(div(k,arr[ci][ci]) );
	      ka(k,ci);
		  goto loop;};
loop:		  
		  //
	   	for (rj=1;rj<=n;rj++)
			if ((rj!=ci)&(arr[rj][ci].a!=0))
			{ k.init(arr[rj][ci]);
			  k.ch=k.ch*(-1);
			  ka_to_b(k,ci,rj);//
			}
			  out();/**/		
	 } ;
 
 
 }
 void out()//gai guo de
 { 
    for (int i=1;i<=n;i++)
	  {
          for (int j=1;j<=n*2;j++)
		  { 
			 if (arr[i][j].a==0) cout<<" 0 ";//
				 else
			 {
			  if (arr[i][j].ch==1) cout<<" ";//
			  else cout<<"-";
			  if (arr[i][j].b==1)cout<<arr[i][j].a<<" ";
			  else cout<<arr[i][j].a<<"/"<<arr[i][j].b<<" ";}
		  
		  }
		  
          cout<<"\n";}

           cout<<"\n";
}
void main()
{  
loop1:  
   readin();
   change();
   out();
   cout<<"do you want go on?then press 1\n";
   int u;
   cin>>u;
   if (u==1) goto loop1;
   /*char ch;
   ch=kbhit();
   if (ch=='r') goto loop1;*/
	   //读入数据
     /*for (int i=1;i<=n;i++)
	  {
          for (int j=1;j<=n;j++)
		  {  cout<<arr[i][j].a<<"/"<<arr[i][j].b<<" ";
		  
		  }
		  
          cout<<"\n";}*/
     /*cout<<arr[1][1].ch<<" "<<arr[1][1].a<<"/"<<arr[1][1].b<<" ";
	 t.init(-1,189,347);
	 t=div(t,arr[1][1]);
      cout<<t.ch<<" "<<t.a<<"/"<<t.b<<" ";*/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -