返回首页

#include<iostream>

using namespace std;

class matrix

{

 int dim1,dim2;

 int **mat;

 public:

 matrix(int x=2,int y=2)

 {

  dim1=x;

  dim2=y;

  mat=new int*[dim1];

  for(int i=0;i<dim1;i++)

   mat[i]=new int[dim2];

  for(int i=0;i<dim1;i++)

   for(int j=0;j<dim2;j++)

    mat[i][j]=0;

 }

 int returndim1()

 {

  return dim1;

 }

 int returndim2()

 { 

  return dim2;

 }

 void input()

 {

  cout<<"Enter the elements of matrix - ";

  for(int i=0;i<dim1;i++)

   for(int j=0;j<dim2;j++)

    cin>>mat[i][j];

 }

 void out()

 {

  for(int i=0;i<dim1;i++)

   {

    cout<<"\n";

    for(int j=0;j<dim2;j++)

 cout<<mat[i][j]<<" ";

   }

 } 

 matrix operator+(matrix x)

 {

  matrix c(dim1,dim2);

  if(dim1==x.returndim1() && dim2==x.returndim2())

   {    

    for(int i=0;i<dim1;i++)

      for(int j=0;j<dim2;j++)

    c.mat[i][j]=this->mat[i][j]+x.mat[i][j];

 return c;

   }

  else

  {

   cout<<"Matrix cant be added";  

   return c;  

  }

 }

 matrix operator-(matrix x)

 {

  matrix c(dim1,dim2);

  if(dim1==x.returndim1() && dim2==x.returndim2())

   { 

    for(int i=0;i<dim1;i++)

      for(int j=0;j<dim2;j++)

    c.mat[i][j]=this->mat[i][j]-x.mat[i][j];

 return c;

   }

  else

  {

   cout<<"Matrix cant be subtracted";  

   return c;  

  }

 }

 matrix operator*(matrix x)

 {

  matrix c(dim1,x.returndim2());

  if(dim2==x.returndim1())

   { 

    for(int i=0;i<dim1;i++)

      for(int j=0;j<x.returndim2();j++)

      for(int k=0;k<dim2;k++)

        c.mat[i][j]+=this->mat[i][k]*x.mat[k][j];

 return c;

   }

  else

  {

   cout<<"Matrix cant be multiplied";  

   return c;  

  }

 }

};

int main()

{

 int x,y;

 char ch;

 do{

      cout<<"\n WELCOME TO MATRIX COMPUTATION"<<endl;

      cout<<"\n THE ASSIGNMENT WAS DONE BY:"<<endl;

      cout<<"\n                               "<<endl;

      cout<<"\n                               "<<endl;

      cout<<"\n                              "<<endl;

      cout<<"\n THE ASSIGNMENT IS DONE  FOR:;<endl;

      cout<<"\n*********************************************************"<<endl;

      cout<<"\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;

      cout<<"\n*********************************************************"<<endl;      

            

      cout<<"\nTHIS ASSIGNMENT IS TO COVER THE FOLLOWING: "<<endl;

      cout<<"\n                         > Find and displaying Determinant"<<endl;

      cout<<"\n                         > Find and displaying the Inverse"<<endl;

      cout<<"\n                           matrix"<<endl;

      cout<<"\n                         > Carrying out row transformation"<<endl;

      cout<<"\n                         > Addition & Subtraction"<<endl;

      cout<<"\n                         > Multiplicationn & division"<<endl;

      cout<<"\n"<<endl;

 cout<<"\nEnter elements, select the value then space-bar then input next"<<endl;

 cout<<"Enter the dimension of matrix 1 (rows x cols) - : "<<endl;

 cout<<"\n"<<endl;  

 cin>>x>>y;

 matrix a(x,y);

 a.input();

 cout<<"\nEnter elements, select the value then space-bar then input next"<<endl;

 cout<<"\nEnter the dimension of matrix 2 (rows x cols) - : "<<endl;

 cout<<"\n"<<endl; 

 cin>>x>>y;

 matrix b(x,y);

 b.input();

 cout<<"\n1.Add matrices\n2.Subtract matrices\n3.Multiplymatrices\n";

 cout<<"Select any one above , then hit ENTER"<<endl;

 cin>>x;

 if(x==1)

  (a+b).out();

 else if(x==2)

  (a-b).out();

 else if(x==3)

  (a*b).out();

 else

  cout<<"Wrong choice";

 cout<<"\nContinue(y/n)";

 cin>>ch;

 }while(ch=='y'||ch=='Y');

 cin.get();

 return 0;

}

 

 



| carlmack | SAKryukov:任何矩阵运算是可能的,全面实施好几种语言,包括C。是你的代码,你没有与此相关的任何意见发表。我希望我能理解你的要求,所以在这个问题的标题不准确制定。不过,我希望我回答的主要问题:这是可能的。任何疑虑{S0的}

mdash; SA

回答