Posted: Sat Jan 15, 2011 8:44 pm Post subject: Urgent Help for Calculator Program
What is it you are trying to achieve?
Being able to answer the equation
What is the problem you are having?
can't calculate the answer
Describe what you have tried to solve this problem
I wrote the code but it doesn't work
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
View.Set ("graphics:602;662,nobuttonbar,title: KoolCulator") %sets screen graphics to specific dimensions (title is set, toolbar is removed)
colourback (black) %sets screen background colour to black
cls %allows whole screen colour to be set to black
%Main Program for my Calculator
colour (white) %sets font to white (Only used this for seeing outputted answer while testing my program)
%Global Variables
var xcoordinate : int %the x coordinate of the button (used for determining if the button has been pressed)
var ycoordinate : int %the y coordinate of the button (used for determining if the button has been pressed)
var button : int %the value of the button (used for determining if the mouse has been clicked; set to 0 if no click, 1 if there is a click)
var buttoncheck : boolean %checks to see if the button has been clicked, if it has been, then if statements are activated
buttoncheck := false %set to false (button hasn't been pressed); value is either true or false (button has been pressed or hasn't been pressed)
var buttonfont : int %the declaration of the font used for the number and operation buttons
var offbuttonfont : int %the declaration of the font used for the off button
var clearbuttonfont : int %the declaration of the font used for the clear button
var number1 : string := ""
var number2 : string := ""
var operation : string := ""
var answer : int := 0
%Draws the button that the numbers will be outputed to
Draw.FillBox (10, 520, 590, 650, grey) %draws the area where the numbers are outputed onto the screen
%Draws the first column of buttons for calculator
Draw.FillBox (10, 10, 350, 110, white) %draws the 0 button onto the screen
Draw.FillBox (10, 130, 110, 240, white) %draws the 1 button onto the screen
Draw.FillBox (10, 260, 110, 370, white) %draws the 4 button onto the screen
Draw.FillBox (10, 390, 110, 500, white) %draws the 7 button onto the screen
%Draws the second column of buttons for calculator
Draw.FillBox (130, 130, 230, 240, white) %draws the 2 button onto the screen
Draw.FillBox (130, 260, 230, 370, white) %draws the 5 button onto the screen
Draw.FillBox (130, 390, 230, 500, white) %draws the 8 button onto the screen
%Draws the third column of buttons for calculator
Draw.FillBox (250, 130, 350, 240, white) %draws the 3 button onto the screen
Draw.FillBox (250, 260, 350, 370, white) %draws the 6 button onto the screen
Draw.FillBox (250, 390, 350, 500, white) %draws the 9 button onto the screen
%Draws the fourth column of buttons for calculator
Draw.FillBox (370, 10, 470, 110, white) %draws the decimal button onto the screen
Draw.FillBox (370, 130, 470, 240, white) %draws the addition button onto the screen
Draw.FillBox (370, 260, 470, 370, white) %draws the multiplication button onto the screen
Draw.FillBox (370, 390, 470, 500, red) %draws the delete button onto the screen
%Draws the fifth column of buttons for calculator
Draw.FillBox (490, 10, 590, 110, white) %draws the equal button onto the screen
Draw.FillBox (490, 130, 590, 240, white) %draws the subtraction button onto the screen
Draw.FillBox (490, 260, 590, 370, white) %draws the division onto the screen
Draw.FillBox (490, 390, 590, 500, red) %draws the off button onto the screen
buttonfont := Font.New ("TimesNewRoman:56") %declares the value of buttonfont (Times New Roman font with a size of 56)
offbuttonfont := Font.New ("TimesNewRoman:36:bold") %declares the value of buttonfont (Times New Roman font, bolded with a size of 36)
clearbuttonfont := Font.New ("TimesNewRoman:32:bold") %declares the value of buttonfont (Times New Roman font, bolded with a size of 36)
Font.Draw ("0", 165, 35, buttonfont, black) %draws the 0 onto the 0 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("1", 40, 155, buttonfont, black) %draws the 1 onto the 1 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("2", 160, 155, buttonfont, black) %draws the 2 onto the 2 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("3", 280, 155, buttonfont, black) %draws the 3 onto the 3 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("4", 40, 290, buttonfont, black) %draws the 4 onto the 4 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("5", 160, 290, buttonfont, black) %draws the 5 onto the 5 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("6", 280, 290, buttonfont, black) %draws the 6 onto the 6 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("7", 40, 420, buttonfont, black) %draws the 7 onto the 7 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("8", 160, 420, buttonfont, black) %draws the 8 onto the 8 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("9", 280, 420, buttonfont, black) %draws the 9 onto the 9 button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw (".", 408, 53, buttonfont, black) %draws the . onto the decimal button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("+", 398, 155, buttonfont, black) %draws the + onto the addition button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("-", 527, 160, buttonfont, black) %draws the - onto the subtraction button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("x", 402, 290, buttonfont, black) %draws the x onto the multiplication button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("?", 520, 283, buttonfont, black) %draws the ? onto the division button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("=", 517, 34, buttonfont, black) %draws the = onto the equal button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("OFF", 494, 425, offbuttonfont, white) %draws the OFF onto the off button for the calculator at these coordinates, using black font, using buttonfont
Font.Draw ("DEL", 377, 425, clearbuttonfont, white) %draws the DEL onto the delete button for the calculator at these coordinates, using black font, using buttonfont
Font.Free (offbuttonfont) %used to free the memory used by the font that won't be needed for the rest of the program
loop %<=========== LOOP 1: loops the whole program
loop %<=========== LOOP 2: loops the section determining the value of number1
Mouse.Where (xcoordinate, ycoordinate, button) %Mouse.Where checks to see where the mouse is located (x,y coordinates)and if the button was pressed
if buttoncheck = false then %checks to see if the button was pressed (buttoncheck is false because no button was pressed)
if button = 1 then %if the button is pressed (holds a value of 1) then buttoncheck is true
buttoncheck := true %buttoncheck is true because a button was pressed
if xcoordinate > 10 and xcoordinate < 350 and ycoordinate > 10 and ycoordinate < 110 then %if the mouse was clicked in this area then...
number1 := number1 + "0" %...add 0 to the value of number1
elsif xcoordinate > 10 and xcoordinate < 110 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
number1 := number1 + "1" %...add 1 to the value of number1
elsif xcoordinate > 130 and xcoordinate < 230 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
number1 := number1 + "2" %...add 2 to the value of number1
elsif xcoordinate > 250 and xcoordinate < 350 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
number1 := number1 + "3" %...add 3 to the value of number1
elsif xcoordinate > 10 and xcoordinate < 110 and ycoordinate > 260 and ycoordinate < 370 then %if the mouse was clicked in this area then...
number1 := number1 + "4" %...add 4 to the value of number1
elsif xcoordinate > 130 and xcoordinate < 230 and ycoordinate > 260 and ycoordinate < 370 then %if the mouse was clicked in this area then...
number1 := number1 + "5" %...add 5 to the value of number1
elsif xcoordinate > 250 and xcoordinate < 350 and ycoordinate > 260 and ycoordinate < 370 then %if the mouse was clicked in this area then...
number1 := number1 + "6" %...add 6 to the value of number1
elsif xcoordinate > 10 and xcoordinate < 110 and ycoordinate > 390 and ycoordinate < 500 then %if the mouse was clicked in this area then...
number1 := number1 + "7" %...add 7 to the value of number1
elsif xcoordinate > 130 and xcoordinate < 230 and ycoordinate > 390 and ycoordinate < 500 then %if the mouse was clicked in this area then...
number1 := number1 + "8" %...add 8 to the value of number1
elsif xcoordinate > 250 and xcoordinate < 350 and ycoordinate > 390 and ycoordinate < 500 then %if the mouse was clicked in this area then...
number1 := number1 + "9" %...add 9 to the value of number1
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 10 and ycoordinate < 110 then %if the mouse was clicked in this area then...
number1 := number1 + "." %...add . to the value of number1
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 390 and ycoordinate < 500 then %if the mouse was clicked in this area then...
number1 := "" %...clears the numbers off the screen
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
operation := "+"
exit
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 260 and ycoordinate < 370 then %if the mouse was clicked in this area then...
operation := "x"
exit
elsif xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
operation := "-"
exit
elsif xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 260 and ycoordinate < 370 then
operation := "?"
elsif xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 10 and ycoordinate < 110 then
end if
end if
exit when xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 390 and ycoordinate < 500
else
if button = 0 then
buttoncheck := false
end if
end if
loop %<=========== LOOP 3: loops the section determining the value of number2
Mouse.Where (xcoordinate, ycoordinate, button)
if buttoncheck = false then
if button = 1 then
buttoncheck := true
if xcoordinate > 10 and xcoordinate < 350 and ycoordinate > 10 and ycoordinate < 110 then %if the mouse was clicked in this area then...
number2 := number2 + "0" %...add 0 to the value of number2
elsif xcoordinate > 10 and xcoordinate < 110 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
number2 := number2 + "1" %...add 1 to the value of number2
elsif xcoordinate > 130 and xcoordinate < 230 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
number2 := number2 + "2" %...add 2 to the value of number2
elsif xcoordinate > 250 and xcoordinate < 350 and ycoordinate > 130 and ycoordinate < 240 then %if the mouse was clicked in this area then...
number2 := number2 + "3" %...add 3 to the value of number2
elsif xcoordinate > 10 and xcoordinate < 110 and ycoordinate > 260 and ycoordinate < 370 then %if the mouse was clicked in this area then...
number2 := number2 + "4" %...add 4 to the value of number2
elsif xcoordinate > 130 and xcoordinate < 230 and ycoordinate > 260 and ycoordinate < 370 then %if the mouse was clicked in this area then...
number2 := number2 + "5" %...add 5 to the value of number2
elsif xcoordinate > 250 and xcoordinate < 350 and ycoordinate > 260 and ycoordinate < 370 then %if the mouse was clicked in this area then...
number2 := number2 + "6" %...add 6 to the value of number2
elsif xcoordinate > 10 and xcoordinate < 110 and ycoordinate > 390 and ycoordinate < 500 then %if the mouse was clicked in this area then...
number2 := number2 + "7" %...add 7 to the value of number2
elsif xcoordinate > 130 and xcoordinate < 230 and ycoordinate > 390 and ycoordinate < 500 then %if the mouse was clicked in this area then...
number2 := number2 + "8" %...add 8 to the value of number2
elsif xcoordinate > 250 and xcoordinate < 350 and ycoordinate > 390 and ycoordinate < 500 then %if the mouse was clicked in this area then...
number2 := number2 + "9" %...add 9 to the value of number2
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 10 and ycoordinate < 110 then
number2 := number2 + "." %...add . to the value of number2
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 390 and ycoordinate < 500 then
number2 := ""
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 130 and ycoordinate < 240 then
operation := "+"
exit
elsif xcoordinate > 370 and xcoordinate < 470 and ycoordinate > 260 and ycoordinate < 370 then
operation := "x"
exit
elsif xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 130 and ycoordinate < 240 then
operation := "-"
exit
elsif xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 260 and ycoordinate < 370 then
operation := "?"
elsif xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 10 and ycoordinate < 110 then
exit
end if
%exit when xcoordinate > 490 and xcoordinate < 590 and ycoordinate > 390 and ycoordinate < 500
end if
else
if button = 0 then
buttoncheck := false
end if
end if
Posted: Sat Jan 15, 2011 9:43 pm Post subject: Re: Urgent Help for Calculator Program
the answer is not calculated and is not put to the screen. My goal is to create a function to store the calculations and then call the function when one of the variables are pressed.
Tony
Posted: Sat Jan 15, 2011 9:48 pm Post subject: RE:Urgent Help for Calculator Program
That is too generic. The answer might not be calculated for any number of reasons.
Posted: Sat Jan 15, 2011 9:58 pm Post subject: Re: Urgent Help for Calculator Program
for example : If I press 5 +3, the answer is 5
another problem is that when i go cls (delete button) and i add 9 +1, the answer is 91.
Tony
Posted: Sat Jan 15, 2011 10:05 pm Post subject: RE:Urgent Help for Calculator Program
Perhaps you should check what the values of number1 and number2 actually at, at the moment when you are calculating your "answer". See if they are what you expect them to be.
Posted: Sat Jan 15, 2011 10:57 pm Post subject: Re: Urgent Help for Calculator Program
I did and I really don't see a problem. If a number button is pressed, then the value is added to either number1 or number2, depending if an operation has been selected before that. I add that value because if I press the 7, 6,and 1 buttons, Turing would see this as 7, 6, and 1, not 761. By doing this, the value is 761. I want this value to be stored when an operation is pressed for future calculations. Do you have any suggestions on why the variable values are not what they are supposed to be. When I test my calculator out, the values of number1 and number2 come up instead of the answer itself most of the time.
I wish Turing has a built-in BEDMAS function.
Sponsor Sponsor
huskiesgoaler34
Posted: Sat Jan 15, 2011 11:00 pm Post subject: Re: Urgent Help for Calculator Program
I just noticed that when you continually use the calculator, the answer always comes out to be the last entered number. Where should I set the variables back at 0. I thought the end of the program but they don't work there.
Tony
Posted: Sat Jan 15, 2011 11:44 pm Post subject: Re: Urgent Help for Calculator Program
huskiesgoaler34 @ Sat Jan 15, 2011 10:57 pm wrote:
Do you have any suggestions on why the variable values are not what they are supposed to be.
If you know what the values actually are, instead of what you think they should be, that should give a hint as to where things might have gone wrong.
During the 1970s and 1980s, RPN had some currency even among the general public, as it was widely used in handheld calculators of the time ? for example, the HP-10C series and Sinclair Scientific calculators.
Posted: Sun Jan 16, 2011 8:41 am Post subject: Re: Urgent Help for Calculator Program
Can you please help me try to find out what the variables actually are? Can you tell me which line so I can figure it out and try to fix it? I understand what RVN does but have no clue how to add it into the program.
Tony
Posted: Sun Jan 16, 2011 9:24 am Post subject: Re: RE:Urgent Help for Calculator Program
Tony @ Sat Jan 15, 2011 10:05 pm wrote:
Perhaps you should check what the values of number1 and number2 actually at, at the moment when you are calculating your "answer".
Such as with a put statement. Unique put statements tell you both where the code is executing and what the (select) values are at that moment.
Posted: Sun Jan 16, 2011 10:39 am Post subject: Re: Urgent Help for Calculator Program
I did it Tony, thanks a lot. I created put statements at the beginning of the calculations to check the values and then a put statement after the calculation to put the answer on the screen. One problem though. When I try to use Font.Draw, the answer is not drawn. I have included my code. Can you please give me an idea. By the way, thanks so much for helping. I really appreciated that you helped me without giving away the answer. Thanks!
put number1
put number2
if operation = "*" then
answer := strint (number1) * strint (number2)
put answer
elsif operation = "/" then
answer := strint (number1) div strint (number2)
put answer
elsif operation = "+" then
answer := strint (number1) + strint (number2)
put answer
elsif operation = "-" then
answer := strint (number1) - strint (number2)
put answer
end if
Font.Draw (intstr (answer), 100, 530, buttonfont, black) %this is where i want to put the answer to the screen using that text but it doesn't do it
Draw.FillBox (10, 520, 590, 650, grey)
huskiesgoaler34
Posted: Sun Jan 16, 2011 11:37 am Post subject: Re: Urgent Help for Calculator Program
Even when I try to convert it to a string (from an int using intstr) it doesn't work
TokenHerbz
Posted: Sun Jan 16, 2011 11:43 am Post subject: RE:Urgent Help for Calculator Program
do you get errors, your font.draw looks correct.
answer is an int form? and your numbers in calc are strings?
Perhaps its located in the wrong spot in your loop, could you post more code?