Homework 1165 - C++作业第十章(主讲老师徐红云)Problem D: C++第十章习题4

Problem D: C++第十章习题4

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 277   Solved: 168
[ Submit][ Status][ Web Board]

Description

在类模板T_Matrix中使用类模板T_Vector(向量运算类模板)实现矩阵的加、减运算,在main函数中使用类模板T_Matrix建立整型矩阵对象进行矩阵的加减运算。

Input

输入为若干组数据,每组数据用若干行表示,其中第1行为一个字符(+或-),表示接下来输入的两个矩阵所要做的运算,第2行为两个整数m和n,表示接下来输入的两个矩阵均为m*n的矩阵。随后若干行为两个n*m的矩阵。

Output

输出为若干组数据,每组数据用若干行表示,为一个m*n的矩阵,即运算结果。

Sample Input

+2 31 2 34 5 61 2 34 5 6-2 31 2 34 5 67 8 910 11 12

Sample Output

2 4 68 10 12-6 -6 -6-6 -6 -6



#include<iostream>
using namespace std;
template<typename T>
class vector
{
protected:
	T a[105][105];
	T d[105][105];
	int n,m;
public:
	vector(T c[][105],T h[][105],int x,int y):m(x),n(y)
	{
		int i,j;
		for(i=0;i<m;i++)
			for(j=0;j<n;j++)
	        {	
			   a[i][j]=c[i][j];
		       d[i][j]=h[i][j];
		    }
	}
	void add();
	void jian();
};
template<typename T>
void vector<T>::add()
{
	int i,j;
   for(i=0;i<m;i++)
	   for(j=0;j<n;j++)
	  {
		  a[i][j]+=d[i][j];
	   }
    for(i=0;i<m;i++)
	{
		cout<<a[i][0];
		for(j=1;j<n;j++)
		{
			//cout<<a[i][0];
			cout<<" "<<a[i][j];
		}
	       cout<<endl;
	}
}
template<typename T>
void vector<T>::jian()
{
	int i;
	int j;
   for(i=0;i<m;i++)
	   for(j=0;j<n;j++)
	  {
		  a[i][j]-=d[i][j];
	   }
    for(i=0;i<m;i++)
	{
		cout<<a[i][0];
		for(j=1;j<n;j++)
		{
			
			cout<<" "<<a[i][j];
		}
	       cout<<endl;
	}
}
int main()
{
	int i,j,m,n;
	int a[105][105],b[105][105];
	char k;
	while(cin>>k)
	{
		switch(k)
		{
		case '+':
			{
			cin>>m>>n;
			for(i=0;i<m;i++)
			for(j=0;j<n;j++)
	        cin>>a[i][j];
			for(i=0;i<m;i++)
			for(j=0;j<n;j++)
	        cin>>b[i][j];
			vector<int> p(a,b,m,n);
			p.add();
			break;
			}
		case '-':
			{
				cin>>m>>n;
				for(i=0;i<m;i++)
			for(j=0;j<n;j++)
	        cin>>a[i][j];
			for(i=0;i<m;i++)
			for(j=0;j<n;j++)
	        cin>>b[i][j];
			vector<int> p(a,b,m,n);
			p.jian();
			break;
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值