MathgeomGLS

Форк
0
/
fCalculator.pas 
534 строки · 12.8 Кб
1
unit fCalculator;
2

3
interface
4

5
uses
6
  Winapi.Windows,
7
  Winapi.Messages,
8
  System.SysUtils,
9
  System.Math,
10
  System.Classes,
11
  Vcl.Graphics,
12
  Vcl.Controls,
13
  Vcl.Forms,
14
  Vcl.Dialogs,
15
  Vcl.StdCtrls,
16
  Vcl.ExtCtrls,
17
  Vcl.Imaging.Jpeg;
18

19
type
20
  TForm1 = class(TForm)
21
    Label1: TLabel;
22
    Label2: TLabel;
23
    BgImg: TImage;
24
    Img0: TImage;
25
    Img1: TImage;
26
    img4: TImage;
27
    img5: TImage;
28
    IMg2: TImage;
29
    img3: TImage;
30
    img6: TImage;
31
    img7: TImage;
32
    img8: TImage;
33
    img9: TImage;
34
    ImgPlus: TImage;
35
    ImgMin: TImage;
36
    ImgMult: TImage;
37
    ImgDiv: TImage;
38
    ImgEqual: TImage;
39
    ImgClose: TImage;
40
    ImgMinimize: TImage;
41
    PointImg: TImage;
42
    PiImg: TImage;
43
    InvImg: TImage;
44
    XyImg: TImage;
45
    ExpImg: TImage;
46
    SinImg: TImage;
47
    CosImg: TImage;
48
    TanImg: TImage;
49
    XinvImg: TImage;
50
    RootImg: TImage;
51
    X2Img: TImage;
52
    LogImg: TImage;
53
    LnImg: TImage;
54
    PmImg: TImage;
55
    McImg: TImage;
56
    MrImg: TImage;
57
    MpImg: TImage;
58
    CImg: TImage;
59
    AcImg: TImage;
60
    RanImg: TImage;
61
    Label3: TLabel;
62
    RoundImg: TImage;
63
    IntImg: TImage;
64
    FactImg: TImage;
65
    Label4: TLabel;
66
    procedure FormCreate(Sender: TObject);
67
    procedure BtnDivClick(Sender: TObject);
68
    Procedure Do_Operation;
69
    Procedure FindResult(Op: Byte);
70
    procedure Img0MouseDown(Sender: TObject; Button: TMouseButton;
71
      Shift: TShiftState; X, Y: Integer);
72
    procedure Img0MouseUp(Sender: TObject; Button: TMouseButton;
73
      Shift: TShiftState; X, Y: Integer);
74
    procedure Img0MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
75
    procedure ImgMinimizeClick(Sender: TObject);
76
    procedure ImgCloseClick(Sender: TObject);
77
    procedure BgImgMouseDown(Sender: TObject; Button: TMouseButton;
78
      Shift: TShiftState; X, Y: Integer);
79
  private
80
  public
81
  end;
82

83
var
84
  Form1: TForm1;
85
  numb1, numb2, result, mem1: Extended;
86
  Operation: Byte; // 1=addition, 2=subtr., 3=Multip., 4=div.
87
  comma, ok, Xy, Pressed: boolean;
88

89
implementation
90

91

92
{$R *.dfm}
93

94
procedure TForm1.Do_Operation;
95
begin
96
  if Operation = 0 then
97
  begin
98
    numb1 := StrToFloat(Label1.Caption);
99
  end
100
  else
101
  begin
102
    numb2 := StrToFloat(Label1.Caption);
103
    FindResult(Operation);
104
  end;
105
  ok := True;
106
  comma := False;
107
end;
108

109
procedure TForm1.FindResult(Op: Byte);
110
begin
111
  case Op of
112
    1:
113
      result := (numb1 + numb2);
114
    2:
115
      result := (numb1 - numb2);
116
    3:
117
      result := (numb1 * numb2);
118
    4:
119
      result := (numb1 / numb2);
120
  else
121
    exit;
122
  end;
123
  numb1 := result;
124
  Label1.Caption := FloatToStr(result);
125
  Operation := 0;
126
end;
127

128
procedure TForm1.FormCreate(Sender: TObject);
129
var
130
  i: Byte;
131
  Rgn: Thandle;
132
begin
133
  Rgn := CreateRoundRectRgn(0, 0, BgImg.width, BgImg.Height, 20, 20);
134
  SetWindowRgn(handle, Rgn, True);
135
  DeleteObject(Rgn);
136
  Label1.Caption := '0';
137
  numb1 := 0;
138
  numb2 := 0;
139
  mem1 := 0;
140
  comma := False;
141
  Xy := False;
142
  Operation := 0;
143
  result := 0;
144
  Pressed := False;
145
  for i := 0 to ComponentCount - 1 do
146
    if (Components[i] is TImage) then
147
      if (TImage(Components[i]).Tag < 1000) then
148
        TImage(Components[i]).Picture.Bitmap.LoadFromResourceID(hInstance,
149
          (TImage(Components[i]).Tag + 1) * 10);
150
end;
151

152
procedure TForm1.BtnDivClick(Sender: TObject);
153
begin
154
  if ok then
155
    exit;
156
  Do_Operation;
157
  Operation := 4;
158
end;
159

160
procedure TForm1.Img0MouseDown(Sender: TObject; Button: TMouseButton;
161
  Shift: TShiftState; X, Y: Integer);
162
begin
163
  if Button <> MBLeft then
164
    exit;
165
  TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
166
    (TImage(Sender).Tag + 1) * 10 + 1);
167
  Pressed := True;
168
end;
169

170
procedure TForm1.Img0MouseUp(Sender: TObject; Button: TMouseButton;
171
  Shift: TShiftState; X, Y: Integer);
172
var
173
  i, m: Extended;
174
begin
175
  if (Button <> MBLeft) or not Pressed then
176
    exit;
177
  TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
178
    (TImage(Sender).Tag + 1) * 10);
179
  case TImage(Sender).Tag of
180
    0 .. 9:
181
      begin // numbers 0..9
182
        if (Label1.Caption = '0') then
183
          Label1.Caption := '';
184
        if (ok and not comma) then
185
        begin
186
          Label1.Caption := '';
187
          ok := False;
188
        end;
189
        if Length(Label1.Caption) >= 15 then
190
          exit;
191
        Label1.Caption := Label1.Caption + IntToStr(TImage(Sender).Tag)
192
      end;
193
    10 .. 13:
194
      begin // opeartions +-*/
195
        if ok then
196
          exit;
197
        Do_Operation;
198
        Operation := TImage(Sender).Tag - 9;
199
      end;
200
    14:
201
      begin // equal =
202
        if (numb1 = 0) or (Operation = 0) and Not Xy then
203
          exit;
204
        numb2 := StrToFloat(Label1.Caption);
205
        if Operation <> 0 then
206
          FindResult(Operation)
207
        else if Xy then
208
        begin
209
          numb1 := Power(numb1, numb2);
210
          Label1.Caption := FloatToStr(numb1);
211
          Label2.Visible := False;
212
          Xy := False;
213
        end;
214
        comma := False;
215
        numb1 := StrToFloat(Label1.Caption);
216
        numb2 := 0;
217
        result := 0;
218
      end;
219
    15:
220
      begin // comma (point)
221
        if (Pos(',', Label1.Caption) > 0) and (Operation = 0) then
222
          exit;
223
        if not ok then
224
          Label1.Caption := Label1.Caption + ','
225
        else
226
          Label1.Caption := '0,';
227
        comma := True;
228
      end;
229
    16:
230
      begin // pi
231
        Label1.Caption := FloatToStr(pi);
232
        comma := True;
233
        ok := False;
234
      end;
235
    17:
236
      begin // inverse
237
        Label2.Caption := 'Inverse';
238
        Label2.Hint := 'Inverse function';
239
        if Xy then
240
        begin
241
          Label2.Visible := False;
242
          Xy := False;
243
          Label1.Caption := FloatToStr(numb1);
244
        end;
245
        Label2.Visible := not Label2.Visible;
246
      end;
247
    18:
248
      begin // Power Xy
249
        if (Label1.Caption = '0') and not Xy then
250
          exit;
251
        if not Xy then
252
        begin
253
          Label2.Caption := 'Power';
254
          Label2.Hint := 'Power function (x^y)';
255
          Label2.Visible := True;
256
          Xy := True;
257
          numb1 := StrToFloat(Label1.Caption);
258
          Label1.Caption := '0';
259
        end
260
        else
261
        begin
262
          Label2.Visible := False;
263
          Xy := False;
264
          Label1.Caption := FloatToStr(numb1);
265
        end;
266
        ok := False;
267
      end;
268
    19:
269
      begin // exponential E
270
        if Label1.Caption = '0' then
271
          Label1.Caption := '';
272
        If Operation = 0 then
273
        begin
274
          Label1.Caption := Label1.Caption + ('E');
275
          numb1 := StrToFloat(Label1.Caption);
276
        end
277
        else
278
        begin
279
          Label1.Caption := ('E');
280
          numb2 := StrToFloat(Label1.Caption);
281
        end;
282
      end;
283
    20:
284
      begin // sine and sine inverse
285
        if Label1.Caption = '' then
286
          exit;
287
        i := StrToFloat(Label1.Caption);
288
        if Label2.Visible and not Xy then
289
        begin
290
          if (i < -1) or (i > 1) then
291
            exit;
292
          i := ArcSin(i) * 180 / pi;
293
          Label2.Visible := False;
294
        end
295
        else
296
          i := sin(i * pi / 180);
297
        Label1.Caption := FloatToStr(i);
298
        ok := False;
299
      end;
300
    21:
301
      begin // cosine and cosine inverse
302
        if Label1.Caption = '' then
303
          exit;
304
        i := StrToFloat(Label1.Caption);
305
        if Label2.Visible and not Xy then
306
        begin
307
          if (i < -1) or (i > 1) then
308
            exit;
309
          i := ArcCos(i) * 180 / pi;
310
          Label2.Visible := False;
311
        end
312
        else
313
          i := cos(i * pi / 180);
314
        Label1.Caption := FloatToStr(i);
315
        ok := False;
316
      end;
317
    22:
318
      begin // tan and tan inverse
319
        if Label1.Caption = '' then
320
          exit;
321
        i := StrToFloat(Label1.Caption);
322
        if Label2.Visible and not Xy then
323
        begin
324
          i := Arctan(i) * 180 / pi;
325
          Label2.Visible := False;
326
        end
327
        else
328
          i := tan(i * pi / 180);
329
        Label1.Caption := FloatToStr(i);
330
        ok := False;
331
      end;
332
    23:
333
      begin // 1/x
334
        if Label1.Caption = '0' then
335
          exit;
336
        i := StrToFloat(Label1.Caption);
337
        i := (1 / i);
338
        Label1.Caption := FloatToStr(i);
339
        ok := False;
340
      end;
341
    24:
342
      begin // root
343
        if (Label1.Caption = '0') then
344
          exit
345
        else if (Copy(Label1.Caption, 1, 1) = '-') then
346
        begin
347
          messagebox(handle, 'Negative numbers are not allowed!',
348
            'Negative Numbers', MB_Ok);
349
          exit;
350
        end;
351
        i := StrToFloat(Label1.Caption);
352
        i := sqrt(i);
353
        Label1.Caption := FloatToStr(i);
354
      end;
355
    25:
356
      begin // x square
357
        if Label1.Caption = '0' then
358
          exit;
359
        i := StrToFloat(Label1.Caption);
360
        i := sqr(i);
361
        Label1.Caption := FloatToStr(i);
362
      end;
363
    26:
364
      begin // log and 10^(x)
365
        if Label1.Caption = '0' then
366
          exit;
367
        i := StrToFloat(Label1.Caption);
368
        if Label2.Visible then
369
        begin
370
          i := Power(10, i);
371
          Label2.Visible := False;
372
        end
373
        else
374
          i := Log10(i);
375
        Label1.Caption := FloatToStr(i);
376
        ok := False;
377
      end;
378
    27:
379
      begin // Ln and exp(x)
380
        if Label1.Caption = '0' then
381
          exit;
382
        i := StrToFloat(Label1.Caption);
383
        if Label2.Visible then
384
        begin
385
          i := exp(i);
386
          Label2.Visible := False;
387
        end
388
        else
389
          i := Ln(i);
390
        Label1.Caption := FloatToStr(i);
391
        ok := False;
392
      end;
393
    28:
394
      begin // +/-
395
        if Label1.Caption = '0' then
396
          exit;
397
        If Copy(Label1.Caption, Length(Label1.Caption), 1) = 'E' then
398
          Label1.Caption := Label1.Caption + '-'
399
        else if Copy(Label1.Caption, Length(Label1.Caption), 1) = '-' then
400
          Copy(Label1.Caption, 1, Length(Label1.Caption) - 1)
401
        else if Copy(Label1.Caption, 1, 1) = '-' then
402
          Label1.Caption := Copy(Label1.Caption, 2, Length(Label1.Caption))
403
        else
404
          Label1.Caption := '-' + Label1.Caption;
405
        If Operation = 0 then
406
          numb1 := StrToFloat(Label1.Caption);
407
      end;
408
    29:
409
      begin // Mc
410
        mem1 := 0;
411
        Label3.Visible := False;
412
      end;
413
    30:
414
      begin // MR
415
        Label1.Caption := FloatToStr(mem1);
416
        If Operation = 0 then
417
          numb1 := StrToFloat(Label1.Caption)
418
        else
419
          numb2 := StrToFloat(Label1.Caption);
420
      end;
421
    31:
422
      begin // M+
423
        mem1 := mem1 + StrToFloat(Label1.Caption);
424
        Label3.Visible := True;
425
      end;
426
    32:
427
      begin // C
428
        if Operation = 0 then
429
          numb1 := 0
430
        else
431
          numb2 := 0;
432
        Label1.Caption := '0';
433
      end;
434
    33:
435
      begin // AC
436
        numb1 := 0;
437
        numb2 := 0;
438
        result := 0;
439
        comma := False;
440
        ok := False;
441
        Operation := 0;
442
        Label1.Caption := '0';
443
      end;
444
    34:
445
      begin // Random number
446
        Randomize;
447
        i := (random(1000) + 1) / 1000;
448
        Label1.Caption := FloatToStr(i);
449
      end;
450
    35:
451
      Begin // Fictorial;
452
        if Pos(',', Label1.Caption) > 0 then
453
        begin
454
          Application.messagebox('Real numbers are not allowed!', 'Error',
455
            MB_Ok + MB_IconStop);
456
          ok := False;
457
          exit;
458
        end;
459
        if Label1.Caption = '0' then
460
        begin
461
          Label1.Caption := '1';
462
          exit;
463
        end;
464
        i := StrToFloat(Label1.Caption);
465
        if i > 60 then
466
        begin
467
          messagebox(handle, 'Large value!', 'Error', MB_Ok + MB_IconStop);
468
          ok := False;
469
          exit;
470
        end;
471
        m := i;
472
        Repeat
473
          m := m - 1;
474
          i := i * m;
475
        until m <= 1;
476
        Label1.Caption := FloatToStr(i);
477
      end;
478
    36:
479
      begin // Integer value
480
        i := StrToFloat(Label1.Caption);
481
        i := Int(i);
482
        Label1.Caption := FloatToStr(i);
483
        comma := False;
484
        ok := False;
485
      end;
486
    37:
487
      begin // Rounded to the nearest integer
488
        i := StrToFloat(Label1.Caption);
489
        i := Round(i);
490
        Label1.Caption := FloatToStr(i);
491
        comma := False;
492
        ok := False;
493
      end;
494
  end; // case
495
end;
496

497
procedure TForm1.Img0MouseMove(Sender: TObject; Shift: TShiftState;
498
  X, Y: Integer);
499
begin
500
  If Not(ssLeft in Shift) then
501
    exit;
502
  if (X <= 0) or (X >= (Sender as TImage).width) or (Y <= 0) or
503
    (Y >= (Sender as TImage).Height) then
504
  begin
505
    TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
506
      (TImage(Sender).Tag + 1) * 10);
507
    Pressed := False;
508
  end
509
  else
510
  begin
511
    TImage(Sender).Picture.Bitmap.LoadFromResourceID(hInstance,
512
      (TImage(Sender).Tag + 1) * 10 + 1);
513
    Pressed := True;
514
  end;
515
end;
516

517
procedure TForm1.ImgMinimizeClick(Sender: TObject);
518
begin
519
  Application.Minimize;
520
end;
521

522
procedure TForm1.ImgCloseClick(Sender: TObject);
523
begin
524
  Application.Terminate;
525
end;
526

527
procedure TForm1.BgImgMouseDown(Sender: TObject; Button: TMouseButton;
528
  Shift: TShiftState; X, Y: Integer);
529
begin
530
  ReleaseCapture;
531
  Form1.Perform(Wm_SysCommand, $F012, 0);
532
end;
533

534
end.
535

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.