Delphi is a successor of a Pascal language.It has all the features that is needed for a development of a good software.
So through this post I want to prove that point.
If u have a basic knowledge of a Delphi language u can make calculator similar to windows within a hour so starting.
Just add button and editbox to the form by drag and drop of the button to the frame then write the event handler that performs the action when that button is clicked.
The algorithm is simple
- When user clicks the button of number i.e(1-9) store it on variable(first stack)
here input[temp] is a string and temp is a variable
temp:=0;
input[temp]:='1'
if again press 2
input[temp]:=input[temp]+'2'; //here temp=0;
- Now when user presses arithmetic operation button(i.e. +,-,*,/) we increase the stack by 1 and store it to value of button to stack
temp:= temp +1; //temp becomes 1;
if user press multiply button
input[temp]:='*' //here temp=1;
Again stack is incremented by 1 so that other number for calculation can be store
i.e.
++temp; //temp becomes 2;
- After that when user press other button of variable(1-9) it is stored in third stack
if user press 2
input[temp]:='2'; //here temp=2;
- Finally when user enters the equal to button all the string is converted to number and the output is calculated and the stack value is reset and point to intial value
Lets see at the code
unit Calculator;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Display: TEdit;
One: TButton;
Four: TButton;
Two: TButton;
Five: TButton;
Eight: TButton;
Three: TButton;
Six: TButton;
Nine: TButton;
Plusminus: TButton;
Divide: TButton;
Multiply: TButton;
Backspace: TButton;
Zero: TButton;
Decimal: TButton;
CE: TButton;
C: TButton;
Minus: TButton;
Plus: TButton;
Sqrt: TButton;
Percentage: TButton;
Reciprocal: TButton;
Equal: TButton;
MC: TButton;
MR: TButton;
MS: TButton;
Mplus: TButton;
Mminus: TButton;
Seven: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure DisplayKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure ButtonClick(Sender: TObject);
private
{ Private declarations }
procedure Calculate;//(sign:String) ;
procedure Add;
procedure Subtract;
procedure Multiplynum;
procedure Dividenum;
procedure Modulus;
procedure Reset() ;
procedure main(value:Integer);
public
{ Public declarations }
end;
var
Form2: TForm2;
input: Array[0..3] of string; // Static array, size = 3
temp: Integer = 0; //defined temporary variable to keep track of the input given
output: Single; //for storing output as output can be in float value
total: Single;
sign: Integer;
// value: Integer;
Sender:TObject;
implementation
{$R *.dfm}
//this procedure is for handling mouse clicks
procedure TForm2.ButtonClick(Sender: TObject);
begin
if Sender = Zero then
begin
Display.Text:=Display.Text+Zero.Caption;
input[temp]:=input[temp]+'0';
end
else if Sender = One then
begin
Display.Text:=Display.Text+One.Caption;
input[temp]:=input[temp]+'1';
end
else if Sender = Two then
begin
Display.Text:=Display.Text+Two.Caption;
input[temp]:=input[temp]+'2';
end
else if Sender = Three then
begin
Display.Text:=Display.Text+Three.Caption;
input[temp]:=input[temp]+'3';
end
else if Sender = Four then
begin
Display.Text:=Display.Text+Four.Caption;
input[temp]:=input[temp]+'4';
end
else if Sender = Five then
begin
Display.Text:=Display.Text+Five.Caption;
input[temp]:=input[temp]+'5';
end
else if Sender = Six then
begin
Display.Text:=Display.Text+Six.Caption;
input[temp]:=input[temp]+'6';
end
else if Sender = Seven then
begin
Display.Text:=Display.Text+Seven.Caption;
input[temp]:=input[temp]+'7';
end
else if Sender = Eight then
begin
Display.Text:=Display.Text+Eight.Caption;
input[temp]:=input[temp]+'8';
end
else if Sender = Nine then
begin
Display.Text:=Display.Text+Nine.Caption;
input[temp]:=input[temp]+'9';
end
else if Sender = Decimal then
begin
Display.Text:=Display.Text+Decimal.Caption;
input[temp]:=input[temp]+'.';
end
else if Sender = Plus then
begin
main(1);
Display.Text := Display.text + Plus.Caption;
end
else if Sender = Minus then
begin
main(2);
Display.Text := Display.text + Minus.Caption;
end
else if Sender = Multiply then
begin
main(3);
Display.Text := Display.text + Multiply.Caption;
end
else if Sender = Divide then
begin
main(4);
Display.Text := Display.text + Divide.Caption;
end
else if Sender = Percentage then
begin
Display.Text := Display.text + Percentage.Caption;
sign:=5;
temp:=temp+1;
end
else if Sender = Reciprocal then
begin
Display.Text := One.Caption+Divide.Caption+Display.text ;
output:= 1/StrToFloat(input[temp]);
Edit1.Text :=FloatToStr(output);
end
else if Sender = Mplus then
begin
total:=total+output;
Display.Text:='M+ ';
Reset();
end
else if Sender = Mminus then
begin
total:=total-output;
Display.Text:='M- ';
Reset();
end
else if Sender = MR then
Edit1.Text := FloatToStr(total)
else if Sender = MC then
begin
Display.Text:='';
Edit1.Text := '';
total:=0;
end
else if Sender = C then
begin
Display.Text := '';
Edit1.Text := '';
Reset();
end
else if Sender = CE then
begin
Display.Text := '';
input[temp]:='';
end
else if Sender = Equal then
begin
Calculate();
Edit1.Text := FloatToStr(output)
end
else
ShowMessage('??? clicked!') ;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
Display.Text := '';
end;
procedure Tform2.Reset() ;
begin
//Display.Text := '';
input[0]:='';
input[1]:='';
output:=0;
sign:=0;
temp:=0;
end;
{this prcedure is for keyboard handling}
procedure TForm2.DisplayKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_NUMPAD0: ButtonClick(Zero);
VK_NUMPAD1: ButtonClick(One);
VK_NUMPAD2: ButtonClick(Two);
VK_NUMPAD3: ButtonClick(Three);
VK_NUMPAD4: ButtonClick(Four);
VK_NUMPAD5: ButtonClick(Five);
VK_NUMPAD6: ButtonClick(Six);
VK_NUMPAD7: ButtonClick(Seven);
VK_NUMPAD8: ButtonClick(Eight);
VK_NUMPAD9: ButtonClick(Nine);
VK_MULTIPLY: ButtonClick(Multiply);
VK_ADD: ButtonClick(Plus);
VK_SUBTRACT: ButtonClick(Minus);
VK_DECIMAL: ButtonClick(Decimal);
VK_DIVIDE: ButtonClick(Divide);
VK_RETURN: ButtonClick(Equal);
end; //end of case
end; //end of procedure
procedure TForm2.main(value:Integer);
begin
if(temp>=1) then
begin
Calculate();
//Display.Text:=FloatToStr(output);
Edit1.Text := FloatToStr(output);
sign:=value;
input[0]:=FloatToStr(output);
input[1]:='';
temp:=1
end
else
begin
temp:=temp+1;
sign:=value;
end ;
end;
procedure TForm2.Add();
begin
output:=StrToFloat(input[0])+StrToFloat(input[1]);
end;
procedure TForm2.Subtract();
begin
output:=StrToFloat(input[0])- StrToFloat(input[1]);
//ShowMessage(FloatToStr(output));
end;
procedure TForm2.Multiplynum();
begin
output:=StrToFloat(input[0]) * StrToFloat(input[1]);
end;
procedure TForm2.Dividenum();
begin
output:=StrToFloat(input[0]) / StrToFloat(input[1]);
end;
procedure TForm2.Modulus();
begin
output:=StrToFloat(input[0]) * StrToFloat(input[1])/100;
end;
procedure Tform2.Calculate() ;
begin
case sign of
0: ShowMessage('What to do');
1: Add();
2: Subtract();
3: Multiplynum();
4: Dividenum();
5: Modulus();
end;
end;
end. // end of unit
0 comments:
Post a Comment