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
187
188
189
190
191
192
193
| //---------------------------------------------------------------------------
#include <vcl.h>
#include<math.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
class Telement {
protected:
Telement *left, *right;
Telement (Telement* L,Telement* R){
left = L; right = R;
}
public:
~Telement(void) {delete left; delete right;}
virtual double rezult(void) = 0;
virtual Telement* copy(void) = 0;
virtual Telement* differ(void) = 0;
virtual void set_var(double X){
if (left) left -> set_var(X);
if (right) right -> set_var(X);
}
};
//---------------------------------------------------------------------------
class Real : public Telement{
double f;
public:
Real(double F):Telement(NULL,NULL){f=F;}
double rezult(void) {return f; }
Telement* copy (void){return new Real(f);}
Telement* differ(void){return new Real(0);}
};
//---------------------------------------------------------------------------
class Var :public Telement{
double x;
public:
Var(double X = 0):Telement(NULL, NULL){x = X;}
double rezult(void){return x;}
Telement* copy(void){return new Var(x);}
Telement* differ(void){return new Real(1.0);}
void set_var(double X) {x = X;}
};
//---------------------------------------------------------------------------
struct Plus:public Telement{
Plus(Telement* L,Telement* R):Telement(L, R){};
double rezult(void) { return left -> rezult() + right -> rezult();}
Telement* copy(void) {return new Plus(left -> copy(), right -> copy());}
Telement* differ(void) { return new Plus(left -> differ(),right -> differ());}
};
//---------------------------------------------------------------------------
struct Minus:public Telement{
Minus(Telement* L,Telement* R):Telement(L, R){};
double rezult(void) {return left->rezult() - right->rezult();}
Telement* copy(void) {return new Minus(left->copy(), right->copy());}
Telement* differ(void) {return new Minus(left->differ(), right->differ());}
};
//--------------------------------------------------------------------------------
struct Mult:public Telement{
Mult(Telement* L, Telement* R):Telement(L, R){};
double rezult(void){return left->rezult()*right->rezult();}
Telement* copy(void){return new Mult(left->copy(),right->copy());}
Telement* differ(void){return new Plus(new Mult( left->differ(), right->copy()),
new Mult( left->copy(), right->differ()));}
};
//-------------------------------------------------------------------------------
struct Div:public Telement{
Div(Telement* L,Telement* R):Telement(L, R) {};
double rezult(void){return left->rezult() / right->rezult();}
Telement* copy(void){return new Div(left->copy(), right->copy());}
Telement* differ(void){
return new Div( new Minus(new Mult(left->differ(), right->copy()),
new Mult(left->copy(), right->differ())),
new Mult( right->copy(), right->copy()));
}
};
//---------------------------------------------------------------------------
int PosFromEnd(AnsiString s, AnsiString sub);
Telement* form(AnsiString s){
Telement* h;
int i,p,l = s.Length();
AnsiString s1, s2;
// Аналізуємо справа наліво:
if ((p = PosFromEnd(s,"+")) > 1){ // Якщо натрапляємо на "+"
s1 =s.SubString(1, p - 1);
s2 =s.SubString(p+1, l-p);
h = new Plus(form(s1), form(s2)); // Створюємо вершину Plus
}
else if ((p = PosFromEnd(s,"-")) > 1){ // Якщо натрапляємо на "-"
s1 =s.SubString(1, p - 1);
s2 = s.SubString(p+1, l-p);
h = new Minus(form(s1), form(s2)); // Створюємо вершину Minus
}
else if ((p = PosFromEnd(s, "*")) > 1){ // Якщо натрапляємо на "*"
s1 = s.SubString(1, p - 1);
s2 = s.SubString(p+1, l-p);
h = new Mult(form(s1), form(s2)); // Створюємо вершину Mult.
}
else if ((p = PosFromEnd(s,"/")) > 1){ // Якщо натрапляємо на "/"
s1 =s.SubString(1, p-1);
s2 = s.SubString(p + 1,l - p);
h = new Div(form(s1), form(s2)); // Створюємо вершину Div
}
else if (s == "x") h = new Var(); // Створюємо вершину Var
else h = new Real(StrToFloat(s)); // Створюємо Real
return h;
}
//---------------------------------------------------------------------------
int PosFromEnd(AnsiString s, AnsiString sub){
int l = s.Length();
AnsiString p;
p.SetLength(l);
for(int i = 1;i<=l;i++)
p[ (l - i) + 1] = s[i]; // Обертаємо рядок
if (p.Pos(sub) > 0)
return l - p.Pos(sub) + 1;
else return 0;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Telement*x = form(Edit1->Text);
Telement* f = form(Edit2->Text);
Telement* f1 = f -> differ();
Telement* f2 = f1 -> differ();
double X = x -> rezult();
f -> set_var( X);
f1 -> set_var( X);
f2 -> set_var( X);
int precision = 15;
int digits = StrToFloat(ComboBox1->Text);
Edit3 -> Text = FloatToStrF( x -> rezult(), ffFixed, precision, digits);
Edit4 -> Text = FloatToStrF( f -> rezult(), ffFixed, precision, digits);
Edit5 -> Text = FloatToStrF( f1 -> rezult(), ffFixed, precision, digits);
Edit6 -> Text = FloatToStrF( f2 -> rezult(), ffFixed, precision, digits);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
Button1Click(Button1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if ((Key>='0')&&(Key<='9'))
return;
if ((Key=='x'))
return;
if ((Key=='+')||(Key=='*')||(Key=='-')||(Key=='/'))
return;
if (Key==VK_BACK)
return;
if (Key==VK_RETURN) {
if (((TEdit*)Sender)->Name == "Edit1") Edit2->SetFocus();
return;
}
Key = 0;
if ((Key == '.')||(Key == ',')) {
Key = DecimalSeparator;
if ( (((TEdit*)Sender)->Text).Pos(DecimalSeparator) != 0)
Key = 0; // разделитель уже введен
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit3KeyPress(TObject *Sender, char &Key)
{
Key = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
if ((Edit1->Text.Length() == 0 ) || (Edit2->Text.Length() == 0))
{
Button1->Enabled = false;
}
else
Button1->Enabled = true;
}
//--------------------------------------------------------------------------- |