*矩阵操作

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

 

#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();  
}; 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值