第九周 任务四

/* 
实验内容:建立一个二维数组类Douary,使该类中有以下数据成员、成员函数及友员函数,完成矩阵的输入、输出、加、减、相等判断等操作。
* 程序的版权和版本声明部分  
* Copyright (c) 2011, 烟台大学计算机学院学生  
* All rights reserved.  
* 文件名称:矩阵的输入、输出、加、减、相等判断                            
* 作    者:  薛广晨                             
* 完成日期:  2012       年  4    月  15      日  
* 版 本号:  x1.0           
*/
//【任务4】建立一个二维数组类Douary,使该类中有以下数据成员、成员函数及友员函数,完成矩阵的输入、输出、加、减、相等判断等操作。
#include <iostream>

using namespace std;

class Douary
{
public:
	Douary(int m, int n);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为
	~Douary(); //析构函数:用于释放动态数组所占用的存储空间。
	friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组,其中d为Dousry类对象;
	friend ostream &operator<<(ostream &output, Douary &d);//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。
	friend Douary &operator+(const Douary &d1,const Douary &d2);//两个矩阵相加,规则:对应位置上的元素相加
	friend Douary &operator-(const Douary &d1,const Douary &d2);//两个矩阵相减,规则:对应位置上的元素相减
	bool operator==(const Douary &d);//判断两个矩阵是否相等,即对应位置上的所有元素是否相等
private:
	int ** Array;      //Array 为动态数组指针。
	int row;          //row  为二维数组的行数。
	int col;          //col   为二维数组的列数。
};

Douary :: Douary(int m, int n) //构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为;
{
    row = m;
    col = n;
    if(m != 0 && n!= 0)
    {
        Array = new int *[row];
        for (int i = 0; i < row; i++)
        {
            Array[i] = new int[col];
        }
    }
    else
        Array = NULL;
}

Douary :: ~Douary()
{
    if (Array != NULL)
    {
        for (int i = 0; i < row; i++)
        {
            delete Array[i];
        }
        delete[] Array;
        Array = NULL;
    }
}

istream &operator>>(istream &input, Douary &d)//重载运算符“>>”输入二维数组,其中d为Douary类对象;
{
    //input >> d.row >> d.col;
    if(d.row != 0 && d.col != 0)
    {
        int i,j;
        d.Array = new int *[d.row];
        for (i = 0; i < d.row; i++)
        {
            d.Array[i] = new int[d.col];
        }
        for (i = 0; i < d.row; ++i)
            for(j = 0; j < d.col; ++j)
            {
                input >> d.Array[i][j];
            }
            
    }
    else
        d.Array = NULL;
    return input;
}

ostream &operator<<(ostream &output, Douary &d)//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。
{
    output << d.row << '/' << d.col << endl; 
    if (d.row != 0 && d.col != 0)
    {
        int i,j;
        for(i = 0; i < d.row; ++i)
        {
            for (j = 0; j < d.col; ++j)
                output << d.Array[i][j] << " ";
            output << endl;
        }
    }
    return output;
}

Douary &operator +(const Douary &d1, const Douary &d2)//两个矩阵相加,规则:对应位置上的元素相加
{
    Douary *p = new Douary(0,0);
    if (d1.row == d2.row && d1.col == d2.col)
    {
        int i, j;
        p -> row = d2.row;
        p -> col = d2.col;
        p -> Array = new int *[d1.row];
        for (i = 0; i < d1.row; i++)
        {
            p -> Array[i] = new int[d1.col];
        }
        for (i = 0; i < d2.row; ++i)
            for(j = 0; j < d2.col; ++j)
            {
                p -> Array[i][j] = d1.Array[i][j] + d2.Array[i][j];
            }
    }
    else
    {
        cout << "不能构成矩阵!" << endl;
    }
    return *p;
}

Douary &operator-(const Douary &d1, const Douary &d2)//两个矩阵相减,规则:对应位置上的元素相减
{
    Douary *p = new Douary(0, 0);
    if (d1.row == d2.row && d1.col == d2.col)
    {
        int i, j;
        p -> row = d2.row;
        p -> col = d2.col;
        p -> Array = new int *[d1.row];
        for (i = 0; i < d1.row; i++)
        {
            p -> Array[i] = new int[d1.col];
        }
        for (i = 0; i < d2.row; ++i)
            for(j = 0; j < d2.col; ++j)
            {
                p -> Array[i][j] = d1.Array[i][j] - d2.Array[i][j];
            }
    }
    else
    {
        cout << "不能构成矩阵!" << endl;
    }
    return *p;
}

bool Douary::operator==(const Douary &d)//判断两个矩阵是否相等,即对应位置上的所有元素是否相等
{
    bool b = false;
    if (row == d.row && col == d.col)
    {
        int i,j;
        for(i = 0; i < row;++i)
            for(j = 0; j < col; ++j)
                if(d.Array[i][j] != Array[i][j])
                    return false;
        b = true;
    }
    return b;
}

int main()
{
	Douary d1(2, 3), d2(2, 3);

	cout << "输入d1:" << endl;
	cin >> d1;
	cout << "输入d2:"<<endl;
	cin >> d2;

	cout << "d1=" << endl << d1;
	cout << "d2=" << endl << d2;
	cout << "d1+d2=" << endl << (d1 + d2);
	cout << "d1-d2=" << endl << (d1 - d2);
	cout << "d1" << ((d1 == d2) ? "==" : "!=") << "d2" << endl;

	system("pause");
	return 0;
}

上机感言:这个题有难度,有挑战性,就是数组的地方比较难

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值