-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
134 lines (118 loc) · 4.18 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <chrono>
#include "sparse_matrix_2d_linked_list.h"
using namespace std;
bool structure_is_okay(row_node* head, int num_rows, int num_columns){
int rows = 0;
while(head!=nullptr){
if (rows >= num_rows){
cout << "Too many rows!\n";
return false;
}
cell_node* cell = head->cell;
if (cell != nullptr){
if (cell->left != nullptr){
cout << "Bad first node!\n";
return false;
}
while (cell!= nullptr){
if (cell->right != nullptr){
if (cell->right->left != cell){
cout << "Bad connection!\n";
return false;
}
if (cell->right->column <= cell->column){
cout << "Columns should be increasing in a row!\n";
return false;
}
}
if (cell->value ==0){
cout << "You can't store zeros in cells!\n";
return false;
}
if (cell->column >= num_columns){
cout << "Too many columns at row " << rows << "!\n";
return false;
}
cell = cell->right;
}
}
head = head->down;
rows++;
}
if (rows < num_rows){
cout << "Too few rows!\n";
return false;
}
return true;
}
int main() {
/////// Timing data ///////
chrono::time_point<chrono::system_clock> start, end;
chrono::duration<double> total_time;
//////////////////////////
string filename1, filename2;
int m1_rows, m1_cols, m2_rows, m2_cols;
cout << "Matrix 1 filename: ";
cin >>filename1;
cout << "Matrix 2 filename: ";
cin >>filename2;
row_node* m1 = read_matrix(filename1, m1_rows, m1_cols);
structure_is_okay(m1, m1_rows, m1_cols);
row_node* m2 = read_matrix(filename2, m2_rows, m2_cols);
structure_is_okay(m2, m2_rows, m2_cols);
cout << "m1\n";
print_matrix(m1, m1_rows, m1_cols);
cout << "m2\n";
print_matrix(m2, m2_rows, m2_cols);
if (m1_rows == m2_rows && m1_cols == m2_cols){
start = chrono::system_clock::now();
row_node* m3 = add_matrices(m1, m2, m1_rows, m1_cols);
end = chrono::system_clock::now();
total_time = end - start;
cout << "Time to add matrices: " << total_time.count() << " seconds\n";
structure_is_okay(m2, m2_rows, m2_cols);
cout << "m3\n";
print_matrix(m3, m2_rows, m2_cols);
delete_matrix(m3);
}
start = chrono::system_clock::now();
bool symmetric = is_symmetric(m1, m1_rows, m1_cols);
end = chrono::system_clock::now();
total_time = end - start;
cout << "Time to check if m1 is symmetric: " << total_time.count() << " seconds\n";
if (symmetric)
cout << "Matrix m1 is symmetric.\n";
else
cout << "Matrix m1 is not symmetric.\n";
start = chrono::system_clock::now();
symmetric = is_symmetric(m2, m2_rows, m2_cols);
end = chrono::system_clock::now();
total_time = end - start;
cout << "Time to check if m2 is symmetric: " << total_time.count() << " seconds\n";
if (symmetric)
cout << "Matrix m2 is symmetric.\n";
else
cout << "Matrix m2 is not symmetric.\n";
start = chrono::system_clock::now();
row_node* m1_transpose = transpose(m1, m1_rows, m1_cols);
end = chrono::system_clock::now();
total_time = end - start;
cout << "Time to transpose m1: " << total_time.count() << " seconds\n";
cout << "m1 transposed\n";
print_matrix(m1_transpose, m1_cols, m1_rows);
delete_matrix(m1_transpose);
start = chrono::system_clock::now();
row_node* m2_transpose = transpose(m2, m2_rows, m2_cols);
end = chrono::system_clock::now();
total_time = end - start;
cout << "Time to transpose m2: " << total_time.count() << " seconds\n";
cout << "m2 transposed\n";
print_matrix(m2_transpose, m2_cols, m2_rows);
delete_matrix(m2_transpose);
delete_matrix(m1);
delete_matrix(m2);
return 0;
}