当前位置:文档之家› 云南大学面向对象程序设计实验实验报告8

云南大学面向对象程序设计实验实验报告8

云南大学软件学院实验课程报告

Course Report

School of Software, Yunnan University

个人成绩

学期: 2013—2014学年秋季学期

课程名称: 面向对象程序设计实验

任课教师: 郁湧

实验题目: lab8

小组长: 李云聪

联系电话: 187******** 电子邮件: 294265421@https://www.doczj.com/doc/711503485.html, 完成提交时间:2013年 11 月 20 日

说明:本实验在实验六的基础上实现

一实验目的

构造一个矩阵matrix,里面包括构造器和相关成员函数,并能对输入的两个矩阵在判断两矩阵能否进行加减乘的基础上进行加减乘运算。

二具体设计

定义变量line,col为矩阵的行数,列数,elems为用于存储矩阵的元素的数组。有三个构造函数matrix(),matrix (int,int),Matrix(const Matrix &m)。关于取和设定这些值的成员函数很常规,就不多说。还需要说的就是,这个类设计中重载了加减乘数乘转置赋值运算符,用的是友元的方式。

三实现源码

matrix.h

#pragma once

#ifndef MATRIX_H

#define MATRIX_H

#include

using namespace std;

class Matrix

{

private :

int line;

int col;

int *elems;

public :

Matrix();

Matrix(int l,int c);

Matrix(const Matrix &m);

void setLine(int l);

void setCol(int c);

void setElems();

int getLine() const;

int getCol() const;

void print() const ;

Matrix& operator= (const Matrix &m); //重载赋值

Matrix operator~ () const; //重载转置

friend Matrix operator+(const Matrix &a, const Matrix &b); //重载加号friend Matrix operator-(const Matrix &a, const Matrix &b); //重载减号friend Matrix operator*(const Matrix &a, const Matrix &b);//重载乘号friend Matrix operator&(const int a, const Matrix &b);//重载数乘

~Matrix();

};

#endif

matrix.cpp

#include"matrix.h"

#include

Matrix::Matrix(){}

Matrix::Matrix(int l,int c)

{

setLine(l);

setCol(c);

elems = new int[col*line];

}

Matrix::Matrix(const Matrix &m)

{

line = m.line;

col = m.col;

elems = new int[line * col];

for(int i = 0;i < line * col;i++)

elems[i]= m.elems[i];

}

void Matrix::setLine(int l)

{

line = l;

}

void Matrix::setCol(int c)

{

col = c;

}

void Matrix::setElems()

{

int i,size = col*line;

for(i = 0;i < size; i++ )

{

cin>>elems[i];

}

}

int Matrix::getLine() const

{

return line;

}

int Matrix::getCol() const

{

return col;

}

void Matrix::print() const

{

for(int i = 0; i < line*col;i++)

{

if((i+1)%(col)==0)

{

cout<

}

else

cout<

}

}

Matrix& Matrix::operator=(const Matrix &m)

{

setLine(m.line);

setCol(m.col);

delete []elems;

elems = new int[line * col];

for(int i = 0;i < line * col;i++)

{

elems[i] = m.elems[i];

}

return *this;

}

Matrix Matrix::operator~() const

{

Matrix t(col,line);

for(int i = 0; i < line;i++)

for(int j = 0; j < col; j++)

t.elems[line*j + i]=elems[col*i + j];

return t;

}

Matrix operator*(const Matrix &a,const Matrix &b)

{

if(a.col != b.line )

{

cerr << "第一个矩阵的列数和第二个矩阵的行数不相同!" << endl;

exit(EXIT_FAILURE);

}

int line = a.line,col = b.col;

int i,j,k;

Matrix temp(line,col);

for(i = 0;i < line * col;i++)

temp.elems[i] = 0;

for(i = 0;i < line;i++)

for(k = 0;k < col;k++)

for(j = 0;j < b.line;j++)

temp.elems[i * col + k] += a.elems[i * col + j] * b.elems[j *

col + k];

return temp;

}

Matrix operator-(const Matrix &a, const Matrix &b)

{

if(a.line != b.line || a.col != b.col)

{

cerr << "两矩阵的行列数不相同!" << endl;

exit(EXIT_FAILURE);

}

Matrix c(a.line,a.col);

for(int i = 0;i < a.line * a.col;i++)

c.elems[i] = a.elems[i] - b.elems[i];

return c;

}

Matrix operator+(const Matrix &a, const Matrix &b)

{

if(a.line != b.line || a.col != b.col)

{

cerr << "两矩阵的行列数不相同!" << endl;

exit(EXIT_FAILURE);

}

Matrix c(a.line,a.col);

for(int i = 0;i < a.line * a.col;i++)

c.elems[i] = a.elems[i] + b.elems[i];

return c;

}

Matrix operator&(const int a, const Matrix &b)

{

Matrix c(b.line,b.col);

for(int i = 0;i < b.line * b.col;i++)

c.elems[i] = a * b.elems[i];

return c;

}

Matrix::~Matrix()

{

delete [] elems;

}

主函数main.cpp

#include

#include"matrix.h"

int main()

{

int row1, col1, row2, col2;

int f = 5;

cout << "请输入矩阵A的行数:" << endl;

cin >> row1;

cout << "请输入矩阵A的列数:" << endl;

cin >> col1;

Matrix a(row1,col1);

cout << "请以行序优先输入第一个矩阵的元素:" << endl;

a.setElems();

a.print();

cout << "请输入矩阵B的行数:" << endl;

cin >> row2;

cout << "请输入矩阵B的列数:" << endl;

cin >> col2;

Matrix b(row2,col2);

cout << "请以行序优先输入第二个矩阵的元素:" << endl;

b.setElems();

b.print();

Matrix c(row1,col2);

cout << "两矩阵的和为:" << endl;

c = a + b;

c.print();

cout << "两矩阵的差为:" << endl;

c = a - b;

c.print();

cout << "两矩阵的乘积为:" << endl;

c = a * b;

c.print();

cout << f << "数乘矩阵b为:" << endl;

c = f & b;

c.print();

cout << "\n矩阵b转置矩阵为:" << endl;

(~b).print();

return 0;

}

四实验结果

相关主题
文本预览
相关文档 最新文档