*矩阵操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Fiverya/article/details/88863163

编写一个程序用于对矩阵进行操作。假定有一个实数矩阵,需要对它进行加法、减法和乘法运算,并且重载运算符(),用来返回矩阵元素的值。最后输出运算结果。

#include<iostream>  
using namespace std;  
class Juzhen  
{   int x,y;  
    int c[10][10];   
    public:  
    Juzhen(){}  
    void Get1(){cin>>x>>y;}  
    void Get2()  
    {   for(int i=0;i<x;i++)  
        for(int j=0;j<y;j++)  
        cin>>c[i][j];  
    }  
    void show()  
    {for(int i=0;i<x;i++)  
        {  
            for(int j=0;j<y;j++)  
            cout<<c[i][j]<<" ";  
            cout<<endl;  
        }  
    }  
    friend Juzhen operator+(Juzhen &j1,Juzhen &j2);    
    friend Juzhen operator-(Juzhen &j1,Juzhen &j2);    
    friend Juzhen operator*(Juzhen &j1,Juzhen &j2);   
};   
Juzhen operator+(Juzhen &j1,Juzhen &j2)  
{  
    int i,j;  
    for(i=0;i<j1.x;i++)  
    for(j=0;j<j1.y;j++)  
    j1.c[i][j]=j1.c[i][j]+j2.c[i][j];  
    return j1;  
}  
Juzhen operator-(Juzhen &j1,Juzhen &j2)  
{  
    int i,j;  
    for(i=0;i<j1.x;i++)  
    for(j=0;j<j1.y;j++)  
    j1.c[i][j]=j1.c[i][j]-j2.c[i][j];  
    return j1;  
}  
Juzhen operator*(Juzhen &j1,Juzhen &j2)  
{  
    int i,j;  
    for(i=0;i<j1.x;i++)  
    for(j=0;j<j1.y;j++)  
    j1.c[i][j]=j1.c[i][j]*j2.c[i][j];  
    return j1;  
}  
int main()  
{  
    Juzhen j1,j2,j3;  
    j1.Get1();  
    j2.Get1();  
    j3.Get1();  
    j1.Get2();  
    j2.Get2();  
    j3=j1+j2;  
    cout<<"c=a+b"<<endl;  
    j3.show();  
    j3=j1-j2;  
    j3=j1-j2;  
    cout<<"c=a-b"<<endl;  
    j3.show();  
    j3=j1+j2;  
    j3=j1*j2;  
    cout<<"c=a*b"<<endl;  
    j3.show();  
}; 

猜你喜欢

转载自blog.csdn.net/Fiverya/article/details/88863163