c++-关于稀疏矩阵的问题(#include "source"是要自己写的地方)

问题描述

关于稀疏矩阵的问题(#include "source"是要自己写的地方)

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.
EXAMPLE INPUT
1000000 1000000

1 1 10
1 1000000 50
1000000 1000000 20

1 1000000 30
1000000 1 40
1 1 -10
EXAMPLE OUTPUT
(1,1000000,80)
(1000000,1,40)
(1000000,1000000,20)

主程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 "source"

#include
using namespace std;

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);

}

解决方案


#include <iostream>
using namespace std;
#define MAXSIZE 10
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;
}; 

class Sparse :public Matrix
{
private:
    int _row;
    int _column;
    Entry _data[MAXSIZE];
    int _h = 0;
public:
    int length(){ return _h; }
    Sparse(int row, int column)
    {
        _row = row;
        _column = column;
    }
    virtual int size(int dimension) const
    {
        if (dimension == 0)
            return _row;
        else if (dimension == 1)
            return _column;
        else
            return 1;
    }
    virtual void set(int row, int column, double value)
    {
        bool done = false;
        for (int i = 0; i < _h; i++)
        {
            if (_data[i].row == row && _data[i].column == column)
            {
                _data[i].value = value;
                done = true;
                break;
            }
        }
        if (!done)
        {
            if (_h < MAXSIZE)
            {
                _data[_h].row = row;
                _data[_h].column = column;
                _data[_h].value = value;
                _h++;
            }
            else
                throw;
        }
    }
    virtual double get(int row, int column) const
    {
        for (int i = 0; i < _h; i++)
        {
            if (_data[i].row == row && _data[i].column == column)
            {
                return _data[i].value;
            }
        }
        return 0; }
    virtual void print()
    {
        for (int i = 0; i < _h; i++)
        {
            cout << "(" << _data[i].row << "," << _data[i].column << "," << _data[i].value << ")" << endl;
        }
    }
    Sparse operator +(Sparse another)
    {
        for (int i = 0; i < another._h; i++)
        {
            double v = get(another._data[i].row, another._data[i].column);
            set(another._data[i].row, another._data[i].column, another._data[i].value + v);
        }
        return *this;
    }
};

解决方案二:

size函数是用来返回Dimension的 如果dimension =1就返回rows dimension =2就返回columns

时间: 2024-09-04 23:31:57

c++-关于稀疏矩阵的问题(#include &amp;quot;source&amp;quot;是要自己写的地方)的相关文章

jsp truts-&amp;amp;lt;div class=&amp;amp;quot;error_mes&amp;amp;quot;&amp;amp;gt;${message}&amp;amp;lt;/div&amp;amp;gt;

问题描述 <div class="error_mes">${message}</div> ${message}什么意思?${message}通常在哪实现? 解决方案 ${message}是java中el表达式 可以获取域中的值 四大作用域 application request session pageContext 建议百度看下el表达式就ok了 解决方案二: jsp中的EL表达式,具体看下这里面.http://blog.csdn.net/chinacshar

ajax-兄弟伙我的OnSuccess=&amp;amp;quot;afterLogin&amp;amp;quot;咋调用不起

问题描述 兄弟伙我的OnSuccess="afterLogin"咋调用不起 兄弟伙我的OnSuccess="afterLogin"咋调用不起,直接返回return Content类容到页面 @{ Layout = null; } <!DOCTYPE html> 网上超市管理系统 </p> <pre><code> //就是执行controller方法以后执行的方法 function afterLogin(data) {

safari-MacBook的Safari下type=&amp;amp;quot;submit&amp;amp;quot;点击后显示不出div

问题描述 MacBook的Safari下type="submit"点击后显示不出div 如题,按钮点击的时候在window浏览器这个红色背景的div会一闪而过,但是在macbook的Safari中什么效果都没有,return false的时候div可以显示出来,但表单还怎么提交啊,return true时页面中div已经加载变成block了,感觉是还没来得及被浏览器渲染页面就刷新了一下,有没有什么办法可以让它有一闪而过的效果? html: <div id="divTes

jquery mobile中使用data-role=&amp;amp;quot;dialog&amp;amp;quot;弹出对话框的问题

问题描述 jquery mobile中使用data-role="dialog"弹出对话框的问题 如图,当页面除了一个"page"和"dialog"还有其它的 容器时,对话框后的背景就会显示没有样式的这个容器内容, 这是为什么呢,怎样才能让背景中不显示任何东西 解决方案 试试把背景内容放到另一个page中 你说的data-role =dialog 我没注意到 这个属性... 另外 可以看看 . data-role=popup http://www.

java web-&amp;amp;lt;c:forEach items=&amp;amp;quot;${salesProducts }&amp;amp;quot; var=&amp;amp;quot;pro&amp;amp;quot;&amp;amp;gt;

问题描述 <c:forEach items="${salesProducts }" var="pro"> ${pro.goodsName}特价:¥${pro}/c:forEach pro的bean类属性都设了getter和setter方法,但是${pro.goodsName}没值,${pro}却有值 解决方案 检查一下goodsName属性名称有没写错. 解决方案二: private String goodsName;public String getG

html里面 &amp;amp;lt;!--include file=&amp;amp;quot;XXX&amp;amp;quot;--&amp;amp;gt;显示不正确,什么原因?

问题描述 为什么我在html文件里面使用<!--includefile="file1.htm"-->不能正确显示file1.htm的内容?说明:我的操作系统是xpsp2,使用ie6调用file1.htm的文件(default.htm)和file1.htm放在同一个目录下面default.htm内容如下:<html><head></html><body>ThisisDefault.htm<!--includefile=&q

#include&amp;lt;file.h&amp;gt;与#include&amp;quot;file.h&amp;quot;的区别

对于这种问题先来看一看外国人怎么想的,因为本来这种语言就是他们创造的,http://www.geekinterview.com/question_details/3379     What is the difference between #include <file> and #include "file"? When writing your C program, you can include files in two ways. The first way is

virtual-语法错误 : 缺少&amp;amp;quot;;&amp;amp;quot;(在标识符&amp;amp;quot;m_Graydlg&amp;amp;quot;的前面)

问题描述 语法错误 : 缺少";"(在标识符"m_Graydlg"的前面) public:CGaryDetection m_Graydlg; 但是我已经包含了头文件啊 #pragma once #include""afxdockablepane.h""#include""GrayDetection.h"" // CGrayDetectionPane class CGrayDetectio

char a = &amp;amp;quot;C&amp;amp;quot;;printf(&amp;amp;quot;%c&amp;amp;quot;,a);打印问题

问题描述 char a = "C";printf("%c",a):打印问题 我用VS2013运行这个代码,老打印 T,将a初始化为"X",X代表其他字符,还是打印 T,这是怎么回事? 解决方案 char a = "C",这里"C"不是字符常量,它表示的是两个字符(字符C和)组成的字符串,"C"实际上表示的是字符串所在的内存地址,上面那条语句试图将一个内存地址赋给a,内存地址占4个字节,而