Recent Posts

Thứ Hai, 15 tháng 8, 2011

Complex Matrix


Trong điện tử - viễn thông, số phức là một công cụ khá quan trọng. Hãy viết chương trình thực hiện phép nhân hai ma trận phức với nhau.

#include<iostream>
#include<complex>// Sử dụng thư viện "#include<complex>" có sẵn.//không cần phải viết 1 class số phức mới.
using namespace std;

// Hàm nhập vào ma trận phức
void  create_complex_matrix(complex<double>**&A,int rows,int cols)
{
                static int k=1;
                A=new complex<double>*[rows];
                for(int i=0;i<rows;i++)
                {
                                A[i]=new complex<double>[cols];
                                for(int j=0;j<cols;j++)
                                {
                                                double re,im;//Phần thực và phần ảo của số phức
                                                cout<<"Nhap vao phan thuc:A"<<k<<"["<<i<<","<<j<<"]=";cin>>re;
                                                cout<<"Nhap vao phan ao:A"<<k<<"["<<i<<","<<j<<"]=";cin>>im;
                                                A[i][j]=complex<double>(re,im); //Tạo một số phức có real=re, imagine=im sau đó gán cho A[i][j]//
                                }
                }
                k++;
}

// Hàm nhân 2 ma trận phức
void  multiply_complex_matrix(complex<double>**&A,int rows1,int cols1,complex<double>**&B,int rows2,int cols2)
{
                if(cols1!=rows2) exit(0); //Nếu số cột của ma trận 1 khác số hàng của ma trận 2 thì thoát
                else
                {
                                // Tạo ma trận phức C[rows1][cols2]
                                complex<double>**C;
                                C=new complex<double>*[rows1];
                                for(int k=0;k<cols2;k++)
                                {
                                                C[k]=new complex<double>[cols2];
                                }
                            //Thực hiện nhân 2 ma trận A,B
                                for(int i=0;i<rows1;i++)
                                                for(int j=0;j<cols2;j++)
                                                {
                                                                C[i][j]=complex<double>(0,0);
                                                                for(int k1=0;k1<rows2;k1++)
                                                                {
                                                                                C[i][j]+=A[i][k1]*B[k1][j];
                                                                }
                                                }
                cout<<"In ra ma tran C<Tich cua 2 ma tran A va B>:"<<endl;
                for(int i=0;i<rows1;i++)
                {
                                for(int j=0;j<cols2;j++)
                                {
                                                cout<<"      "<<C[i][j]<<" ";
                                                if(j==rows1-1)
                                                {
                                                                cout<<"\n";
                                                                cout<<"\n";
                                                }
                                }
                }
                }

}
// Hàm main()
int _tmain(int argc, _TCHAR* argv[])
{
                complex<double>**A,**B; //Khai báo 2 con trỏ mảng 2 chiều A,B
                int rows1,cols1,rows2,cols2; //Số hàng và số cột của 2 ma trận A,B

                cout<<"Nhap vao so hang cua ma tran thu nhat:";cin>>rows1;
                cout<<"Nhap vao so cot cua ma tran thu nhat:";cin>>cols1;
                cout<<"Nhap vao ma tran thu nhat:"<<endl;
                create_complex_matrix(A,rows1,cols1);
                cout<<"Nhap vao so hang cua ma tran thu 2:";cin>>rows2;
                cout<<"Nhap vao so cot cua ma tran thu 2:";cin>>cols2;

                cout<<"Nhap vao ma tran thu 2:"<<endl;
                create_complex_matrix(B,rows2,cols2);
                multiply_complex_matrix(A,rows1,cols1,B,rows2,cols2);

                system("pause");
                return 0;
}
///Kết quả chạy thử với ma trận A[2][2], B[2][2]

Không có nhận xét nào:

Đăng nhận xét