-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatVar.cpp
More file actions
90 lines (85 loc) · 2.01 KB
/
FloatVar.cpp
File metadata and controls
90 lines (85 loc) · 2.01 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
#include "FloatVar.h"
#include "BooleanVar.h"
FloatVar::FloatVar(float value)
{
f = value;
}
FloatVar::FloatVar(const FloatVar& var)
{
f = var.f;
}
FloatVar::~FloatVar(void)
{
}
Variable * FloatVar::clone(void) const
{
return new FloatVar(f);
}
std::shared_ptr<Variable> FloatVar::operate(const std::string& op, std::shared_ptr<Variable> rhs, bool allocate)
{
std::shared_ptr<FloatVar> vf = std::dynamic_pointer_cast<FloatVar>(rhs);
if (vf!=0)
{
switch(op.front())
{
case'+':
if (op.back()=='=') {
f += vf->f;
return *std::shared_ptr<std::shared_ptr<Variable>>(address);
} else {
return std::shared_ptr<Variable>(new FloatVar(f+vf->f));
}
case'-':
if (op.back()=='=') {
f -= vf->f;
return *std::shared_ptr<std::shared_ptr<Variable>>(address);
} else {
return std::shared_ptr<Variable>(new FloatVar(f-vf->f));
}
case'*':
if (op.back()=='=') {
f *= vf->f;
return *std::shared_ptr<std::shared_ptr<Variable>>(address);
} else {
return std::shared_ptr<Variable>(new FloatVar(f*(vf->f)));
}
case'/':
if (op.back()=='=') {
f /= vf->f;
return *std::shared_ptr<std::shared_ptr<Variable>>(address);
} else {
return std::shared_ptr<Variable>(new FloatVar(f/vf->f));
}
case'<':
if (op.back()=='=')
return std::shared_ptr<Variable>(new BooleanVar(f<=vf->f));
else
return std::shared_ptr<Variable>(new BooleanVar(f<vf->f));
case'>':
if (op.back()=='=')
return std::shared_ptr<Variable>(new BooleanVar(f>=vf->f));
else
return std::shared_ptr<Variable>(new BooleanVar(f>vf->f));
case'=':
if (op.back()=='=' && op.size()==2) {
return std::shared_ptr<Variable>(new BooleanVar(f==vf->f));
} else {
f=vf->f;
return shared_from_this();
}
case'!':
if (op.back()=='=') {
return std::shared_ptr<Variable>(new BooleanVar(f!=vf->f));
}
}
}
else
{
if (op.compare("-")==0)
return std::shared_ptr<Variable>(new FloatVar(-f));
}
if (allocate)
return Variable::operate(op, rhs);
else
return 0;
}