-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
68 lines (56 loc) · 1.49 KB
/
main.cpp
File metadata and controls
68 lines (56 loc) · 1.49 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
#include <iostream>
#include "Math.h"
#include "Parser.h"
#include "Visitor.h"
#include "InfixPrinterVisitor.h"
#include "DerivativeVisitor.h"
typedef InfixPrinterVisitor Printer;
typedef DerivativeVisitor Derivativer;
class MathContainer {
protected:
AtomPtr expr, last;
std::shared_ptr<Printer> printer;
std::shared_ptr<Derivativer> derivativer;
public:
MathContainer() : printer(std::make_shared<Printer>()) , derivativer(std::make_shared<Derivativer>()) {
}
void setExpr(AtomPtr t) {
expr = t;
}
Atom* getExpr() {
return expr.get();
}
Atom* derivate() {
last = expr;
expr.get()->Accept(derivativer.get());
expr = derivativer.get()->get();
return expr.get();
}
void print() {
expr.get()->Accept(printer.get());
std::cout << printer.get()->get() << std::endl;
}
void reset() {
expr.reset();
expr = last;
last.reset();
}
};
int main() {
MathContainer mtc;
Parser p;
p.convertFromRPN("5x2^+");
std::cout << "===============" << std::endl;
//p.convertFromInfix("(5 + x + 2*x)/(5*5) - 55 + 28.32 * x / x ^ 2");
p.convertFromInfix("x^5 + 6*x^7 + 8*x + 9");
std::cout << "===============" << std::endl;
AtomPtr expr = p.getExpr();
AtomPtr expr2 = std::make_shared<Add>(std::make_shared <Times>(std::make_shared<X>(), std::make_shared<X>()), std::make_shared<X>());
mtc.setExpr(expr);
mtc.print();
std::cout << " f(1) = " << mtc.getExpr()->eq(1) << std::endl;
mtc.derivate();
mtc.print();
std::cout << " f(1) = " << mtc.getExpr()->eq(1) << std::endl;
return 0;
}