-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.cpp
More file actions
executable file
·125 lines (99 loc) · 1.95 KB
/
Copy pathvariables.cpp
File metadata and controls
executable file
·125 lines (99 loc) · 1.95 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
/*
*
* This is a simple variable list for scicalc
*
* Friedrich Feichtinger, 13.09.2012
*
*/
#include "variables.h"
#include "parseException.h"
#include <cmath>
#include <QDebug>
QList<Variable*>Variables::variables;
void Variables::init()
{
variables.clear();
// math
set("pi", M_PI);
set("i", Value::Number(0, 1));
set("j", Value::Number(0, 1));
// mechanic
set("_g", 9.81);
set("_G", 6.67384e-11);
// electromagnetism
set("_mu0", 4*M_PI*1e-7);
set("_eps0", 8.85418781762e-12);
set("_c0", 299792458);
set("_e", 1.60217653e-19);
// thermodynamic
set("_kB", 1.3806488e-23);
// quantum physic
set("_h", 6.62606957e-34);
}
void Variables::set(QString name, long double value, QString unit)
{
set(name, Value(value), unit);
}
void Variables::set(QString name, Value::Number value, QString unit)
{
set(name, Value(value), unit);
}
void Variables::set(QString name, Value value, QString unit)
{
//qDebug() << "setVariable" << name;
// search the list for this name
Variable* var=search(name);
// check if variable allready exists
if(var==0)
{
// no: let's make a new variable
var=new Variable(name, value, unit);
variables.append(var);
}
else
{
// yes: let's change it's value
var->value=value;
var->unit=unit;
}
}
long double Variables::get(QString name)
{
Value value=getValue(name);
if(!value.isScalar())
{
throw ParseException("variable '"+name+"' is not a scalar");
}
if(value.scalar().imag()!=0)
{
throw ParseException("variable '"+name+"' is not real");
}
return value.scalar().real();
}
Value Variables::getValue(QString name)
{
Variable* var=search(name);
if(var==0)
{
throw ParseException("variable '"+name+"' has no value yet");
}
else
{
return var->value;
}
}
QList<Variable*> Variables::all()
{
return variables;
}
Variable* Variables::search(QString name)
{
for(int i=0; i<variables.length(); i++)
{
if(variables.at(i)->name==name)
{
return variables.at(i);
}
}
return 0;
}