blob: 7e4a7acfae467b093c79c18167ed3da94f5257e2 (
plain) (
blame)
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
|
#include "Table.h"
#include "CellDouble.h"
#include "CellInt.h"
#include "CellString.h"
#include "CellFormula.h"
#include <cstring>
Table::Table(unsigned rows, unsigned cols) {
if (rows == 0 || cols == 0)
return;
cells = List<List<Cell*>>(rows);
for (unsigned i = 0; i < rows; i++)
cells[i] = List<Cell*>(cols);
}
unsigned Table::get_cols() const {
return cells[0].get_length();
}
bool containsNumber(const char*& str) {
while (*str == ' ') str++;
return ((*str == '+' || *str == '-') && (*(str + 1) >= '0' && *(str + 1) <= '9')) ||
(*str >= '0' && *str <= '9'); // + or -, followed by digit, or it just a digit
}
void Table::putCell(unsigned row, unsigned col, const char* rawValue) {
Cell* newCell;
if (containsNumber(rawValue)) {
double intParse = atoi(rawValue);
double doubleParse = atof(rawValue);
newCell = (doubleParse - intParse > 0.00001 && doubleParse > 0.00001)
? (Cell*)new CellDouble(doubleParse) : (Cell*)new CellInt(intParse);
}
else {
newCell = (*rawValue == '=')
? (Cell*)new CellFormula(rawValue, &cells) : (Cell*)new CellString(rawValue);
}
if (cells[row][col] != nullptr)
delete cells[row][col];
cells[row][col] = newCell;
}
List<String> Table::getAllCells() const {
List<String> toRet;
for (unsigned i = 0; i < cells.get_length(); i++) {
for (unsigned j = 0; j < cells[i].get_length(); j++)
if (cells[i][j] == nullptr)
toRet.add("");
else toRet.add(cells[i][j]->getValueForPrint());
}
return toRet;
}
|