-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse
292 lines (283 loc) · 6.29 KB
/
sparse
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdio.h>
#include<stdlib.h>
using namespace std;
struct Element
{
int i;
int j;
int x;
}
struct Sparse
{
int row;
int col;
int num;
struct Element *e;
}
void create(struct Sparse *s)
{
int i, j,k, x;
printf("Enter the dimensions\n");
scanf("%d%d", &i, &j);
s->row = i;
s->col = j;
printf("Enter the num of elements\n");
scanf("%d", &k);
s->num = k;
s->e = (struct Element*) malloc(sizeof(struct Element) * k);
printf("Enter the Sparse Matrix\n");
for ( k = 0; k < s->num; k ++) {
scanf("%d%d%d", &i,&j,&x);
s->e[k].i = i;
s->e[k].j = j;
s->e[k].x = x;
}
}
void display(struct Sparse s)
{
int i;
int j;
int k;
for ( i = 0; i < s.row; i ++) {
for ( j = 0 ; j < s.col; j++) {
for ( int k = 0; k < s.num; k++) {
if ( i == s.e[k].i && j == s.e[k].j ) {
printf("%d ", s.e[k].x);
}else {
printf("0 ");
}
}
}
printf("\n");
}
}
struct Sparse * add(struct Sparse *s1, struct Sparse *s2)
{
if ( s1->row != s2->row || s1->col != s2->col ) return NULL;
struct Sparse *sum ;
sum = (struct Sparse *) malloc(sizeof(struct Sparse));
sum->row = s1->row;
sum->col = s1->col;
sum->e = new Element[s1->num + s2->num];
// or sum->e = (struct Element *) malloc(sizeof(struct Element) * (s1->num + s2->num));
int i = 0; // trace s1
int j = 0; // trace s2
int k = 0;
while ( i < s1->num && j < s2->num) {
if ( s1->e[i].i < s2->e[j].i ) {
sum->e[k++] = s1->e[i++]; // copy s1 to sum
} else if ( s2->e[j].i < s1->e[i].i ) {
sum->e[k++] = s2->e[j++]; // copy s2 to sum
} else {
if ( s1->e[i].j < s2->e[j].j ) {
sum->e[k++] = s1->e[i++]; // copy s1 to sum
} else if ( s2->e[j].j < s1->e[i].j ) {
sum->e[k++] = s2->e[j++]; // copy s2 to sum
} else {
sum->e[k] = s1->e[i++];
sum->e[k++].x += s2->e[j++].x;
}
}
}
while ( i < s1->num ) {
sum->e[k++] = s1->e[i++];
}
while ( j < s2->num) {
sum->e[k++] = s2->e[j++];
}
sum->num = k;
return sum;
}
class Element
{
public:
int i;
int j;
int x;
Element(int i, int j, int x)
:i(i), j(j), x(x)
{
}
~Element() {}
bool operator==(Element e)
{
if (i==e.i && j == e.j ) return true;
else return false;
}
bool operator < (Element e)
{
if (i < e.i || i == e.i && j < e.j ) return true;
else return false;
}
bool operator > (Element e)
{
if (i > e.i || i == e.i && j > e.j ) return true;
else return false;
}
Element& operator= (Element e)
{
i = e.i;
j = e.j;
x = e.x;
return this;
}
friend ostream & operator << (ostream &os, const Element e)
{
os << e.i << " " << e.j << " " << e.x;
return os;
}
}
class Sparse
{
private:
int m;
int n;
int num;
Element *ele;
public:
Sparse(int m, int n, int num)
{
this->m = m;
this->n = n;
this->num =num;
ele = new Element[num];
}
~Sparse()
{
delete []ele;
}
void create();
void display();
Sparse *add(Sparse s2);
friend ostream & operator<<(ostream &os, Sparse &s) {
int i, j, k;
i = j = k = 0;
for (i = 0; i < s.m; i++) {
for (j = 0; j < s.n; j++ ) {
if ( s.ele[k].i == i && s.ele[k].j == j) {
os << s.ele[k++].x << " ";
} else {
os << "0 "
}
}
os << endl;
}
return os
}
friend istream & operator >> (istream &is, Sparse &s) {
cout << "Enter nonzero elements\n";
for ( int i = 0; i < s.num; i++) {
is >> s.ele[i].i >> s.ele[i].j >> s.ele[i].x;
}
return is;
}
Sparse operator+(Sparse &s);
};
void Sparse::create()
{
int i, j,k, x;
printf("Enter the dimensions\n");
scanf("%d%d", &i, &j);
m = i;
n = j;
printf("Enter the num of elements\n");
scanf("%d", &k);
num = k;
//e = (struct Element*) malloc(sizeof(struct Element) * k);
ele = new Element[k];
printf("Enter the Sparse Matrix\n");
for ( k = 0; k < s->num; k ++) {
scanf("%d%d%d", &i,&j,&x);
ele[k].i = i;
ele[k].j = j;
e[k].x = x;
}
}
void Sparse::display()
{
int i;
int j;
int k;
for ( i = 0; i < m; i ++) {
for ( j = 0 ; j < n; j++) {
for ( int k = 0; k < num; k++) {
if ( i == e[k].i && j == e[k].j ) {
printf("%d ", e[k].x);
}else {
printf("0 ");
}
}
}
printf("\n");
}
Sparse *Sparse::add(Sparse s2)
{
if ( m != s2->m || n != s2->n ) return NULL;
Sparse *sum = new Sparse();
//sum = (struct Sparse *) malloc(sizeof(struct Sparse));
m = s1->m;
n= s1->n;
sum->e = new Element[s1->num + s2->num];
// or sum->e = (struct Element *) malloc(sizeof(struct Element) * (s1->num + s2->num));
int i = 0; // trace s1
int j = 0; // trace s2
int k = 0;
while ( i < num && j < s2->num) {
if ( e[i].i < s2->e[j].i ) {
sum->e[k++] = e[i++]; // copy s1 to sum
} else if ( s2->e[j].i < e[i].i ) {
sum->e[k++] = s2->e[j++]; // copy s2 to sum
} else {
if ( e[i].j < s2->e[j].j ) {
sum->e[k++] = e[i++]; // copy s1 to sum
} else if ( s2->e[j].j < e[i].j ) {
sum->e[k++] = s2->e[j++]; // copy s2 to sum
} else {
sum->e[k] = e[i++];
sum->e[k++].x += s2->e[j++].x;
}
}
}
while ( i < num ) {
sum->e[k++] = e[i++];
}
while ( j < s2->num) {
sum->e[k++] = s2->e[j++];
}
sum->num = k;
return sum;
}
Sparse Sparse::operator+(Sparse &s2)
{
if ( m != s2.m || n != s2.n ) return Sparse(0,0,0);
Sparse *sum = new Sparse(m, n, num + s2.num);
//sum = (struct Sparse *) malloc(sizeof(struct Sparse));
// or sum->e = (struct Element *) malloc(sizeof(struct Element) * (s1->num + s2->num));
int i = 0; // trace s1
int j = 0; // trace s2
int k = 0;
while ( i < num && j < s2.num) {
if ( e[i].i < s2.e[j].i ) {
sum->e[k++] = e[i++]; // copy s1 to sum
} else if ( s2.e[j].i < e[i].i ) {
sum->e[k++] = s2.e[j++]; // copy s2 to sum
} else {
if ( e[i].j < s2.e[j].j ) {
sum->e[k++] = e[i++]; // copy s1 to sum
} else if ( s2.e[j].j < e[i].j ) {
sum->e[k++] = s2.e[j++]; // copy s2 to sum
} else {
sum->e[k] = e[i++];
sum->e[k++].x += s2.e[j++].x;
}
}
}
while ( i < num ) {
sum->e[k++] = e[i++];
}
while ( j < s2->num) {
sum->e[k++] = s2.e[j++];
}
sum->num = k;
return *sum;
}