Posted: Sat Jan 17, 2009 9:46 pm Post subject: Decimals and Negatives
We have to make a calculator on turing I have everything else I am just having a little trouble with AC, decimal, and the negative part of the programming. The AC is working only for the fist number and only when I press an operator. The decimals and negatives are confusing. If someone can help that would be great!
Sponsor Sponsor
DanielG
Posted: Sat Jan 17, 2009 9:50 pm Post subject: RE:Decimals and Negatives
negative shouldn't be a problem, all you need is a boolean to check if negative, if it is, when adding a number add the negative of the number clicked, if not add the positive.
decimals is a bit more complex, you need a boolean variable (again) to store whether the dot has been placed yet or if it hasn't, you also need an integer depth (discussed later), if it hasn't you obviously multiply your current value by 10 then add the value, if it has been placed, then you have to divide the number by 10 as many times as the depth then add it to the number (without multiplying by 10)
hope this helps.
Crazy
Posted: Sat Jan 17, 2009 10:27 pm Post subject: RE:Decimals and Negatives
that helps
can you give me an example of how this would look?for both the decimal and negative
DanielG
Posted: Sat Jan 17, 2009 10:52 pm Post subject: RE:Decimals and Negatives
for negative, you have the number -100 stored, which you know is a negative, so when clicking on say 6 on the calculator, it would mutliply -100 by 10, giving -1000, then add -6, resulting in -1006, which is what should happen on a calculator.
for decimals, if '.' has not been pressed on, then do as above (only with positives assuming it is positive). if it has, then use the depth to see how much you divide by. for example, you have 1.31 stored, the depth would be 1000, since that is what you need to divide by, so when for example 3 is pressed, the number becomes 1.31+3/1000, afterwards, remember to multiply depth by 10 after opearation (earler I said take 10^depth, but this is easier)
Crazy
Posted: Sat Jan 17, 2009 11:00 pm Post subject: RE:Decimals and Negatives
thank you it actually helps
lol
I understand somewat
DanielG
Posted: Sat Jan 17, 2009 11:11 pm Post subject: RE:Decimals and Negatives
np
Crazy
Posted: Sat Jan 17, 2009 11:26 pm Post subject: Re: Decimals and Negatives
Quote:
if op= "4" then %negative
drawfillbox (100, 200, 350, 365, black)
num1 *10+num2
end if
i made the negative sign into op 4 the drawfillbox is my screen
does that look right?
I'm sory if im bothering you but imnew to turing and we didnt learn alot in class.
DanielG
Posted: Sat Jan 17, 2009 11:42 pm Post subject: RE:Decimals and Negatives
wait, can you properly explain what the assignment is? I want to be sure I haven't explained to you something that is completely unrelated.
Sponsor Sponsor
Crazy
Posted: Sat Jan 17, 2009 11:45 pm Post subject: RE:Decimals and Negatives
I think you have explained it properly I'm not getting it. We have to make a calculator the basic functions +,-,*, and /. WIth those functions I am also including negatives, decimals, and an ac button.
DanielG
Posted: Sat Jan 17, 2009 11:50 pm Post subject: RE:Decimals and Negatives
how do you get the input? what is ac again?
Crazy
Posted: Sat Jan 17, 2009 11:55 pm Post subject: RE:Decimals and Negatives
we used the buttonwait so the users click on the numbers and the show on the screen.
Posted: Sun Jan 18, 2009 8:09 pm Post subject: RE:Decimals and Negatives
lol i got the negative to work thanks for ur help and the users on chat
So for the decimals as u said before instead of multiplying it by 10 we would divde number by 10?
deltatux
Posted: Sun Jan 18, 2009 8:32 pm Post subject: RE:Decimals and Negatives
I don't remember Turing too much, but if you can set it to a double (haven't used Turing since I ditched it in grade 9 ... or 4 yrs ago) you can store decimal numbers.
in Java, I would do something like this:
code:
double dblDecimal = 0;
I think it's called real numbers in Turing.
deltatux
Crazy
Posted: Sun Jan 18, 2009 8:38 pm Post subject: RE:Decimals and Negatives
ye it is real numbers, i dont know where to go on form there
DanielG
Posted: Sun Jan 18, 2009 8:41 pm Post subject: RE:Decimals and Negatives
you need to store how many numbers after the decimal point you have placed, and divide by a power of 10 accordingly, or even better, whenever adding a number, multiply your division constant by 10.