-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspreadsheet.cpp
More file actions
166 lines (144 loc) · 3.65 KB
/
spreadsheet.cpp
File metadata and controls
166 lines (144 loc) · 3.65 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <QVariant>
#include <QtMath>
#include <QDebug>
#include <QDataStream>
#include "spreadsheet.h"
SpreadSheet::SpreadSheet()
{
cellData[QPair<int,int>(2,3)] = "Hello";
cellStyles[QPair<int,int>(2,3)] = CellStyle{true, false};
}
QString SpreadSheet::getAt(int x, int y)
{
QPair<int,int> at(x, y);
if (cellData.contains(at))
{
return cellData.value(at);
} else return "";
}
bool SpreadSheet::contains(int x, int y)
{
return cellData.contains(QPair<int,int>(x, y));
}
void SpreadSheet::setAt(int x, int y, QString val)
{
cellData[QPair<int,int>(x,y)] = val;
}
void SpreadSheet::clearReferences()
{
refStack.clear();
}
double SheetEval::getVar(QString named)
{
if (named == "pi")
return 3.141592;
else if (named.length() >= 2 && named[0].isUpper() && named[1].isNumber())
{
qDebug() << sheet->refStack;
bool ok = true;
int col = named[0].unicode() - 'A';
named.remove(0, 1);
// Starts at 1
int row = named.toInt(&ok) - 1;
qDebug() << "Reference to" << row << col;
if (!ok)
{
qDebug() << "Is not number";
throw DefinitionError{named};
}
if (sheet->refStack.contains(QPair<int,int>(row, col)))
{
qDebug() << "Recursion :(";
for (auto coord : sheet->refStack)
{
qDebug() << coord;
}
throw RecursionError{named};
}
auto res = sheet->evalAt(row, col).toDouble(&ok);
if (!ok)
throw DefinitionError{named};
qDebug() << "=" << res;
return res;
}
else throw DefinitionError{named};
}
double SheetEval::getFunc(QString named, QList<double> args)
{
if (named == "sqrt")
return qSqrt(args[0]);
else if (named == "sin")
return qSin(args[0]);
else if (named == "tan")
return qTan(args[0]);
else if (named == "cos")
return qCos(args[0]);
else if (named == "ceil")
return qCeil(args[0]);
else if (named == "floor")
return qFloor(args[0]);
else throw DefinitionError{named};
}
CellStyle CellStyle::basic()
{
return CellStyle
{
false,
false,
};
}
CellStyle *SpreadSheet::getStyle(int row, int col)
{
QPair<int,int> p(row, col);
return &cellStyles[p];
}
QString SpreadSheet::evalAt(int x, int y)
{
QString value = getAt(x, y);
if (value.startsWith("="))
{
refStack.append(QPair<int,int>(x, y));
value.remove(0, 1);
try
{
qDebug() << "Evaling" << value << refStack;
SheetEval eval{this};
eval.setSourceString(value);
QString res = QString::number(eval.expr());
refStack.removeLast();
return res;
}
// No point cleaning up the ref stack if an error occurs.
catch (ParsingError &)
{
throw EvalError{"Error parsing expression"};
}
catch (DefinitionError &)
{
throw EvalError{"Error resolving variable"};
}
catch (RecursionError &)
{
throw EvalError{"Recursive cell references are not allowed"};
}
}
else return value;
}
QDataStream &operator<<(QDataStream &out, const SpreadSheet &in)
{
out << in.cellData << in.cellStyles;
return out;
}
QDataStream &operator>>(QDataStream &in, SpreadSheet &out)
{
in >> out.cellData >> out.cellStyles;
return in;
}
QDataStream &operator<<(QDataStream &out, const CellStyle &in)
{
out << in.bold << in.italic;
}
QDataStream &operator>>(QDataStream &in, CellStyle &out)
{
in >> out.bold >> out.italic;
}