-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculatorcore.h
More file actions
49 lines (40 loc) · 1.22 KB
/
Copy pathcalculatorcore.h
File metadata and controls
49 lines (40 loc) · 1.22 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
#ifndef CALCULATORCORE_H
#define CALCULATORCORE_H
#include <QString>
#include <QVector>
#include <QStringList>
class CalculatorCore
{
public:
CalculatorCore();
struct Result {
QString valueStr;
bool isError;
QString errorMsg;
};
Result compute(const QString &expression);
static QString toHexFloatString(long double v, int fracDigits = 12);
private:
enum class TokType {
Number,
Op,
UnaryPreOp,
UnaryPostOp,
LParen,
RParen
};
struct Token {
TokType type;
QString text;
};
bool tokenize(const QString &expr, QVector<Token> &outTokens, QString &err) const;
bool toRpn(const QVector<Token> &tokens, QVector<Token> &outRpn, QString &err) const;
bool evalRpn(const QVector<Token> &rpn, long double &outValue, QString &err) const;
int precedence(const QString &op) const;
bool isLeftAssociative(const QString &op) const;
static bool parseHexFloat(const QString &s, long double &out, QString &err);
static long double fastPow(long double base, long long exp);
static long double safePow(long double a, long double b);
static long double factorial(long long n);
};
#endif // CALCULATORCORE_H