-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
84 lines (66 loc) · 2.25 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
#include <algorithm>
#include <iostream>
#include <string>
#include <regex>
#include <sparse_matrix.hpp>
using namespace std;
using namespace regex_constants;
static const regex SET(R"(^\s*(\d+)\s*,\s*(\d+)\s*=\s*(\d+)\s*$)", icase | optimize);
static const regex PRINT(R"(^\s*(?:p|print)\s*(\d+)\s*,\s*(\d+)\s*$)", icase | optimize);
static const regex SAVEGV(R"(^\s*(?:g|graphviz)\s+([^\\\?%\*]+)\s*$)", icase | optimize);
static const regex QUIT(R"(^\s*(?:q|quit|exit)\s*$)", icase | optimize);
/**
* @brief Ponto de entrada
*
* @param argc Número de argumentos da linha de comando
* @param argv Valores dos argumentos da linha de comando
* @return int Código de retorno
*/
int main(int argc, char** argv) {
string line;
smatch m;
sparse_matrix<int> matrix(0);
// Cabeçalho
cout << "Interactive Sparse Matrix" << endl;
cout << endl;
cout << "n,m=x : Set (N,M) to be X" << endl;
cout << "p|print n,m : Print out the value at (N,M)" << endl;
cout << "g|graphviz <filename> : Save Graphviz model to file" << endl;
cout << "q|e|quit|exit : Quit" << endl;
cout << endl;
cout << "Have fun!" << endl;
cout << endl;
for (;;) {
cout << "matrix> ";
getline(cin, line);
// Encerra o programa
if (regex_match(line, QUIT)) {
break;
// Define o valor de uma posição na matriz
} else if (regex_search(line, m, SET)) {
try {
int r = stoi(m[1]),
c = stoi(m[2]),
x = stoi(m[3]);
matrix[r][c] = x;
} catch (const char* msg) {
cerr << "Err: " << msg << endl;
}
// Escreve o valor de uma posição da matriz na tela
} else if (regex_search(line, m, PRINT)) {
int r = stoi(m[1]),
c = stoi(m[2]);
cout << matrix[r][c] << endl;
// Salva a matriz como modelo do graphviz num arquivo
} else if (regex_search(line, m, SAVEGV)) {
ofstream f(m[1]);
matrix.gv_save(f);
f.close();
// Comando inválido
} else {
cerr << "Err: Invalid command" << endl;
continue;
}
}
return 0;
}