Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Models / CellFormula.cpp
blob: 3029a19ff8135a500ab595627121bb61515a35fa (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
#include "CellFormula.h"
#include "CellDouble.h"
#include <cstring>

CellFormula::CellFormula(const char* str) : referencedCells(), formula(), rawFormula() {
	parseAndSetValue(str);
}

double CellFormula::calculate(unsigned index) {
	double lval = formula[index].left->getNumeralValue();

	if (formula[index].right & branch) {
		switch (formula[index++].right & !branch) {
			case plus   : lval += formula[index].left->getNumeralValue(); break;
			case minus  : lval -= formula[index].left->getNumeralValue(); break;
			case mult   : lval *= formula[index].left->getNumeralValue(); break;
			case div    : lval /= formula[index].left->getNumeralValue(); break;
			// case pow : lval += formula[index].left->getNumeralValue(); break;
		}
	}

	switch(formula[index].right) {
		case plus  : return lval + calculate(index++);
		case minus : return lval - calculate(index++);
		case mult  : return lval * calculate(index++);
		case div   : return lval / calculate(index++);
		case pow   :
		case none  :
		default    : return lval;
	}
}

String CellFormula::getValueForPrint() {
	return String() += calculate();
}

void CellFormula::parseAndSetValue(const char* str) {
	str++;
	while (*str == ' ') str++;
	double number = atof(str);
}

void CellFormula::readFromFile(std::ifstream& file) {
	file >> rawFormula;
}

void CellFormula::writeToFile(std::ofstream& file) {
	file << rawFormula;
}