Matrix.hh

 class Matrix
{
int row;
int col;
int *p;
void copy(const Matrix &m);
void clearup();
public: Matrix();
Matrix(int x,int y);
~Matrix(); Matrix(const Matrix &m); const int getrows() const;
const int getcols() const; void setelem(int x,int y,int elem); const int getelem(int x,int y) const; Matrix & operator=(const Matrix &m);
Matrix & operator+=(const Matrix &m);
Matrix & operator-=(const Matrix &m);
Matrix & operator*=(const Matrix &m); const Matrix operator+(const Matrix &m) const;
const Matrix operator-(const Matrix &m) const;
const Matrix operator*(const Matrix &m) const; bool operator==(const Matrix &m) const;
bool operator!=(const Matrix &m) const; };

Matrix.cpp

 #include "Matrix.hh"
#include <cassert>
#include <iostream> using namespace std; Matrix::Matrix()
{
row=;
col=;
p=;
} Matrix::Matrix(int x,int y)
{
assert(x>=);
assert(y>=);
row=x;
col=y;
p=new int[row*col];
for(int i=;i<row*col;i++)
p[i]=;
} void Matrix::copy(const Matrix &m)
{
// row = m.row;
// col = m.row;
p = new int[row * col];
for(int i = ;i < row * col;i++)
p[i]=m.p[i];
} void Matrix::clearup()
{
delete[] p;
} Matrix::Matrix(const Matrix &m)
{
row = m.row;
col = m.col;
(*this).copy(m);
} Matrix::~Matrix()
{
(*this).clearup();
} const int Matrix::getrows() const
{
return row;
} const int Matrix::getcols() const
{
return col;
} const int Matrix::getelem(int x,int y) const
{
assert(x>=);
assert(y>=);
return p[x*col+y];
} void Matrix::setelem(int x,int y,int elem)
{
assert(x>=);
assert(y>=);
p[x*col+y]=elem;
} Matrix & Matrix:: operator=(const Matrix &m)
{
if(this!=&m)
{
clearup();
row = m.row;
col = m.col;
(*this).copy(m);
}
return *this;
} Matrix & Matrix:: operator+=(const Matrix &m)
{
assert(row==m.row);
assert(col==m.col);
for(int i=;i<row*col;i++)
p[i]+=m.p[i];
return *this;
} Matrix & Matrix:: operator-=(const Matrix &m)
{
assert(row==m.row);
assert(col==m.col);
for(int i=;i<row*col;i++)
p[i]-=m.p[i];
return *this;
} Matrix & Matrix:: operator*=(const Matrix &m)
{
// assert(col==m.row);
int *pp = new int[row * m.col];
int sum = ;
for(int i=;i<row;i++)
{
for(int j=;j<m.col;j++)
{
for(int k=;k<col;k++)
{
sum+=p[i * row + k] * m.p[k * m.row + j];
}
pp[i * m.col + j]=sum;
sum = ;
}
} delete[] p;
p = pp;
col = m.col;
return *this;
} const Matrix Matrix:: operator+(const Matrix &m) const
{
assert(row == m.row);
assert(col == m.col); Matrix other;
other = *this;
other += m;
return other;
} const Matrix Matrix:: operator-(const Matrix &m) const
{
assert(row == m.row);
assert(col == m.col); Matrix other;
other = *this;
other -= m;
return other;
} const Matrix Matrix:: operator*(const Matrix &m) const
{
assert(col==m.row);
Matrix other;
other = *this;
other *= m;
return other;;
} bool Matrix:: operator==(const Matrix &m) const
{
bool b = true;
// assert(row == m.row);
// assert(col == m.col);
if(row != m.row || col != m.col)
return false;
for(int i = ; i < row * col; i++)
{
if(p[i] != m.p[i])
{
b = false;
return b;
}
}
return b;
} bool Matrix:: operator!=(const Matrix &m) const
{
return !((*this)==m);
} /*
void Matrix:: add(Matrix &m)
{
assert( (row==m.row) || (col==m.col) );
for(int i=0;i<row*col;i++)
p[i]+=m.p[i];
} void Matrix::subtract(Matrix &m)
{
assert( (row==m.row) || (col==m.col) );
for(int i=0;i<row*col;i++)
p[i]-=m.p[i];
} bool Matrix::equals(Matrix &m)
{
bool b=true;
if( (row!=m.row) || (col!=m.col) )return false;
for(int i=0;i<row*col;i++)
if(p[i]!=m.p[i])
{ b=false;return b;}
return b;
}
*/

cs11_c++_lab3的更多相关文章

  1. cs11_c++_lab7

    wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...

  2. cs11_c++_lab6

    expressions.hh #ifndef EXPRESSIONS_HH #define EXPRESSIONS_HH #include "environment.hh" #in ...

  3. cs11_c++_lab5待修改

    heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...

  4. cs11_c++_lab4b

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  5. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  6. cs11_c++_lab2

    Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...

  7. cs11_c++_lab1

    lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...

随机推荐

  1. 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...

  2. build配置

     buildTypes {         debug {             // 显示Log             buildConfigField "boolean", ...

  3. windows脚本调用批处理

    set ws=wscript.createobject(“wscript.shell”)ws.run “batchfn.bat /start”,0 save as xxx.vbe file.

  4. ASP.NET MVC图片上传

    工具:ssi-uploader $('#id').ssi_uploader({ url: 'path' }); public ActionResult path() { HttpPostedFileB ...

  5. ubuntu12.04+Elasticsearch2.3.3伪分布式配置,集群状态分片调整

    目录 [TOC] 1.什么是Elashticsearch 1.1 Elashticsearch介绍 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.能够快速搜索数 ...

  6. MYSQL 判断一个时间段是否在另一个时间段内。

    [1 CREATE TABLE #B 2 ( 3 MeetingRoom int, 4 BeginTime datetime, 5 EndTime datetime6 ) 7 insert into ...

  7. Head First设计模式之装饰者模式(Decorator Pattern)

    前言: 本节将深度讨论继承滥用问题,将会学到使用对象组合的方式,在运行时装饰类,在不修改任何底层代码的情况下,给对象赋予新的职责. 1.    基本需求:咖啡连锁店业务扩张需要重新设计订单系统 背景: ...

  8. javaweb页面上展示动态图片

    HTML <img alt="点击设定" name="CONSTRUCTIONPLANHIS_IMAGE_curr_img_0" src="vi ...

  9. Spring Security 3整合CAS 实现SSO

    spring security 3整合cas client用于实现各Application之间的单点登录. 1. 需要准备的jar spring-security-core-3.0.8.RELEASE ...

  10. 学习taobao框架

    参考:https://github.com/xulingbo/xulingbo.github.io/issues http://blog.csdn.net/hguisu/article/details ...