这里讲的矩阵有创建矩阵,矩阵加法,矩阵乘法,输出矩阵这些功能。

#include<iostream>

using namespace std;

template<class T>

class matrix       //矩阵节点

{

public:

matrix(int theRows = 0, int theColumns = 0);

matrix(const matrix<T>&);

~matrix() { delete[] element; };

int rows()const { return theRows; };

int cols()const { return theColumns; };

T& operator()(int i, int j)const;

matrix<T>& operator=(const matrix<T>&);

matrix<T> operator+()const;

matrix<T> operator+(const matrix<T>&)const;

matrix<T> operator-()const;

matrix<T> operator-(const matrix<T>&)const;

matrix<T> operator*(const matrix<T>&)const;

matrix<T>& operator+=(const T&);

void Input();

void Output();

private:

int theRows;

int theColumns;

T* element;             //元素

};

template<class T>

matrix<T>::matrix(int theRows, int theColumns)

{

if (theColumns < 0 || theRows < 0)

{

cout<<"Rows and Cols must be >= 0 ";

}

if ((theColumns == 0 || theRows == 0) && (theColumns != 0 || theRows != 0))

{

cout<<"Either both or neither rows and columns should be zero ";

}

this->theColumns = theColumns;

this->theRows = theRows;

element = new T[theColumns * theRows];

}

template<class T>

matrix<T>::matrix(const matrix<T>& m)

{

theColumns = m.theColumns;

theRows = m.theRows;

size_t number = theColumns * theRows;

element = new T[number];

copy(m.element, m.element + number, element);

}

template<class T>

matrix<T>& matrix<T>::operator=(const matrix<T>& m)

{

if (this != &m)

{

delete[] element;

theColumns = m.theColumns;

theRows = m.theRows;

size_t number = theColumns * theRows;

element = new T[number];

copy(m.element, m.element + number, element);

}

return *this;

}

template<class T>

T& matrix<T>::operator()(int i, int j) const

{

if (i<1 || i>theRows || j<1 || j>theColumns)

{

cout<<"Matrix Index Out Of Bounds ";

}

return element[(i - 1)*theColumns + j - 1];

}

//矩阵相加

template<class T>

matrix<T> matrix<T>::operator+(const matrix<T>& m)const

{

if (theRows != m.theRows || theColumns != m.theColumns)

{

cout<< "Matrix Size is Out of batch ";

}

matrix<T> w(theRows, theColumns);

size_t number = theColumns*theRows;

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

{

w.element[i] = element[i] + m.element[i];

}

return w;

}

//矩阵相乘

template<class T>

matrix<T> matrix<T>::operator*(const matrix<T>& m)const

{

if (theColumns != m.theRows || theRows != m.theColumns)

{

cout<<"Matrix Style is Out of batch ";

}

matrix<T> w(theRows, theColumns);

int ct = 0;

int cm = 0;

int cw = 0;

for (int i = 1; i <= theRows; i++)

{

for (int j = 1; j <= theColumns; j++)

{

T sum = element[ct] * m.element[cm];

for (int k = 2; k <= theColumns; k++)

{

ct++;

cm += m.theColumns;

sum += element[ct] * m.element[cm];

}

w.element[cw++] = sum;

ct -= theColumns - 1;

cm = j;

}

ct += theColumns;

cm = 0;

}

return w;

}

//输入矩阵

template<class T>

void matrix<T>::Input()

{

size_t number = theColumns * theRows;

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

{

cin >> element[i];

}

cout << "Input fanished" << endl;

return;

}

//输出矩阵

template<class T>

void matrix<T>::Output()

{

size_t number = theColumns * theRows;

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

{

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

{

cout << element[i*theColumns + j] << " ";

}

cout << endl;

}

cout << "Output finished " << endl;

return;

}

int main()

{

matrix<int> a(3, 3);

matrix<int> b(3, 3);

matrix<int> c(3, 3);

matrix<int> d(3, 3);

a.Input();

b.Input();

c= a * b;

d=a+b;

c.Output();

d.Output();

return 0;

}

c++矩阵的更多相关文章

  1. C语言 · 矩阵乘法 · 算法训练

    问题描述 输入两个矩阵,分别是m*s,s*n大小.输出两个矩阵相乘的结果. 输入格式 第一行,空格隔开的三个正整数m,s,n(均不超过200). 接下来m行,每行s个空格隔开的整数,表示矩阵A(i,j ...

  2. 获取Canvas当前坐标系矩阵

    前言 在我的另一篇博文 Canvas坐标系转换 中,我们知道了所有的平移缩放旋转操作都会影响到画布坐标系.那在我们对画布进行了一系列操作之后,怎么再知道当前矩阵数据状态呢. 具体代码 首先请看下面的一 ...

  3. CSharpGL(32)矩阵与四元数与角度旋转轴的相互转换

    CSharpGL(32)矩阵与四元数与角度旋转轴的相互转换 三维世界里的旋转(rotate),可以用一个3x3的矩阵描述:可以用(旋转角度float+旋转轴vec3)描述.数学家欧拉证明了这两种形式可 ...

  4. “为什么DirectX里表示三维坐标要建一个4*4的矩阵?”

    0x00 前言 首先要说明的是,本文的标题事实上来自于知乎上的一个同名问题:为什么directX里表示三维坐标要建一个4*4的矩阵? - 编程 .因此,正如Milo Yip大神所说的这个标题事实上是存 ...

  5. js实现蛇形矩阵

    参加腾讯前端实习生笔试,真的是被虐了千百遍,除了一条js程序题,其他半点前端都没有,都是考算法,计算机原理,数据结构.下面贴上腾讯笔试最后三大条中的一条,实现一个蛇形矩阵的输出.蛇形矩阵的什么样这里我 ...

  6. ACM 中 矩阵数据的预处理 && 求子矩阵元素和问题

            我们考虑一个$N\times M$的矩阵数据,若要对矩阵中的部分数据进行读取,比如求某个$a\times b$的子矩阵的元素和,通常我们可以想到$O(ab)$的遍历那个子矩阵,对它的各 ...

  7. PAT 1050. 螺旋矩阵(25)

    本题要求将给定的N个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵",是指从左上角第1个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为m行n列,满足条 ...

  8. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  9. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  10. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

随机推荐

  1. cms-详细页面2

    详细页面遗留下来的部分: 1:当前位置 2.分享 3.时间格式 4.摘要 5.关键字: 解决方案: 1:当前位置:---后台拼接 2:分享:前端一段js代码 3.摘要,直接数据库查询 4.时间格式:引 ...

  2. appuim操作webview控件

    1.操作webview控件,在uiautomator中如下图,能定位的只有最外层的内容.就一个webview控件,查找不到里面内容 1.使用driver.getContext(),获取是否是webvi ...

  3. POJ 3614 Sunscreen(贪心,区间单点匹配)

    把牛的SPF看作一个区间,防晒霜看作点.一个点可以匹配C[i]次,问最大匹配数.可以用图论做. 也可以贪心.贪心的思想是,把区间和点排序以后,考虑最左边的点,加入和这个点相交的区间, 并排除出界的区间 ...

  4. 【BZOJ4650】[NOI2016] 优秀的拆分(后缀数组)

    点此看题面 大致题意: 定义将一个字符串拆成\(AABB\)的形式为优秀拆分,求一个字符串所有子串的优秀拆分个数. 后缀数组 这题可是一道后缀数组黑题啊. 其实看完题解这题还是挺简单的. 大致思路 显 ...

  5. 2017.12.14 Java实现-----图书管理系统

    通过对图书的增删改查操作 用数组实现 Manager类 package demo55; import java.util.*; public class Manager { Scanner sc = ...

  6. Python 之继承

    概要 如果要修改现有类的行为,我们不必再从头建一个新的类,可以直接利用继承这一功能.下面将以实例相结合介绍继承的用法.   新建一个基类 代码如下: class Marvel(object): num ...

  7. CUDA实现数组倒序

    数组倒序,将在主机上初始化的数组传输到设备上,然后用CUDA并行倒序,此时在全局内存上操作,再将结果返回到主机并验证. #include <stdio.h> #include <as ...

  8. Linux学习记录(二)

    1.远程连接工具的使用 实际开发中,Linux服务器都在其他的地方,我们要通过远程的方式去连接Linux并操作它,Linux远程的操作工具有很多,企业中常用的有Puttty.secureCRT.SSH ...

  9. facebook的infer检测工具的安装

    缘由 由于公司产出代码的时候会使用静态扫描工具检测代码的质量,所以自己就想动手尝试一番infer整个的使用方式和使用效果,便动手安装了infer,结果安装过程中遇见太多的坑,导致很多时候都安装失败,这 ...

  10. 通过rsync+inotify实现数据实时备份

    rsync的优点与不足 与传统的cp,scp,tar,备份方式相比,rsync具有安全性高备份迅速支持增量备份的优点,可以满足对实时性要求不高的需求,例如定期备份文件服务器数据到远端服务器,但是,当数 ...