• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

How to make a calculator?

I want to create a calculator. How can I make a Window where the numbers can be printed. I've tried, but when i press at t.ex. '1' then '2' then '3' it prints 321 ! I want it to print 123 as a regular calculator.
Suggestions?
0
Monsemann
Asked:
Monsemann
  • 4
  • 3
  • 3
  • +4
1 Solution
 
LischkeCommented:
Hu? What window do you use? If you have a normal TEdit then the numbers appear in the same order as they are typed.

You can also use a string to hold all input of the user by adding each new character to the end of the string. Then use either your own output function or assign the string to a control which can display it (e.g. TLabel.Caption).

Ciao, Mike
0
 
men xinCEOCommented:
what?i can't understand your means?;-(
0
 
intheCommented:
yep
 one of my first projetcs was to write calculator and i used a tedit.
i remember this was also a question of mine when i was new to delphi using buttons for the numbers.
if i remember right just do on the  button click:


Edit1.Text:=Edit1.Text + 1  (<- the number)
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
BatalfCommented:
Hi pall!

Do you use an edit-component as the display of your calculator?

In that case, I assume that your code when you press a button is :

edit1.text := edit1.text + button1.caption;

Have you tried this instead :
edit1.text:=button1.caption+edit1.text;

Regards
Batalf
0
 
gallagheCommented:
Unless you need special functionality, why not use a prebuilt calculator. For instance the freeware library called RX has a calculator with source code. As is you can not use it w/o RX. What I did was fixed it so that it works outside of RX.

If this is a possiblity I can email u the RX calc which I cut loose from the RX library.

Let me know

kevin.s.gallagher@state.or.us
0
 
intheCommented:
if you are new to delphi you can go to places like here:

http://www.torry.ru/math.htm

and get freeware components to either install or read the source code of to see how some stuff is done.


0
 
BatalfCommented:
Some additional comments+ a correction :

Do you use an edit-component as the display of your calculator?

In that case, I assume that your code when you press a button is :

ex : for the button "1" :

when you click it, you have this code :

edit1.text:='1'+edit1.text;


Have you tried this instead :

edit1.text := edit1.text + '1'

If you do this, the number you click would be placed at the eand of the numbers in the display instead of at the front(eks : 33+1 = 331, instead of 133)

Regards
Batalf
0
 
intheCommented:
um didnt i just say that.. ?
0
 
BatalfCommented:
To Inthe

Sorry

I didn't read what you typed.

My last comment was just a correction of my first, which I wrote when you probably was writing your comment.

I also thought that some explanation could be useful.



0
 
aubsCommented:
If you use the button caption like Batalf mentioned you could use code like this:

procedure TForm1.ButtonClick(Sender: TObject);
begin
  edit1.text:= edit1.text + (sender as TButton).caption;
end;

Then make all the click events for the buttons  '.', 0, 1, 2... 9 call this procedure.
0
 
BatalfCommented:
My comment above
0
 
MonsemannAuthor Commented:
Thank You!
You're an excellent expert!

Monsemann
0
 
LischkeCommented:
Strange decision, I as well as Barry have told the same stuff before Batalf, and you haven't shown any reaction....
0
 
MonsemannAuthor Commented:
To 'Lischke'

When I checked the answers to my question today, were all these comments there already. In other words: I recieved them all at the same time!
I had to choose one of the answers.
I don't think You understood my problem (or maybe I didn't understand the answer...). Therfore I rejected Your comment. Then I tryed 'inthe's answer. It didn't work!
'Batalf's answer did work!
Now i know why:
Inthe's command:    Edit1.Text + 1
Batalf's command:   Edit1.Text + '1'
You can see the difference....
I couldn't give them t.ex. 100 points each - I had to choose.
Therefor I chose the one who gave me the command that worked!

Monsemann

-who is 'Barry'?????
0
 
LischkeCommented:
inthe
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 4
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now