Wanted to show 2 patterns on calc example, but calc itself has become not simple task…
Below version that works correctly but there is things to do…
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 |
unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses, uniGUIClasses, uniGUIRegClasses, uniGUIForm, uniEdit, uniGUIBaseClasses, uniButton, uniBitBtn, uniLabel; type TMainForm = class(TUniForm) b8: TUniBitBtn; b9: TUniBitBtn; b4: TUniBitBtn; b5: TUniBitBtn; b6: TUniBitBtn; bOne: TUniBitBtn; b2: TUniBitBtn; b3: TUniBitBtn; eResult: TUniEdit; UniBitBtn10: TUniBitBtn; bEqual: TUniBitBtn; bDivide: TUniBitBtn; bMultiply: TUniBitBtn; bMinus: TUniBitBtn; bPlus: TUniBitBtn; bCE: TUniBitBtn; b7: TUniBitBtn; unilabel: TUniLabel; procedure UniFormCreate(Sender: TObject); procedure digitClick(Sender: TObject); procedure signClick(Sender: TObject); procedure bEqualClick(Sender: TObject); procedure bCEClick(Sender: TObject); private { Private declarations } faIsEmpty: Boolean; fbIsEmpty: Boolean; fcIsEmpty: Boolean; fdIsEmpty: Boolean; fIsSignClickedBefore: Boolean; fIsDigitClicked: Boolean; fa, fb, fc: real; fd: char; foperators: set of Char; procedure nullAll; public { Public declarations } end; function MainForm: TMainForm; implementation {$R *.dfm} uses uniGUIVars, MainModule, uniGUIApplication, System.Generics.Collections; function MainForm: TMainForm; begin Result := TMainForm(UniMainModule.GetFormInstance(TMainForm)); end; procedure TMainForm.bCEClick(Sender: TObject); begin nullAll(); end; procedure TMainForm.bEqualClick(Sender: TObject); begin case fd of '-': fc := fa - fb; '+': fc := fa + fb; '*': fc := fa * fb; '/': fc := fa / fb; end; fa := fc; fbIsEmpty := True; eResult.Text := FloatToStr(fc); end; procedure TMainForm.nullAll; begin fa := 0.00; fb := 0.00; fc := 0.00; unilabel.Caption := ''; faIsEmpty := true; fbIsEmpty := true; fcIsEmpty := true; fdIsEmpty := true; foperators := ['+', '-', '*', '/']; unilabel.Caption := ''; eResult.Text := ''; end; procedure TMainForm.digitClick(Sender: TObject); begin fIsSignClickedBefore := false; fIsDigitClicked := True; if faIsEmpty then begin fa := strToFloat((Sender as TUniBitbtn).Caption); faIsEmpty := false; end else begin fb := strToFloat((Sender as TUniBitbtn).Caption); fbIsEmpty := False; end; unilabel.Caption := unilabel.Caption + (Sender as TUniBitbtn).Caption; end; procedure TMainForm.signClick(Sender: TObject); var s: string; lastSymbol:char; begin fdIsEmpty := false; if fIsSignClickedBefore then begin s := unilabel.Caption; lastSymbol:=s[Length(s)]; if ((s[Length(s)] in foperators) and ((lastSymbol <> (Sender as TUniBitbtn).Caption))) then begin delete(s, length(s), 1); fdIsEmpty := true; unilabel.Caption := s; //fIsSignClickedBefore := false; end else if (s[Length(s)] = (Sender as TUniBitbtn).Caption) then exit; end; unilabel.Caption := unilabel.Caption + (Sender as TUniBitbtn).Caption; s := (Sender as TUniBitBtn).Caption; fd := s[Low(s)]; if fdIsEmpty then exit; if not fIsDigitClicked and fIsSignClickedBefore then exit; fIsSignClickedBefore := true; if (not faIsEmpty) and (not fbIsEmpty) then begin case fd of '-': fc := fa - fb; '+': fc := fa + fb; '*': fc := fa * fb; '/': fc := fa / fb; end; fa := fc; eResult.Text := FloatToStr(fc); fIsDigitClicked := false; end; end; procedure TMainForm.UniFormCreate(Sender: TObject); begin nullAll(); end; initialization RegisterAppFormClass(TMainForm); end. |