MathgeomGLS
534 строки · 12.8 Кб
1unit fCalculator;
2
3interface
4
5uses
6Winapi.Windows,
7Winapi.Messages,
8System.SysUtils,
9System.Math,
10System.Classes,
11Vcl.Graphics,
12Vcl.Controls,
13Vcl.Forms,
14Vcl.Dialogs,
15Vcl.StdCtrls,
16Vcl.ExtCtrls,
17Vcl.Imaging.Jpeg;
18
19type
20TForm1 = class(TForm)
21Label1: TLabel;
22Label2: TLabel;
23BgImg: TImage;
24Img0: TImage;
25Img1: TImage;
26img4: TImage;
27img5: TImage;
28IMg2: TImage;
29img3: TImage;
30img6: TImage;
31img7: TImage;
32img8: TImage;
33img9: TImage;
34ImgPlus: TImage;
35ImgMin: TImage;
36ImgMult: TImage;
37ImgDiv: TImage;
38ImgEqual: TImage;
39ImgClose: TImage;
40ImgMinimize: TImage;
41PointImg: TImage;
42PiImg: TImage;
43InvImg: TImage;
44XyImg: TImage;
45ExpImg: TImage;
46SinImg: TImage;
47CosImg: TImage;
48TanImg: TImage;
49XinvImg: TImage;
50RootImg: TImage;
51X2Img: TImage;
52LogImg: TImage;
53LnImg: TImage;
54PmImg: TImage;
55McImg: TImage;
56MrImg: TImage;
57MpImg: TImage;
58CImg: TImage;
59AcImg: TImage;
60RanImg: TImage;
61Label3: TLabel;
62RoundImg: TImage;
63IntImg: TImage;
64FactImg: TImage;
65Label4: TLabel;
66procedure FormCreate(Sender: TObject);
67procedure BtnDivClick(Sender: TObject);
68Procedure Do_Operation;
69Procedure FindResult(Op: Byte);
70procedure Img0MouseDown(Sender: TObject; Button: TMouseButton;
71Shift: TShiftState; X, Y: Integer);
72procedure Img0MouseUp(Sender: TObject; Button: TMouseButton;
73Shift: TShiftState; X, Y: Integer);
74procedure Img0MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
75procedure ImgMinimizeClick(Sender: TObject);
76procedure ImgCloseClick(Sender: TObject);
77procedure BgImgMouseDown(Sender: TObject; Button: TMouseButton;
78Shift: TShiftState; X, Y: Integer);
79private
80public
81end;
82
83var
84Form1: TForm1;
85numb1, numb2, result, mem1: Extended;
86Operation: Byte; // 1=addition, 2=subtr., 3=Multip., 4=div.
87comma, ok, Xy, Pressed: boolean;
88
89implementation
90
91
92{$R *.dfm}
93
94procedure TForm1.Do_Operation;
95begin
96if Operation = 0 then
97begin
98numb1 := StrToFloat(Label1.Caption);
99end
100else
101begin
102numb2 := StrToFloat(Label1.Caption);
103FindResult(Operation);
104end;
105ok := True;
106comma := False;
107end;
108
109procedure TForm1.FindResult(Op: Byte);
110begin
111case Op of
1121:
113result := (numb1 + numb2);
1142:
115result := (numb1 - numb2);
1163:
117result := (numb1 * numb2);
1184:
119result := (numb1 / numb2);
120else
121exit;
122end;
123numb1 := result;
124Label1.Caption := FloatToStr(result);
125Operation := 0;
126end;
127
128procedure TForm1.FormCreate(Sender: TObject);
129var
130i: Byte;
131Rgn: Thandle;
132begin
133Rgn := CreateRoundRectRgn(0, 0, BgImg.width, BgImg.Height, 20, 20);
134SetWindowRgn(handle, Rgn, True);
135DeleteObject(Rgn);
136Label1.Caption := '0';
137numb1 := 0;
138numb2 := 0;
139mem1 := 0;
140comma := False;
141Xy := False;
142Operation := 0;
143result := 0;
144Pressed := False;
145for i := 0 to ComponentCount - 1 do
146if (Components[i] is TImage) then
147if (TImage(Components[i]).Tag < 1000) then
148TImage(Components[i]).Picture.Bitmap.LoadFromResourceID(hInstance,
149(TImage(Components[i]).Tag + 1) * 10);
150end;
151
152procedure TForm1.BtnDivClick(Sender: TObject);
153begin
154if ok then
155exit;
156Do_Operation;
157Operation := 4;
158end;
159
160procedure TForm1.Img0MouseDown(Sender: TObject; Button: TMouseButton;
161Shift: TShiftState; X, Y: Integer);
162begin
163if Button <> MBLeft then
164exit;
165TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
166(TImage(Sender).Tag + 1) * 10 + 1);
167Pressed := True;
168end;
169
170procedure TForm1.Img0MouseUp(Sender: TObject; Button: TMouseButton;
171Shift: TShiftState; X, Y: Integer);
172var
173i, m: Extended;
174begin
175if (Button <> MBLeft) or not Pressed then
176exit;
177TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
178(TImage(Sender).Tag + 1) * 10);
179case TImage(Sender).Tag of
1800 .. 9:
181begin // numbers 0..9
182if (Label1.Caption = '0') then
183Label1.Caption := '';
184if (ok and not comma) then
185begin
186Label1.Caption := '';
187ok := False;
188end;
189if Length(Label1.Caption) >= 15 then
190exit;
191Label1.Caption := Label1.Caption + IntToStr(TImage(Sender).Tag)
192end;
19310 .. 13:
194begin // opeartions +-*/
195if ok then
196exit;
197Do_Operation;
198Operation := TImage(Sender).Tag - 9;
199end;
20014:
201begin // equal =
202if (numb1 = 0) or (Operation = 0) and Not Xy then
203exit;
204numb2 := StrToFloat(Label1.Caption);
205if Operation <> 0 then
206FindResult(Operation)
207else if Xy then
208begin
209numb1 := Power(numb1, numb2);
210Label1.Caption := FloatToStr(numb1);
211Label2.Visible := False;
212Xy := False;
213end;
214comma := False;
215numb1 := StrToFloat(Label1.Caption);
216numb2 := 0;
217result := 0;
218end;
21915:
220begin // comma (point)
221if (Pos(',', Label1.Caption) > 0) and (Operation = 0) then
222exit;
223if not ok then
224Label1.Caption := Label1.Caption + ','
225else
226Label1.Caption := '0,';
227comma := True;
228end;
22916:
230begin // pi
231Label1.Caption := FloatToStr(pi);
232comma := True;
233ok := False;
234end;
23517:
236begin // inverse
237Label2.Caption := 'Inverse';
238Label2.Hint := 'Inverse function';
239if Xy then
240begin
241Label2.Visible := False;
242Xy := False;
243Label1.Caption := FloatToStr(numb1);
244end;
245Label2.Visible := not Label2.Visible;
246end;
24718:
248begin // Power Xy
249if (Label1.Caption = '0') and not Xy then
250exit;
251if not Xy then
252begin
253Label2.Caption := 'Power';
254Label2.Hint := 'Power function (x^y)';
255Label2.Visible := True;
256Xy := True;
257numb1 := StrToFloat(Label1.Caption);
258Label1.Caption := '0';
259end
260else
261begin
262Label2.Visible := False;
263Xy := False;
264Label1.Caption := FloatToStr(numb1);
265end;
266ok := False;
267end;
26819:
269begin // exponential E
270if Label1.Caption = '0' then
271Label1.Caption := '';
272If Operation = 0 then
273begin
274Label1.Caption := Label1.Caption + ('E');
275numb1 := StrToFloat(Label1.Caption);
276end
277else
278begin
279Label1.Caption := ('E');
280numb2 := StrToFloat(Label1.Caption);
281end;
282end;
28320:
284begin // sine and sine inverse
285if Label1.Caption = '' then
286exit;
287i := StrToFloat(Label1.Caption);
288if Label2.Visible and not Xy then
289begin
290if (i < -1) or (i > 1) then
291exit;
292i := ArcSin(i) * 180 / pi;
293Label2.Visible := False;
294end
295else
296i := sin(i * pi / 180);
297Label1.Caption := FloatToStr(i);
298ok := False;
299end;
30021:
301begin // cosine and cosine inverse
302if Label1.Caption = '' then
303exit;
304i := StrToFloat(Label1.Caption);
305if Label2.Visible and not Xy then
306begin
307if (i < -1) or (i > 1) then
308exit;
309i := ArcCos(i) * 180 / pi;
310Label2.Visible := False;
311end
312else
313i := cos(i * pi / 180);
314Label1.Caption := FloatToStr(i);
315ok := False;
316end;
31722:
318begin // tan and tan inverse
319if Label1.Caption = '' then
320exit;
321i := StrToFloat(Label1.Caption);
322if Label2.Visible and not Xy then
323begin
324i := Arctan(i) * 180 / pi;
325Label2.Visible := False;
326end
327else
328i := tan(i * pi / 180);
329Label1.Caption := FloatToStr(i);
330ok := False;
331end;
33223:
333begin // 1/x
334if Label1.Caption = '0' then
335exit;
336i := StrToFloat(Label1.Caption);
337i := (1 / i);
338Label1.Caption := FloatToStr(i);
339ok := False;
340end;
34124:
342begin // root
343if (Label1.Caption = '0') then
344exit
345else if (Copy(Label1.Caption, 1, 1) = '-') then
346begin
347messagebox(handle, 'Negative numbers are not allowed!',
348'Negative Numbers', MB_Ok);
349exit;
350end;
351i := StrToFloat(Label1.Caption);
352i := sqrt(i);
353Label1.Caption := FloatToStr(i);
354end;
35525:
356begin // x square
357if Label1.Caption = '0' then
358exit;
359i := StrToFloat(Label1.Caption);
360i := sqr(i);
361Label1.Caption := FloatToStr(i);
362end;
36326:
364begin // log and 10^(x)
365if Label1.Caption = '0' then
366exit;
367i := StrToFloat(Label1.Caption);
368if Label2.Visible then
369begin
370i := Power(10, i);
371Label2.Visible := False;
372end
373else
374i := Log10(i);
375Label1.Caption := FloatToStr(i);
376ok := False;
377end;
37827:
379begin // Ln and exp(x)
380if Label1.Caption = '0' then
381exit;
382i := StrToFloat(Label1.Caption);
383if Label2.Visible then
384begin
385i := exp(i);
386Label2.Visible := False;
387end
388else
389i := Ln(i);
390Label1.Caption := FloatToStr(i);
391ok := False;
392end;
39328:
394begin // +/-
395if Label1.Caption = '0' then
396exit;
397If Copy(Label1.Caption, Length(Label1.Caption), 1) = 'E' then
398Label1.Caption := Label1.Caption + '-'
399else if Copy(Label1.Caption, Length(Label1.Caption), 1) = '-' then
400Copy(Label1.Caption, 1, Length(Label1.Caption) - 1)
401else if Copy(Label1.Caption, 1, 1) = '-' then
402Label1.Caption := Copy(Label1.Caption, 2, Length(Label1.Caption))
403else
404Label1.Caption := '-' + Label1.Caption;
405If Operation = 0 then
406numb1 := StrToFloat(Label1.Caption);
407end;
40829:
409begin // Mc
410mem1 := 0;
411Label3.Visible := False;
412end;
41330:
414begin // MR
415Label1.Caption := FloatToStr(mem1);
416If Operation = 0 then
417numb1 := StrToFloat(Label1.Caption)
418else
419numb2 := StrToFloat(Label1.Caption);
420end;
42131:
422begin // M+
423mem1 := mem1 + StrToFloat(Label1.Caption);
424Label3.Visible := True;
425end;
42632:
427begin // C
428if Operation = 0 then
429numb1 := 0
430else
431numb2 := 0;
432Label1.Caption := '0';
433end;
43433:
435begin // AC
436numb1 := 0;
437numb2 := 0;
438result := 0;
439comma := False;
440ok := False;
441Operation := 0;
442Label1.Caption := '0';
443end;
44434:
445begin // Random number
446Randomize;
447i := (random(1000) + 1) / 1000;
448Label1.Caption := FloatToStr(i);
449end;
45035:
451Begin // Fictorial;
452if Pos(',', Label1.Caption) > 0 then
453begin
454Application.messagebox('Real numbers are not allowed!', 'Error',
455MB_Ok + MB_IconStop);
456ok := False;
457exit;
458end;
459if Label1.Caption = '0' then
460begin
461Label1.Caption := '1';
462exit;
463end;
464i := StrToFloat(Label1.Caption);
465if i > 60 then
466begin
467messagebox(handle, 'Large value!', 'Error', MB_Ok + MB_IconStop);
468ok := False;
469exit;
470end;
471m := i;
472Repeat
473m := m - 1;
474i := i * m;
475until m <= 1;
476Label1.Caption := FloatToStr(i);
477end;
47836:
479begin // Integer value
480i := StrToFloat(Label1.Caption);
481i := Int(i);
482Label1.Caption := FloatToStr(i);
483comma := False;
484ok := False;
485end;
48637:
487begin // Rounded to the nearest integer
488i := StrToFloat(Label1.Caption);
489i := Round(i);
490Label1.Caption := FloatToStr(i);
491comma := False;
492ok := False;
493end;
494end; // case
495end;
496
497procedure TForm1.Img0MouseMove(Sender: TObject; Shift: TShiftState;
498X, Y: Integer);
499begin
500If Not(ssLeft in Shift) then
501exit;
502if (X <= 0) or (X >= (Sender as TImage).width) or (Y <= 0) or
503(Y >= (Sender as TImage).Height) then
504begin
505TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
506(TImage(Sender).Tag + 1) * 10);
507Pressed := False;
508end
509else
510begin
511TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
512(TImage(Sender).Tag + 1) * 10 + 1);
513Pressed := True;
514end;
515end;
516
517procedure TForm1.ImgMinimizeClick(Sender: TObject);
518begin
519Application.Minimize;
520end;
521
522procedure TForm1.ImgCloseClick(Sender: TObject);
523begin
524Application.Terminate;
525end;
526
527procedure TForm1.BgImgMouseDown(Sender: TObject; Button: TMouseButton;
528Shift: TShiftState; X, Y: Integer);
529begin
530ReleaseCapture;
531Form1.Perform(Wm_SysCommand, $F012, 0);
532end;
533
534end.
535