-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitFormMain.cpp
More file actions
187 lines (168 loc) · 5.14 KB
/
UnitFormMain.cpp
File metadata and controls
187 lines (168 loc) · 5.14 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//---------------------------------------------------------------------------
/*
FunctionPlotter, plots a function
Copyright (C) 2010 Richel Bilderbeek
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <sstream>
#include <boost/scoped_ptr.hpp>
#include "UnitFunctionParser.h"
#include "UnitFormMain.h"
#include "UnitFormAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
OnEditChange(0);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::OnEditChange(TObject *Sender)
{
FunctionParser f;
//Parse the formula
f.Parse(EditFormula->Text.c_str(),"x");
if (f.GetParseErrorType()!= FunctionParser::FP_NO_ERROR)
{
PanelStatus->Caption = "Function could not be parsed";
return;
}
if (!IsDouble(EditXmin->Text.c_str()))
{
PanelStatus->Caption = "Value of x_min is not a valid double";
return;
}
if (!IsDouble(EditXmax->Text.c_str()))
{
PanelStatus->Caption = "Value of x_max is not a valid double";
return;
}
const double x_min = EditXmin->Text.ToDouble();
const double x_max = EditXmax->Text.ToDouble();
if (x_min >= x_max)
{
PanelStatus->Caption = "Value of x_min must be smaller than x_max";
return;
}
const double dx = (x_max - x_min) / 100.0;
Series->Clear();
for (double x = x_min; x < x_max; x+=dx)
{
//Evaluate the parsed formula
const double xs[1] = { x };
const double y = f.Eval(xs);
if (f.EvalError()!=0)
{
continue;
}
Series->AddXY(x,y);
}
PanelStatus->Caption = "Function plotted successfully";
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppIsDouble.htm
const bool IsDouble(const String& s)
{
try
{
s.ToDouble();
return true;
}
catch (EConvertError& e)
{
return false;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonAboutClick(TObject *Sender)
{
boost::scoped_ptr<TFormAbout> f(new TFormAbout(0));
f->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::OnRangeChange(TObject *Sender)
{
if (!IsDouble(EditXmin->Text.c_str()))
{
PanelStatus->Caption = "Value of x_min is not a valid double";
return;
}
if (!IsDouble(EditXmax->Text.c_str()))
{
PanelStatus->Caption = "Value of x_max is not a valid double";
return;
}
const double x_min = EditXmin->Text.ToDouble();
const double x_max = EditXmax->Text.ToDouble();
if (x_min >= x_max)
{
PanelStatus->Caption = "Value of x_min must be smaller than x_max";
return;
}
Chart->BottomAxis->Minimum = x_min;
Chart->BottomAxis->Maximum = x_max;
PanelStatus->Caption = "Chart rescaled successfully";
OnEditChange(0);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ButtonApproxIntegralClick(TObject *Sender)
{
FunctionParser f;
//Parse the formula
f.Parse(EditFormula->Text.c_str(),"x");
if (f.GetParseErrorType()!= FunctionParser::FP_NO_ERROR)
{
PanelStatus->Caption = "Function could not be parsed";
return;
}
if (!IsDouble(EditXmin->Text.c_str()))
{
PanelStatus->Caption = "Value of x_min is not a valid double";
return;
}
if (!IsDouble(EditXmax->Text.c_str()))
{
PanelStatus->Caption = "Value of x_max is not a valid double";
return;
}
const double x_min = EditXmin->Text.ToDouble();
const double x_max = EditXmax->Text.ToDouble();
if (x_min >= x_max)
{
PanelStatus->Caption = "Value of x_min must be smaller than x_max";
return;
}
const double dx = (x_max - x_min) / 10000.0;
double sumy = 0.0;
for (double x = x_min; x < x_max; x+=dx)
{
//Evaluate the parsed formula
const double xs[1] = { x };
const double y = f.Eval(xs);
if (f.EvalError()!=0)
{
continue;
}
sumy+=y;
}
const double surface = sumy * dx;
PanelStatus->Caption = "Approximated surface under curve: " + FloatToStr(surface);
}
//---------------------------------------------------------------------------