稀疏矩阵,用vector,实现抽象类
来源:互联网 发布:手机淘宝首页装修布局 编辑:程序博客网 时间:2024/05/16 03:22
Matrix16
Design a class Sparse that implements interface Matrix: Sparse should has the following public object functions in addition:
- A constructor Sparse(int rows, int column), which initializes all elements in the matrix to 0's.
- A function Sparse Sparse::operator + (Sparse & sparse2), which returns the pair-wise sum of two sparse matrixes.
1000000 10000001 1 101 1000000 501000000 1000000 201 1000000 301000000 1 401 1 -10EXAMPLE OUTPUT
(1,1000000,80)(1000000,1,40)(1000000,1000000,20)
class Entry { public: int row; int column; double value; }; class Matrix { public: virtual int size(int dimension) const = 0; virtual void set(int row, int column, double value) = 0; virtual double get(int row, int column) const = 0; virtual void print() = 0; }; #include<vector>#include<algorithm> #include <iostream> using namespace std; bool Comp1(const Entry &a,const Entry &b){return a.row<b.row;}bool Comp2(const Entry &a,const Entry &b){return (a.row==b.row&&a.column<b.column);}class Sparse : public Matrix{private:int _rows, _columns;vector<Entry> entry;public:Sparse(int rows, int column){_rows = rows;_columns = column;entry = vector<Entry>();}int size(int dimension) const{if(dimension == 1) return _rows;if(dimension == 2) return _columns;}void set(int row, int column, double value){Entry e;e.row = row;e.column = column;e.value = value;entry.push_back(e);}double get(int row, int column) const{for(int i=0; i < entry.size();i++){if(entry[i].row == row && entry[i].column == column ){return entry[i].value;}}}void print(){for(int i=0; i < entry.size();i++){cout<<"("<<entry[i].row<<","<<entry[i].column<<","<<entry[i].value<<")\n";}}Sparse operator + (Sparse & sparse2){Sparse s(_rows, _columns);for(int i=0; i < this->entry.size(); i++){for(int j = 0 ;j< sparse2.entry.size(); j++){if(this->entry[i].row == sparse2.entry[j].row && this->entry[i].column == sparse2.entry[j].column){Entry e;e.row = sparse2.entry[j].row;e.column = sparse2.entry[j].column;e.value = this->entry[i].value + sparse2.entry[j].value;if(e.value)s.entry.push_back(e);this->entry[i].value = 0;sparse2.entry[j].value = 0;}}}for(int i=0; i < this->entry.size(); i++){Entry e;e.row = this->entry[i].row;e.column = this->entry[i].column;e.value = this->entry[i].value;if(e.value)s.entry.push_back(e);}for(int i=0; i < sparse2.entry.size(); i++){Entry e;e.row = sparse2.entry[i].row;e.column = sparse2.entry[i].column;e.value = sparse2.entry[i].value;if(e.value)s.entry.push_back(e);}sort(s.entry.begin(),s.entry.end(),Comp1);sort(s.entry.begin(),s.entry.end(),Comp2);return s;}}; void print(Matrix & matrix) { matrix.print(); } void readAndSetElement(Matrix & matrix) { int row; int column; double value; cin >> row >> column >> value; matrix.set(row, column, value); } void readAndSetMultipleElements(Matrix & matrix, int count) { for (int i = 0; i < count; ++ i) { readAndSetElement(matrix); } } int main() { int rows; int columns; cin >> rows >> columns; Sparse sparse1(rows, columns); readAndSetMultipleElements(sparse1, 3); Sparse sparse2(rows, columns); readAndSetMultipleElements(sparse2, 3); Sparse sparse3 = sparse1 + sparse2; print(sparse3); }
0 0
- 稀疏矩阵,用vector,实现抽象类
- 稀疏矩阵的实现
- 稀疏矩阵及其实现
- 简单的稀疏矩阵实现
- hadoop实现稀疏矩阵乘法
- 稀疏矩阵的C++实现
- 顺序表实现稀疏矩阵
- 稀疏矩阵之python实现
- 稀疏矩阵之python实现
- 用三元组存储稀疏矩阵并实现转置
- 三元组稀疏矩阵类
- virtualAlloc/VirtualFree()实现一个大稀疏矩阵
- 稀疏矩阵实现算法(部分)
- Python 实现简单的稀疏矩阵
- 4.3稀疏矩阵的运算与实现
- 数据结构之---C语言实现稀疏矩阵
- 实现稀疏矩阵相乘C/C++
- C++实现稀疏矩阵的压缩存储
- C++ STL编程轻松入门(1)
- js中confirm()函数的用法
- android 屏幕适配
- SGU 125 Shtirlits(dfs)
- 用栈判断表达式中的括号是否匹配
- 稀疏矩阵,用vector,实现抽象类
- 个人对webdriver的理解(二)
- 小马哥---高仿苹果6 主板型号z13刷机拆机主板图 6572芯片nand字库
- lua next 用法
- mysql null 值相关规则
- SecureCRT vi彩色显示关键字
- 大数据工程师技能图谱
- canvas-转盘
- Scrapy 爬取 豆瓣电影的短评