-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCalcUnit.pas
More file actions
264 lines (233 loc) · 6.23 KB
/
MyCalcUnit.pas
File metadata and controls
264 lines (233 loc) · 6.23 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
unit MyCalcUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls;
resourcestring
String_overflow='溢出,不能计算这样的操作数';
type
TMyCalcForm = class(TForm)
GroupBox1: TGroupBox;
dec_sb: TSpeedButton;
Dec_Edit: TEdit;
hex_Edit: TEdit;
hex_sb: TSpeedButton;
Bin_Edit: TEdit;
bin_sb: TSpeedButton;
Num1_Edit: TEdit;
NUm2_Edit: TEdit;
Op_rg: TRadioGroup;
Label1: TLabel;
Label2: TLabel;
inc1_sb: TSpeedButton;
Label3: TLabel;
Result1_Edit: TEdit;
dec1_sb: TSpeedButton;
not1_sb: TSpeedButton;
AddDec1_sb: TSpeedButton;
adddec2_sb: TSpeedButton;
inc2_sb: TSpeedButton;
dec2_sb: TSpeedButton;
not2_sb: TSpeedButton;
calc_sb: TSpeedButton;
Label4: TLabel;
Label5: TLabel;
Result2_Edit: TEdit;
procedure dec_sbClick(Sender: TObject);
procedure hex_sbClick(Sender: TObject);
procedure bin_sbClick(Sender: TObject);
procedure calc_sbClick(Sender: TObject);
procedure inc1_sbClick(Sender: TObject);
procedure inc2_sbClick(Sender: TObject);
procedure dec1_sbClick(Sender: TObject);
procedure dec2_sbClick(Sender: TObject);
procedure not1_sbClick(Sender: TObject);
procedure not2_sbClick(Sender: TObject);
procedure AddDec1_sbClick(Sender: TObject);
procedure adddec2_sbClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MyCalcForm: TMyCalcForm;
implementation
{$R *.dfm}
function MyIntToBinStr(Value:Int64;Size: Integer): String;
var
i: Integer;
Tempint64:int64;
begin
Result:='';
Tempint64:=1;
for i:=Size downto 1 do begin
if Value and (Tempint64 shl (i-1))<>0 then begin ////use Tempint64 instead 1
Result:=Result+'1';
end else Result:=Result+'0';
end;
end;
function MyBinToInt(ValueStr:String):int64;
var
Len: Integer;
i:integer;
Tempint64:int64;
begin
Result:=0;
Tempint64:=1;
Len:=Length(ValueStr);
if Len>0 then
begin
for i:=Len Downto 1 do
begin
if (ValueStr[i]<>'1') and (ValueStr[i]<>'0') then Result:=0;
if ValueStr[i]='1' then
Result:=Result+(Tempint64 shl (Len-i)); ////use Tempint64 instead 1
end;
end;
end;
procedure TMyCalcForm.dec_sbClick(Sender: TObject);
var tempint64:int64;
Ecode:integer;
begin
val(Dec_edit.Text,tempint64,Ecode);
if Ecode=0 then
begin
Hex_edit.Text:=intToHex(tempint64,1);
Bin_edit.Text:=MyintToBinStr(tempint64,Length(Hex_edit.Text)*4);
end;
end;
procedure TMyCalcForm.hex_sbClick(Sender: TObject);
var tempint64:int64;
Ecode:integer;
begin
val('$'+hex_edit.Text,tempint64,Ecode);
if Ecode=0 then
begin
Dec_edit.Text:=intToStr(tempint64);
Bin_edit.Text:=MyintToBinStr(tempint64,Length(Hex_edit.Text)*4);
end;
end;
procedure TMyCalcForm.bin_sbClick(Sender: TObject);
var tempint64:int64;
begin
tempint64:=MyBinToInt(Bin_edit.Text);
Dec_edit.Text:=inttoStr(tempint64);
Hex_edit.Text:=inttoHex(tempint64,1);
end;
procedure TMyCalcForm.calc_sbClick(Sender: TObject);
var TempInt641:Int64;
Tempint642:int64;
TempDouble:Double;
Ecode:integer;
begin
val(Num1_Edit.text,TempInt641,Ecode);
if Ecode>0 then Exit;
val(Num2_edit.text,TempInt642,Ecode);
if Ecode>0 then Exit;
Try
case Op_rg.ItemIndex of
0: begin
Result1_edit.Text:=intToStr(TempInt641+TempInt642);
Result2_edit.Text:=intToHex(TempInt641+TempInt642,1);
end;
1: begin
Result1_edit.Text:=intToStr(TempInt641-TempInt642);
Result2_edit.Text:=intToHex(TempInt641-TempInt642,1);
end;
2: begin
Result1_edit.Text:=intToStr(TempInt641*TempInt642);
Result2_edit.Text:=intToHex(TempInt641*TempInt642,1);
end;
3: begin
Result1_edit.Text:=FloatToStr(TempInt641/TempInt642);
Result2_edit.Text:=intToHex(TempInt641 div TempInt642,1);
end;
4: begin
Result1_edit.Text:=intToStr(TempInt641 mod TempInt642);
Result2_edit.Text:=intToHex(TempInt641 mod TempInt642,1);
end;
5: begin
TempDouble:=TempInt641; //ln 只认浮点数
TempDouble:=Exp( Ln(TempDouble)* TempInt642 );
Result1_edit.Text:=FloatToStr( TempDouble );
Result2_edit.Text:=intToHex( Round(TempDouble),1 );
end;
end;
except
Showmessage(String_overflow);
end;
end;
procedure TMyCalcForm.inc1_sbClick(Sender: TObject);
var TempInt641:Int64;
Ecode:integer;
begin
val(Num1_Edit.text,TempInt641,Ecode);
if Ecode>0 then Exit;
inc(TempInt641);
Num1_Edit.text:=intToStr(TempInt641);
end;
procedure TMyCalcForm.inc2_sbClick(Sender: TObject);
var TempInt642:Int64;
Ecode:integer;
begin
val(Num2_Edit.text,TempInt642,Ecode);
if Ecode>0 then Exit;
inc(TempInt642);
Num2_Edit.text:=intToStr(TempInt642);
end;
procedure TMyCalcForm.dec1_sbClick(Sender: TObject);
var TempInt641:Int64;
Ecode:integer;
begin
val(Num1_Edit.text,TempInt641,Ecode);
if Ecode>0 then Exit;
Dec(TempInt641);
Num1_Edit.text:=intToStr(TempInt641);
end;
procedure TMyCalcForm.dec2_sbClick(Sender: TObject);
var TempInt642:Int64;
Ecode:integer;
begin
val(Num2_Edit.text,TempInt642,Ecode);
if Ecode>0 then Exit;
Dec(TempInt642);
Num2_Edit.text:=intToStr(TempInt642);
end;
procedure TMyCalcForm.not1_sbClick(Sender: TObject);
var TempInt641:Int64;
Ecode:integer;
begin
val(Num1_Edit.text,TempInt641,Ecode);
if Ecode>0 then Exit;
TempInt641:=$FFFFFFFF-TempInt641;
Num1_Edit.text:=intToStr(TempInt641);
end;
procedure TMyCalcForm.not2_sbClick(Sender: TObject);
var TempInt642:Int64;
Ecode:integer;
begin
val(Num2_Edit.text,TempInt642,Ecode);
if Ecode>0 then Exit;
TempInt642:=$FFFFFFFF-TempInt642;
Num2_Edit.text:=intToStr(TempInt642);
end;
procedure TMyCalcForm.AddDec1_sbClick(Sender: TObject);
var TempInt641:Int64;
Ecode:integer;
begin
val(Num1_Edit.text,TempInt641,Ecode);
if Ecode>0 then Exit;
TempInt641:=0-TempInt641;
Num1_Edit.text:=intToStr(TempInt641);
end;
procedure TMyCalcForm.adddec2_sbClick(Sender: TObject);
var TempInt642:Int64;
Ecode:integer;
begin
val(Num2_Edit.text,TempInt642,Ecode);
if Ecode>0 then Exit;
TempInt642:=0-TempInt642;
Num2_Edit.text:=intToStr(TempInt642);
end;
end.