-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsv.h
More file actions
105 lines (85 loc) · 2.97 KB
/
Csv.h
File metadata and controls
105 lines (85 loc) · 2.97 KB
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
#ifndef CSV_H
#define CSV_H
#include <vector>
#include <iosfwd>
#include <map>
#include <string>
#include <stdexcept>
namespace errors {
/*
Base class of interpreting errors
*/
struct ErrorInterpreter : std::runtime_error {
explicit ErrorInterpreter(const std::string &message)
: std::runtime_error(message) {
}
};
}
namespace csv_interpreter {
class Csv {
public:
enum Operation {
ADD, SUBTRACT, MULTIPLY, DIVIDE, NOT_SET
};
/*
table consists of Cells
which can be either calculated value (integer) or formula (ARG1 OP ARG2)
*/
struct Cell {
bool isCalculated = false;
int calculatedValue = 0;
std::tuple<bool, int, int> arg1; // {isInteger, row_i , column_i} : indexes of arg in table
std::tuple<bool, int, int> arg2; // or {isInteger, i, -1} : integer i
Operation op = NOT_SET;
};
/*
calculates Cell values into calculatedValue field using formulas
calls calc() (private)
*/
void compute();
/*
overloaded operator of reading from std::fstream
*/
friend std::fstream &operator>>(std::fstream &in, Csv &csvToRead);
/*
overloaded operator of writing to std::ostream
*/
friend std::ostream &operator<<(std::ostream &out, const Csv &csvToPrint);
private:
std::vector<std::string> mColumns; // column names
std::vector<int> mRows; // row numbers
std::vector<std::vector<Cell>> mTable;
/*
parses cells content
throws InvalidFormulaFormat or InvalidArgument
converts cell string-content into Cell structure
returns parsed table
*/
std::vector<std::vector<Cell>> parse(const std::vector<std::string> &columns,
const std::vector<int> &rows,
const std::vector<std::vector<std::string>> &table);
/*
checks dimension of table (throws InvalidTableFormat)
checks names of columns and numbers of rows (throws InvalidColumnName)
*/
void check(const std::vector<std::vector<std::string>> &table);
/*
reads csv-format file into two-dimensional vector of string (separating by commas)
calls check() and parse() inside
*/
void read(std::fstream &in);
/*
returns value of mTable[i][j]
if it is not calculated goes in recursion
throws IncorrectFormula if cells refer to each other in a loop
throws DivisionByZero error
*/
int calc(int i, int j, std::vector<std::vector<bool>> &marked);
/*
prints csv-format table in std::ostream
throws NotCalculatedValue if value of cell has not been calculated
*/
void print(std::ostream &out) const;
};
} // namespace csv_interpreter
#endif //CSV_H