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

📄 matrix.bak

📁 一些基本的矩阵算法
💻 BAK
字号:
class matrix{
      int **mat1,**mat2,**mat3;
      int r1,r2,r3,c1,c2,c3;
      public :
      matrix(){
	  r1=0;r2=0;
	  c1=0;c2=0;
      }
      void createMem(int,int,int,int,int,int);
      //six args. row and col args for each matrix.
      void matAdd();
      void matSub();
      void matMul();
};

void matrix :: createMem(int r1,int c1,int r2,int c2,int r3,int c3){
     int i;
     mat1 = new int *[r1];
     for(i=0;i<r1;i++)
	mat1[i] = new int[c1];

     mat2 = new int *[r2];
     for(i=0;i<r2;i++)
	mat2[i] = new int[c2];

     mat3 = new int *[r3];
     for(i=0;i<r3;i++)
	mat3[i] = new int[c3];
}

void matrix :: matAdd(){
     clrscr();
     cout<<"\n\n*****Matrix Addition*****\n\n";
     cout<<"Rule : Order of Matrix should be equal\n\n";
     cout<<"Enter Row and Column order for Matrix 1 : ";
     cin>>r1>>c1;
     cout<<"Enter Row and Column order for Matrix 2 : ";
     cin>>r2>>c2;

     if(r1!=r2 || c1!=c2)
	 cout<<"\n\nNot satisfying Rule\n";
     else
     {
	//Create Enough Memory
	createMem(r1,c1,r2,c2,r1,c2);

	//Key-in Data entry
	int i,j;
	cout<<"\nKey-in Data Entry for Matrix 1\n";
	for(i=0;i<r1;i++){
	  for(j=0;j<c1;j++){
	     cin>>mat1[i][j];
	  }
	}
	cout<<"\nKey-in Data Entry for Matrix 2\n";
	for(i=0;i<r2;i++){
	  for(j=0;j<c2;j++){
	     cin>>mat2[i][j];
	  }
	}
	cout<<"\n\nMatrix Addition\n";
	for(i=0;i<r1;i++){
	  for(j=0;j<c1;j++){
	     mat3[i][j] = mat1[i][j] + mat2[i][j];
	     cout<<setw(5)<<mat3[i][j];
	  }
	  cout<<endl;
	}
     }
     getch();
}

void matrix :: matSub(){
     clrscr();
     cout<<"\n\n*****Matrix Subtraction*****\n\n";
     cout<<"Rule : Order of Matrix should be equal\n\n";
     cout<<"Enter Row and Column order for Matrix 1 : ";
     cin>>r1>>c1;
     cout<<"Enter Row and Column order for Matrix 2 : ";
     cin>>r2>>c2;

     if(r1!=r2 || c1!=c2)
	 cout<<"\n\nNot satisfying Rule\n";
     else
     {
	//Create Enough Memory
	createMem(r1,c1,r2,c2,r1,c2);

	//Key-in Data entry
	int i,j;
	cout<<"\nKey-in Data Entry for Matrix 1\n";
	for(i=0;i<r1;i++){
	  for(j=0;j<c1;j++){
	     cin>>mat1[i][j];
	  }
	}
	cout<<"\nKey-in Data Entry for Matrix 2\n";
	for(i=0;i<r2;i++){
	  for(j=0;j<c2;j++){
	     cin>>mat2[i][j];
	  }
	}
	cout<<"\n\nMatrix Subtraction\n";
	for(i=0;i<r1;i++){
	  for(j=0;j<c1;j++){
	     mat3[i][j] = mat1[i][j] - mat2[i][j];
	     cout<<setw(5)<<mat3[i][j];
	  }
	  cout<<endl;
	}
     }
     getch();
}


void matrix :: matMul(){
     clrscr();
     cout<<"\n\n*****Matrix Multiplication*****\n\n";
     cout<<"Rule : If no. of columns of matrix1 and no. of"
	 <<" rows of matrix2 are same then A*B is possible.\n\n";
     cout<<"Enter Row and Column order for Matrix 1 : ";
     cin>>r1>>c1;
     cout<<"Enter Row and Column order for Matrix 2 : ";
     cin>>r2>>c2;

     if(c1!=r2)
	 cout<<"\n\nNot satisfying Rule\n";
     else
     {
	//Create Enough Memory
	createMem(r1,c1,r2,c2,r1,c2);

	//Key-in Data entry
	int i,j,k;
	cout<<"\nKey-in Data Entry for Matrix 1\n";
	for(i=0;i<r1;i++){
	  for(j=0;j<c1;j++){
	     cin>>mat1[i][j];
	  }
	}
	cout<<"\nKey-in Data Entry for Matrix 2\n";
	for(i=0;i<r2;i++){
	  for(j=0;j<c2;j++){
	     cin>>mat2[i][j];
	  }
	}
	cout<<"\n\nMatrix Multiplication\n";
	for(i=0;i<r1;i++){
	  for(j=0;j<c2;j++){
	      mat3[i][j]=0;
	   for(k=0;k<r2;k++){
	     mat3[i][j] += mat1[i][k] * mat2[k][j];
	  }
	   cout<<setw(5)<<mat3[i][j];
	}
	 cout<<endl;
	}
     }
     getch();
}

⌨️ 快捷键说明

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