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
|
#include "CellFormula.h"
#include "CellDouble.h"
#include <cstring>
#include <iostream>
#include <stdexcept>
const Cell* CellFormula::ptrByInd(Pair<int, int> loc) const {
return (loc.left < 0) ? &localCells[loc.right] : (*tableCells)[loc.left][loc.right];
}
double pow(double x, unsigned y) {
if (y == 0) return 1;
double res = x;
for (unsigned i = 1; i < y; i++)
res *= x;
return res;
}
double CellFormula::calculate(unsigned index) const {
const Cell* currCell = ptrByInd(formula[index].left);
double numVal = currCell->getNumeralValue();
while (formula[index].right < 0) {
switch (-formula[index++].right) {
case '+' : numVal += ptrByInd(formula[index].left)->getNumeralValue(); break;
case '*' : numVal *= ptrByInd(formula[index].left)->getNumeralValue(); break;
case '/' : if (ptrByInd(formula[index].left)->getNumeralValue() == 0) throw std::logic_error("");
numVal /= ptrByInd(formula[index].left)->getNumeralValue(); break;
case '^' : numVal = pow(numVal, ptrByInd(formula[index].left)->getNumeralValue()); break;
}
}
double temp;
switch(formula[index].right) {
case '+' : return numVal + calculate(++index);
case '*' : return numVal * calculate(++index);
case '/' : temp = calculate(++index);
if (temp == 0) throw std::logic_error("");
return numVal / temp;
case '^' : return pow(numVal, calculate(++index));
case '\0' :
default : return numVal;
}
}
CellFormula::CellFormula(const char* str, const List<List<Cell*>>* tableCells) : localCells(), formula(), rawFormula() {
this->tableCells = tableCells;
CellFormula::parseAndSetValue(str);
}
#include <iostream>
double CellFormula::getNumeralValue() const {
try {
return calculate();
}
catch (std::logic_error) {
return 0;
}
}
String CellFormula::getValueForPrint() const {
try {
return String() += calculate();
}
catch (std::logic_error) {
return "Error";
}
}
void CellFormula::parseAndSetValue(const char* str) {
rawFormula = String(str);
Pair<int, int> loc;
char currOp;
while (*str != '\0') {
str++;
while (*str == ' ') str++;
if (*str == 'R') {
int row = atoi(++str);
while (*str != 'C') str++;
int col = atoi(++str);
while (*str != ' ' && *str != '\0') str++;
while (*str == ' ') str++;
currOp = *str;
loc = {row, col};
}
else {
double val = atof(str);
while ((*str >= '0' && *str <= '9') || *str == ' ' || *str == '.') str++;
currOp = *str;
localCells.add(CellDouble(val));
loc = {-1, localCells.get_count() - 1};
}
if (currOp == '^') currOp *= -1;
else if ((currOp == '+' || currOp == '-') && formula.get_count() != 0) {
for (unsigned i = formula.get_count() - 1; i < formula.get_count() && (formula[i].right == '*' || formula[i].right == '/'); i--)
if (formula[i].right > 0) formula[i].right *= -1;
}
if (currOp == '-') {
formula.add({loc, '+'});
localCells.add(CellDouble(-1.0));
formula.add({{-1, localCells.get_count() - 1}, '*' * -1});
}
else formula.add({loc, currOp});
}
}
void CellFormula::readFromFile(std::ifstream& file) {
file >> rawFormula;
}
void CellFormula::writeToFile(std::ofstream& file) {
file << rawFormula;
}
|