Computer Science Canada Calculator Questions |
Author: | Wajih [ Thu Jan 13, 2011 6:10 pm ] |
Post subject: | Calculator Questions |
What is it you are trying to achieve? I am trying to create a basic calculator. What is the problem you are having? Hey guys, thanks to your help, i have created a basic calculator. it still has a few bugs that i would like to fix. 1) Right now, the calculator can only do single digit operations. like 6 /8 = 0.75. What should i do so that calculator can recognize i entered the number 123 instead of 1, then clearing the number, 2 then clearing the number, and then 3, then clearing the number. 2) I need help with a part of the program that clears a certain parameter of data when a certain button is pressed. The creating the button is easy, but how do you make it so when the button is clicked it only clears a certain part of the screen. Is that even possible? 3) For my calculator, you have to enter the two numbers then the operator to do the calculations. How do you make it so that you have to enter the first number, then the operator, and then the second number to do the calculations. For 1) my teacher told me that i have to do some sort of storage technique but it was a bit confusing. can you guys elaborate? Describe what you have tried to solve this problem i tried creating a new variable to store the answer, but it did not work. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) available upon request Please specify what version of Turing you are using 4.1.1 |
Author: | Tony [ Thu Jan 13, 2011 6:35 pm ] |
Post subject: | RE:Calculator Questions |
1 and 3 are the same thing, and your teacher is right -- you need to keep track of what state you are in right now. For example, "entering the first number"; while in this state, every digit pressed gets added to the first number being entered. 2 -- clearing means drawing something new on top. You can draw something new in just one part of the screen. |
Author: | Wajih [ Thu Jan 13, 2011 6:42 pm ] |
Post subject: | RE:Calculator Questions |
1 and 3-but my calculator has a button type interface, so if i enter a number, it will only show that number. and by state, you mean creating a new variable that keeps track if it is in this state or not? 2- but i want to clear only a certain paramters of the code x and y, coordinates. oh i get it, do i have to do something like if this button is clicked, cls this part of the screen? |
Author: | Tony [ Thu Jan 13, 2011 6:46 pm ] |
Post subject: | RE:Calculator Questions |
yes and yes. |
Author: | Wajih [ Thu Jan 13, 2011 7:04 pm ] |
Post subject: | RE:Calculator Questions |
1 and 3- kk i get it. so it will be like if state = 1 then so and so else do the single digit operation. 20 its clearer to me but do i have to be like if button = 1 and so and so then cls ( x1, y1, x2, y2) end if. that doesnt seem right because i think the cls will clear the whole screen instead of the certain parameters |
Author: | Wajih [ Thu Jan 13, 2011 7:06 pm ] |
Post subject: | RE:Calculator Questions |
2-* |
Author: | Tony [ Thu Jan 13, 2011 7:15 pm ] |
Post subject: | RE:Calculator Questions |
you can read the build in documentation or our copy of it here -- cls |
Author: | Wajih [ Thu Jan 13, 2011 7:19 pm ] |
Post subject: | RE:Calculator Questions |
alright i read it, but how do i change the row and column? |
Author: | Tony [ Thu Jan 13, 2011 7:25 pm ] |
Post subject: | RE:Calculator Questions |
of the cursor? locate(r,c) Did you read this part? Quote: The entire output window is set to the current text background color Hint: cls is probably not what you are looking for. |
Author: | Wajih [ Thu Jan 13, 2011 7:29 pm ] |
Post subject: | RE:Calculator Questions |
oh so the if i do cls, it clears the entire thing since the background is white. do you know where to find what i am looking for? erase, delete? |
Author: | Tony [ Thu Jan 13, 2011 7:38 pm ] |
Post subject: | Re: RE:Calculator Questions |
Tony @ Thu Jan 13, 2011 6:35 pm wrote: 2 -- clearing means drawing something new on top. You can draw something new in just one part of the screen. |
Author: | Wajih [ Thu Jan 13, 2011 7:39 pm ] |
Post subject: | RE:Calculator Questions |
never mind man i just solved the 2nd one ![]() and if it 0 then it will do single digit operations. if state is 1, it will do triple or digit operations. but how? do i have to put num, num? or num, num2? |
Author: | Tony [ Thu Jan 13, 2011 7:43 pm ] |
Post subject: | RE:Calculator Questions |
You have clicked the first number. You have a copy of that single digit in num1, and you are in a state that describes this situation. You click on another digit. What needs to happen to num1 and what needs to happen to the state? How about if you click on an operator, such as "+"? |
Author: | Wajih [ Thu Jan 13, 2011 7:52 pm ] |
Post subject: | RE:Calculator Questions |
so i am in state = 1. num1 becomes the first number after the second digit i click. if state = 1 then i should add to the num1? |
Author: | Tony [ Thu Jan 13, 2011 8:06 pm ] |
Post subject: | RE:Calculator Questions |
if state = 1 describes a situation where you are "still entering num1", then yes, the second digit needs to be included. The tricky parts are the transitions. How do you get to state = 2 and what does it mean? ![]() |
Author: | Wajih [ Thu Jan 13, 2011 8:22 pm ] |
Post subject: | RE:Calculator Questions |
why would you get to state = 2? doesnt state = 1 include all the operations for double, triple digit operations, etc? or for each additional digit you have to go to another state? if stage = 1, then how would output lets say 23? |
Author: | Tony [ Thu Jan 13, 2011 8:31 pm ] |
Post subject: | RE:Calculator Questions |
As you are entering the first number, do you need different states for having already entered 1, 2, or 3 digits? If so, how are they different? |
Author: | Wajih [ Thu Jan 13, 2011 8:40 pm ] |
Post subject: | RE:Calculator Questions |
im thinking you can add as many numbers as you want until you hit the second set of numbers |
Author: | Tony [ Thu Jan 13, 2011 9:18 pm ] |
Post subject: | RE:Calculator Questions |
Indeed. As such, you need just a few general states. |
Author: | Wajih [ Thu Jan 13, 2011 9:49 pm ] |
Post subject: | RE:Calculator Questions |
im sorry, i dont understand what you are trying to say. so if its a 6 digit number it should be in state 5? but first, if its in state = 1, how would you output a number? |
Author: | Tony [ Thu Jan 13, 2011 9:59 pm ] |
Post subject: | Re: RE:Calculator Questions |
Wajih @ Thu Jan 13, 2011 9:49 pm wrote: so if its a 6 digit number it should be in state 5?
Wajih @ Thu Jan 13, 2011 8:40 pm wrote: im thinking you can add as many numbers as you want until you hit the second set of numbers |
Author: | Wajih [ Thu Jan 13, 2011 10:20 pm ] |
Post subject: | RE:Calculator Questions |
oh i get what you are saying now. but for displaying the number that you want, how do you do it? do you add num and num to get num? |
Author: | Tony [ Thu Jan 13, 2011 10:26 pm ] |
Post subject: | RE:Calculator Questions |
Yes, almost. But if the first digit is 1 and second is 2, you want to get 1&2=12, not 1+2=3. |
Author: | Wajih [ Thu Jan 13, 2011 10:43 pm ] |
Post subject: | RE:Calculator Questions |
then how would you do something like that? would you add to num, multiply it? i will send you a part of my code : if (button = 1) and (x >= 137) and (x <= 158) and (y >= 142) and (y <= 163) then locate (5, 19) num := 1 put num .. end if if (button = 1) and (x >= 194) and (x <= 214) and (y >= 142) and (y <= 163) then locate (5, 19) num := 2 put num .. end if do i have to change the num? |
Author: | Tony [ Thu Jan 13, 2011 10:48 pm ] |
Post subject: | RE:Calculator Questions |
if you had 1 and there was another number entered, then that 1 should become 10 before the second number is added in, right? |
Author: | Wajih [ Thu Jan 13, 2011 10:52 pm ] |
Post subject: | RE:Calculator Questions |
right, but how would it become 10? because its already given a value of 1 right? is there a code that allows a value to be added right before a second number is clicked? |
Author: | Tony [ Thu Jan 13, 2011 11:01 pm ] | ||
Post subject: | RE:Calculator Questions | ||
for example, you can do things like
When you click that second button, you know what num was before and you know what button was clicked, so you have enough information to figure out what num should become. |
Author: | Wajih [ Thu Jan 13, 2011 11:20 pm ] |
Post subject: | RE:Calculator Questions |
when you mean the second button, you mean 2 right? and when you know mean "you know what num was befor and you know what button was clicked" you are talking about 1 and 2 right? so 1 and 12 will equal 12. but how do i write that into my program? because i already assigned num a value of real and 0. |
Author: | Tony [ Thu Jan 13, 2011 11:25 pm ] |
Post subject: | RE:Calculator Questions |
you can assign num a new value; see the example above. |
Author: | Wajih [ Thu Jan 13, 2011 11:47 pm ] |
Post subject: | RE:Calculator Questions |
see i did that but when i entered 1 and then 2, it erased the one ![]() |
Author: | Tony [ Fri Jan 14, 2011 12:03 am ] |
Post subject: | RE:Calculator Questions |
You could post the relevant parts here. |
Author: | Wajih [ Fri Jan 14, 2011 8:55 pm ] |
Post subject: | RE:Calculator Questions |
var num : real := 0 var num2 : real := 0 var symbol : string := "" var x, y, button : int := 0 var state : string := "" put "what state would you like to work in? ( 0 ) Right now this calculator does not do double digit operations. but it will be able to soon " get state cls if (state not= "0") then put "I am sorry but you have entered an invalid number. Please try again." put "" end if View.Set ("offscreenonly") if state = "0" then loop View.Update Mouse.Where (x, y, button) Text.Locate (24, 1) if button = 0 then put x : 0, " ", y : 0, " button up" else put x : 0, " ", y : 0, " button down" end if if (button = 1) and (x >= 196) and (x <= 215) and (y >= 110) and (y <= 130) then locate (5, 19) num := 0 put num .. end if if (button = 1) and (x >= 137) and (x <= 158) and (y >= 142) and (y <= 163) then locate (5, 19) num := 1 put num .. end if if (button = 1) and (x >= 194) and (x <= 214) and (y >= 142) and (y <= 163) then locate (5, 19) num := 2 put num .. end if if (button = 1) and (x >= 251) and (x <= 272) and (y >= 142) and (y <= 163) then locate (5, 19) num := 3 put num .. end if if (button = 1) and (x >= 137) and (x <= 158) and (y >= 190) and (y <= 208) then locate (5, 19) num := 4 put num .. end if if (button = 1) and (x >= 194) and (x <= 214) and (y >= 190) and (y <= 208) then locate (5, 19) num := 5 put num .. end if if (button = 1) and (x >= 251) and (x <= 272) and (y >= 190) and (y <= 208) then locate (5, 19) num := 6 put num .. end if if (button = 1) and (x >= 137) and (x <= 158) and (y >= 241) and (y <= 258) then locate (5, 19) num := 7 put num .. end if if (button = 1) and (x >= 196) and (x < 210) and (y > 237) and (y < 259) then locate (5, 19) num := 8 put num .. end if if (button = 1) and (x >= 251) and (x <= 272) and (y >= 241) and (y <= 258) then locate (5, 19) num := 9 put num .. end if drawbox (100, 100, 500, 475, red) drawbox (128, 305, 470, 350, black) locate (2, 32) put "CALCULATOR" .. locate (18, 26) put "0" .. locate (16, 19) put "1" .. locate (16, 26) put "2" .. locate (16, 33) put "3" .. locate (13, 19) put "4" .. locate (13, 26) put "5" .. locate (13, 33) put "6" .. locate (10, 19) put "7" .. locate (10, 26) put "8" .. locate (10, 33) put "9" .. locate (5, 43) put "=" .. locate (8, 48) put "-" .. locate (8, 42) put "+" .. locate (8, 29) put "/" .. locate (8, 23) put "*" .. locate (13, 57) put "0" .. locate (16, 40) put "1" .. locate (16, 45) put "2" .. locate (16, 50) put "3" .. locate (13, 40) put "4" .. locate (13, 45) put "5" .. locate (13, 50) put "6" .. locate (10, 40) put "7" .. locate (10, 45) put "8" .. locate (10, 50) put "9" .. locate (16, 55) put "CLEAR" .. drawbox (426, 137, 479, 164, black) drawoval (452, 200, 15, 15, black) drawoval (205, 120, 12, 12, black) drawoval (148, 151, 12, 12, black) drawoval (205, 151, 12, 12, black) drawoval (262, 151, 12, 12, black) drawoval (148, 200, 12, 12, black) drawoval (205, 200, 12, 12, black) drawoval (262, 200, 12, 12, black) drawoval (148, 249, 12, 12, black) drawoval (205, 249, 12, 12, black) drawoval (262, 249, 12, 12, black) drawoval (316, 200, 13, 13, black) drawoval (316, 151, 13, 13, black) drawoval (356, 200, 13, 13, black) drawoval (396, 200, 13, 13, black) drawoval (396, 152, 13, 13, black) drawoval (356, 152, 13, 13, black) drawoval (316, 248, 13, 13, black) drawoval (356, 248, 13, 13, black) drawoval (396, 248, 13, 13, black) drawoval (180, 280, 10, 10, black) drawoval (228, 280, 10, 10, black) drawoval (332, 279, 12, 12, black) drawoval (380, 279, 12, 12, black) drawfillbox (285, 100, 286, 300, black) if (button = 1) and (x >= 440) and (x <= 465) and (y >= 187) and (y <= 213) then locate (5, 25) num2 := 0 put num2 .. end if if (button = 1) and (x >= 305) and (x <= 327) and (y >= 140) and (y <= 163) then locate (5, 25) num2 := 1 put num2 .. end if if (button = 1) and (x >= 345) and (x <= 367) and (y >= 140) and (y <= 163) then locate (5, 25) num2 := 2 put num2 .. end if if (button = 1) and (x >= 386) and (x <= 406) and (y >= 140) and (y <= 163) then locate (5, 25) num2 := 3 put num2 .. end if if (button = 1) and (x >= 305) and (x <= 327) and (y >= 190) and (y <= 211) then locate (5, 25) num2 := 4 put num2 .. end if if (button = 1) and (x >= 345) and (x <= 367) and (y >= 190) and (y <= 211) then locate (5, 25) num2 := 5 put num2 .. end if if (button = 1) and (x >= 386) and (x <= 406) and (y >= 190) and (y <= 211) then locate (5, 25) num2 := 6 put num2 .. end if if (button = 1) and (x >= 305) and (x <= 327) and (y >= 238) and (y <= 260) then locate (5, 25) num2 := 7 put num2 .. end if if (button = 1) and (x >= 345) and (x <= 367) and (y >= 238) and (y <= 260) then locate (5, 25) num2 := 8 put num2 .. end if if (button = 1) and (x >= 386) and (x <= 406) and (y >= 238) and (y <= 260) then locate (5, 25) num2 := 9 put num2 .. end if if (button = 1) and (x >= 172) and (x <= 187) and (y >= 273) and (y <= 290) then locate (5, 22) put "*" .. locate (5, 45) put num * num2 .. end if if (button = 1) and (x >= 220) and (x <= 236) and (y >= 273) and (y <= 290) then locate (5, 22) put "/" .. locate (5, 45) put num / num2 .. end if if (button = 1) and (x >= 323) and (x <= 341) and (y >= 271) and (y <= 290) then locate (5, 22) put "+" .. locate (5, 45) put num + num2 .. end if if (button = 1) and (x >= 370) and (x <= 389) and (y >= 271) and (y <= 290) then locate (5, 22) put "-" .. locate (5, 45) put num - num2 .. end if if (button = 1) and (x >= 426) and (x <= 479) and (y >= 137) and (y <= 164) then cls end if locate (20, 1) put "INSTRUCTIONS : to get your desired answer, you must first enter a number on the left side of the calculator, and then enter a number on the right side. Once youhave done that enter an operator" locate (1, 1) put "Welcome to" .. locate (2, 1) put "Wajihs Basic" .. locate (3, 1) put "Calculator" .. end loop end if im sorry, i had to post of all it because im confused. i tried making num and num 2 string with a value of "" but even that did not work. that is the reason i posted all of my code |
Author: | Wajih [ Fri Jan 14, 2011 10:34 pm ] |
Post subject: | RE:Calculator Questions |
i was reading something about arrays. can i do something like that for this program? |
Author: | TokenHerbz [ Fri Jan 14, 2011 10:44 pm ] |
Post subject: | RE:Calculator Questions |
YES OMG, how about an array of a class of these buttons? --> Lets fix this yes yes? |
Author: | Wajih [ Fri Jan 14, 2011 11:22 pm ] |
Post subject: | RE:Calculator Questions |
im not sure if you're being serious or not lol, no offense, but would arrays work in this case? |
Author: | Tony [ Fri Jan 14, 2011 11:33 pm ] |
Post subject: | RE:Calculator Questions |
I am assuming the the coordinates of any particular button can be figured out based on its index, so maybe just a for-loop instead? |
Author: | Wajih [ Sat Jan 15, 2011 12:02 am ] |
Post subject: | RE:Calculator Questions |
wouldnt you have to make up a nex variable then? cant just do like this: for i : 0 ...1000000 end for or something like that |
Author: | Wajih [ Sat Jan 15, 2011 4:41 pm ] |
Post subject: | RE:Calculator Questions |
hold up. my two sets of numbers are in the same loop. do i have to make num in one loop and num2 in another, then loop them ? right now it is like this: loop num num2 end loop but should it be like this? : loop loop num end loop loop num2 end loop end loop |
Author: | Tony [ Sat Jan 15, 2011 7:23 pm ] |
Post subject: | RE:Calculator Questions |
In the second version, you'd be looping as you are entering the first number, end when you are done, then loop while entering the second number. That sounds like a right thing to do. |
Author: | Wajih [ Sat Jan 15, 2011 8:42 pm ] |
Post subject: | RE:Calculator Questions |
the second state? or the second version of the loop that i posted? |
Author: | Tony [ Sat Jan 15, 2011 8:45 pm ] |
Post subject: | RE:Calculator Questions |
The second version of the loop. You'd need to keep track of your states to figure out when to exit one loop and enter into another. |
Author: | Wajih [ Sat Jan 15, 2011 9:26 pm ] |
Post subject: | RE:Calculator Questions |
alright but how many view updates would you have and where wouldyou put them in the loops? |
Author: | Tony [ Sat Jan 15, 2011 9:30 pm ] |
Post subject: | RE:Calculator Questions |
Think about what View.Update does, that should lead towards the answer (or to the question that should lead towards the answer ![]() |
Author: | Wajih [ Sat Jan 15, 2011 9:46 pm ] |
Post subject: | RE:Calculator Questions |
so there should be 3? because there are 3 loops and each loop is drawing the calculator? |
Author: | Tony [ Sat Jan 15, 2011 9:50 pm ] |
Post subject: | RE:Calculator Questions |
That sounds correct. |
Author: | Wajih [ Sat Jan 15, 2011 10:05 pm ] |
Post subject: | RE:Calculator Questions |
i did that but when i tried to press any second set of buttons, they would not work, any reasons why? |
Author: | Tony [ Sat Jan 15, 2011 10:14 pm ] |
Post subject: | RE:Calculator Questions |
Perhaps the code flow is not where you expect it to be, or the values of mouse variables don't line up with the expected if conditionals. Trace through your program, and try to figure out exactly what's going on at every step of it. |
Author: | Wajih [ Sat Jan 15, 2011 10:27 pm ] |
Post subject: | RE:Calculator Questions |
but when i put it all in one loop, both sets of numbers work, why is that? and do you know how to shorten string catenation generated by a string |
Author: | Tony [ Sat Jan 15, 2011 10:56 pm ] |
Post subject: | Re: RE:Calculator Questions |
Wajih @ Sat Jan 15, 2011 10:27 pm wrote: but when i put it all in one loop, both sets of numbers work, why is that?
When you randomly put things in and out of code, there's a discrepancy between what you are telling the computer to do and what you understand about the things that you tell the computer to do. The difference between the two is the cause for a majority of problems with running programs. Wajih @ Sat Jan 15, 2011 10:27 pm wrote: and do you know how to shorten string catenation generated by a string I don't understand this question. |
Author: | Wajih [ Sat Jan 15, 2011 11:09 pm ] |
Post subject: | RE:Calculator Questions |
but i am doing the same thing that you told me. i put loop view update draw stuff loop viewupdate drawstuff num end loop loop viewupdate drawstuff num2 end loop end loop i did that. it recognized the first number and the operator, but it did not recognize the second number. also, is there a command similar to locate? |
Author: | Tony [ Sat Jan 15, 2011 11:49 pm ] |
Post subject: | RE:Calculator Questions |
I also said to read the documentation on View.Update (and the example provided there). Things you draw will not show up on screen until View.Update is called (think about the order of drawing stuff, and telling it to show up). Also, unless there's a lot of "flashing" happening, you don't really need offscreenonly mode. Documentation for locate might list related functions. Is there something in particular you are looking for? |
Author: | Wajih [ Sun Jan 16, 2011 12:07 am ] |
Post subject: | RE:Calculator Questions |
exactly bro, i read it. i out the view update at the bottom of the loops AFTER it did all the drawing stuff, but then the whole screen went blank and i couldnt do anything with it. and there is a lot of flashing going on which is why i am using offscreenonly yes, there is. i made up a button and a variable. once something is multiplied, or divided, or added, or subtracted i store it in a variable. then i "locate" that variable onto a certain row and column. there is a button that when it is pressed, it is supposed to clear what i have typed and set the answer back to 0. but if try pressing that button , only the numbers that i did the operations with get deleted, not the answer. is there anything related that will cls of the answer and set the answer back to 0? |
Author: | Tony [ Sun Jan 16, 2011 12:17 am ] | ||
Post subject: | RE:Calculator Questions | ||
if you use something like
That will clear (draw on top of) the entire row. |
Author: | Wajih [ Sun Jan 16, 2011 1:16 pm ] |
Post subject: | RE:Calculator Questions |
i got that too work but i still do not know how to do double digit operations. |
Author: | Tony [ Sun Jan 16, 2011 1:37 pm ] |
Post subject: | RE:Calculator Questions |
Maybe ask huskiesgoaler34? http://compsci.ca/v3/viewtopic.php?t=27249 |