矩阵相乘·(离散数学我再也不怕啦

直接上代码

#include<iostream>
using namespace std;
int n;
class node
{
	public:
	int data[20][20];
	int size;
	node() //无参构造 
	{
		size=0;
	}
	node(const node & a)  //带参构造 
	{
		this->size=a.size;
		int limit=a.size;
		for(int i=0;i<limit;i++)
			for(int j=0;j<limit;j++)
				(this->data)[i][j]=(a.data)[i][j];
	}
	friend ostream& operator <<(ostream&,const node&);
	friend istream& operator >>(istream&,const node&);
	friend node operator * (const node&,const node&);
};
node operator * (const node&a,const node&b)
{
	node res;
	res.size=n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			int sum=0;
			for(int t=0;t<n;t++)
				sum+=(a.data)[i][t]*(b.data)[t][j];
			(res.data)[i][j]=sum;
		}
	}
	return res;
}
ostream& operator <<(ostream& o,const node& x)
{
	int limit=x.size;
	for(int i=0;i<limit;i++)
	{
		for(int j=0;j<limit;j++)
			o<<(x.data)[i][j]<<' ';
		cout<<endl;
	}	
	return o;
}
istream& operator >>(istream& o,node& t)
{
	cout<<"n=";
	o>>n;
	t.size=n;
	cout<<"请输入n*n矩阵"<<endl;
	for(int i=0;i<t.size;i++)
		for(int j=0;j<t.size;j++)
		{
			int x;
			o>>x;
			(t.data)[i][j]=x;
		}
	return o;
}
int main()
{
	int choice;
	cout<<"Choice 1:a*b(a,b为矩阵)\nChoice 2:a^b(a为矩阵,b为int数)\n";
	cin>>choice;
	switch(choice)
	{
		case 1:
		{
			node a;
			node b;
			cin>>a;
			cout<<"-------------"<<endl;
			cin>>b;
			cout<<"-------------"<<endl; 
			cout<<a*b;
			break;
		}
		case 2:
		{
			node a;
			int b;
			cin>>a;
			cout<<"-------------\nb="; 
			cin>>b;
			node temp=a;
			for(int i=1;i<=b;i++)
			{
				cout<<"a^"<<i<<endl;
				cout<<temp;
				temp=temp*a;
			}
		}
	}
}
/*
1 2 0 0
0 0 1 0
1 0 0 1
0 0 1 0
*/
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值