/* I have a problem with my class matran, when I make "(A+B)*(D+E)" it's no result */
/* help me to correct it */
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
using namespace std;

class matran
{
    int n,m;
    double A[20][20];
    public:
    void nhap();
    void in(matran);
    matran operator+(matran);
    matran operator-(matran);
    matran operator*(matran);   
};
////////////////////////////////////////////////////////
void matran::nhap()
{
    char s;
    cout<<"\nten ma tran:";
    cin>>s;
    cout<<"nhap vao kich thuoc ma tran bac m*n\n";
    cout<<"m=";
    cin>>m;
    while(m<=0)
    {
        cout<<"\nnhap lai m";
        cin>>m;
    }
    cout<<"\nn=";
    cin>>n;
    while(n<=0)
    {
        cout<<"\nnhap lai n";
        cin>>n;
    }
    cout<<"\nnhap vao cac phan tu cua ma tran" <<s<<"[m*n]\n";
    //nhap theo hang ngang
    for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++)
    {
        cout<<s<<"["<<i<<","<<j<<"]=";
        cin>>A[i][j];
    }
}
///////////////////////////////////////////////////////////
void matran::in(matran)
{
    cout<<"in ra ma tran: \n";
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        cout<<A[i][j]<<setw(6);
        cout<<"\n";
    }
}
/////////////////////////////////////////////////////////// 
matran matran::operator+(matran B)
{
    matran C;
    if(this->m!=B.m||this->n!=B.n)
    cout<<"\nkhong thuc hien duoc phep cong hai ma tran nay\n";
    else
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {   
            C.A[i][j]=this->A[i][j]+B.A[i][j];
            cout<<C.A[i][j]<<setw(6);
        }
        cout<<"\n";       
    }
    return C;
}
//////////////////////////////////////////////////////////
matran matran::operator-(matran B)
{
    matran C;
    if(this->m!=B.m||this->n!=B.n)
    cout<<"\nkhong thuc hien duoc phep tru hai ma tran nay\n";
    else
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            C.A[i][j]=this->A[i][j]-B.A[i][j];
            cout<<C.A[i][j]<<setw(6);
        }
        cout<<"\n";
    }
    return C;
}
//////////////////////////////////////////////////////////
matran matran::operator*(matran B)
{
    matran C;
    if(this->n!=B.m)
    cout<<"\nkhong thuc hien duoc phep nhan hai ma tran nay";
    else
    {
        cout<<"\nket qua phep nhan hai ma tran:\n";
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                    C.A[i][j]=0;
                for(int k=1;k<=m;k++)
                    C.A[i][j]+=this->A[i][k]*B.A[k][j];
                cout<<C.A[i][j]<<setw(6);
            }
            cout<<"\n";
        }
        return C;
    }
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void main()
{
        matran A, B, C, D, E;
        cout<<"nhap vao ma tran thu nhat \n";
        A.nhap();
        A.in(A);
        cout<<"nhap vao ma tran thu hai \n";
        B.nhap();
        B.in(B);
        cout<<"nhap vao ma tran thu ba \n";
        D.nhap();
        D.in(D);
        cout<<"nhap vao ma tran thu tu \n";
        E.nhap();
        E.in(E);
        cout<<"\nket qua cua phep tinh sau: (A+B)*(D+E) \n";
        C=(D+E)+B;

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use code tags when you post code. Please tell us an example input, the expected output, the current output, and any compiler errors. Also, please use English - unfortunately most readers of this forum would have no idea what that code is supposed to be doing.

David

This article has been dead for over six months. Start a new discussion instead.