Last week we learned to set Android/ios and write the first Hello World, and we're going to write the first app easy calculator today. After adding a blank app, we added an edit element to the screen and set the align to top. "--" Then change the horzalign of the edit textsettings to trailing so that the text is aligned on the right. Then we start to set the button of the calculator, we use the buttons element, change the Text property and pull to the appropriate position. After we've all set up, we can start writing the program. There are probably a few parts to the program ~
- Digital buttons (press 0~9 and decimal)
- Operator Management (press + 、-、 *,/)
- Calculate results (press =)
- Other parts (press C 0, press +/-, and%)
Let's deal with it in one by one. Digital buttons We first declare three variables in the program, NUM1, num2 the first to second time the number of inputs, r to calculate the results, type is extended. Also a ioperator integer, which is used to record the operators (1 tables +, 2 tables-, 3 tables *, 4 tables/). Varform1:tform1;num1,num2,r:extended;ioperator:integer;
And then we deal with the numbers and the small number of events that are pressed, and enter the following code on the button 0 double click Two. If the display is 0 or not by the number of words, directly display the number of input, if not, the screen will be displayed on the numbers added. Procedure Tform1.button1click (sender:tobject); Begin if (Edit1.text = ' 0 ') and (TButton (Sender). Text<> '. ') Then edit1.text: = TButton (Sender). Text Else Edit1.text: = Edit1.text + TButton (Sender). Text;end;
After writing, we will order the other 1-9 and small button onclick events to Button1Click. Because in the event that we just wrote, it was through TButton (Sender). Text to judge the value of the button, and different buttons to the sender of different can do their own judgment! Operators With the addition of the multiplication part, we also enter the following code at the + button two. When pressed, the values displayed on the current screen are recorded to the NUM1, and then the +-*/is given ioperator different values, and the screen is finally cleared to 0. Procedure Tform1.button5click (sender:tobject); begin Num1: = Strtofloat (edit1.text); If TButton (Sender). Text = ' + ' then ioperator: = 1 else if TButton (Sender). Text = '-' then ioperator: = 2 else if TButton (Sender). Text = ' X ' then ioperator: = 3 else if TButton (Sender). Text = '/' then ioperator: = 4 Else ioperator: = 0; Edit1.text: = ' 0 '; end;
After writing the same as the previous step, the-*/button is assigned to Button5click. Calculate the results of the management At the end of the result = button, enter the following code after the = button double-click Two. Procedure Tform1.button7click (sender:tobject); begin If Ioperator >0 then begin Num2: = Strtofloat ( Edit1.text); Case Ioperator of 1:r: = Num1 + num2; 2:r: = num1-num2; 3:r: = Num1 * NUM2; 4:r: = num1/num2; End; Edit1.text: = Floattostr (R); End;end;
Other parts
- 0 – Empty the screen to zero
- Positive negative value – multiply the number by minus 1
- Percent – Divide the numbers by 100
Results Finally, we can see the results of the program. Completed the first time we supported the Android, IOS, Wind32 computer app. Download the original Https://github.com/superlevin/XE6calculator |