Author |
Message |
darknoob
|
Posted: Wed Mar 29, 2006 8:25 pm Post subject: Trouble compiling program. |
|
|
code: |
var number : string
var located : int := 1
var intnumber : int :=0
var realnumber : real :=0
var rea11 : int := 0
var decimal : int := 0
var add : int := 0
get number
for i : 1 .. length (number)
if ord (number (i)) = 46 then
decimal := i
rea11 := 1
end if
end for
if rea11 = 1 then
if length (number) = 3 then
if decimal = 1 then
located :=2
realnumber := realnumber + ((ord (number ( located )) -48 / 10)
located :=3
realnumber := realnumber + ((ord (number ( located )) -48) / 100)
elsif decimal =2 then
located :=1
realnumber :=realnumber + ((ord (number ( located )) -48
located :=3
realnumber := realnumber + ((ord (number ( located )) -48 / 10))
elsif decimal =3 then
located :=1
realnumber := realnumber + ((ord (number ( located )) -48) * 10)
located :=2
realnumber := realnumber + ((ord (number ( located )) -48))
end if
end if
put realnumber
else
if length (number) = 1 then
located := 1
intnumber := intnumber + ord (number (located)) -48
elsif length (number) =2 then
located :=1
intnumber := intnumber + ord (number (located)) -48
elsif length (number) = 2 then
located :=1
intnumber := intnumber + ((ord (number (located)) -48) * 10)
located := 2
intnumber := intnumber + ((ord (number (located)) -48))
elsif length (number) = 3 then
located :=1
intnumber := intnumber + ((ord (number (located)) -48) * 100)
located :=2
intnumber := intnumber + ((ord (number (located)) - 48) * 10)
located :=3
intnumber := intnumber + ((ord(number (located)) -48))
elsif length (number) = 4 then
located :=1
intnumber :=intnumber + ((ord(number (located)) -48) *1000)
located :=2
intnumber :=intnumber + ((ord(number (located)) -48) *100)
located :=3
intnumber :=intnumber + ((ord(number (located)) -48) *10)
located :=4
intnumber :=intnumber + ((ord(number (located)) -48))
end if
put intnumber
end if
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 8:26 pm Post subject: (No subject) |
|
|
i donno what is wrong, every time i try to run it it always say error on 'located' |
|
|
|
|
|
HellblazerX
|
Posted: Wed Mar 29, 2006 8:31 pm Post subject: (No subject) |
|
|
the problem is the variable located, but the line before that
for the line
code: | realnumber := realnumber + ((ord (number (located)) - 48 / 10) |
you missed a bracket at the end. change it to:
code: | realnumber := realnumber + ((ord (number (located)) - 48 / 10)) |
and for the line
code: | realnumber := realnumber + ((ord (number (located)) - 48 |
you're missing 2 brackets, so change it to:
code: | realnumber := realnumber + ((ord (number (located)) - 48)) |
it should work after that. |
|
|
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 8:36 pm Post subject: (No subject) |
|
|
hmm...but what should i do?
when i run the program, everything (for example 45+44) it always turn out to be 0
what should i do?? |
|
|
|
|
|
person
|
Posted: Wed Mar 29, 2006 8:39 pm Post subject: (No subject) |
|
|
a) indent ur code
b) this is almost the exact same code as: http://www.compsci.ca/v2/viewtopic.php?t=11677
c) y dont u read the error message turing gives u?
d) tell us wat ur program is supposed to do |
|
|
|
|
|
HellblazerX
|
Posted: Wed Mar 29, 2006 8:40 pm Post subject: (No subject) |
|
|
wut is your program suppose to do? |
|
|
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 8:43 pm Post subject: (No subject) |
|
|
hmm...i know that guy!!!0_o
message says unnamed #1 finished execution, nothing about what is wrong
the run window says this when i try to run it
|
|
|
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 8:45 pm Post subject: (No subject) |
|
|
the program is suppose to read and decode a simple mathematical expression given as a string |
|
|
|
|
|
Sponsor Sponsor
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 8:54 pm Post subject: (No subject) |
|
|
i still get 0 after i changed all the stuff |
|
|
|
|
|
HellblazerX
|
Posted: Wed Mar 29, 2006 8:54 pm Post subject: (No subject) |
|
|
in that case, why don't you just use the strint function to convert the String into an int, and perform the operations on the int, instead of getting the ASCII value of the number, and working with those. |
|
|
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 8:56 pm Post subject: (No subject) |
|
|
hmm...i'm really new at programing >_< just started this year |
|
|
|
|
|
person
|
Posted: Wed Mar 29, 2006 9:06 pm Post subject: (No subject) |
|
|
Try using the debugger menu to find whats wrong |
|
|
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 9:09 pm Post subject: (No subject) |
|
|
i told u i'm really new to this turing thing and i dont know anything about it, just want to pass computer science and move on @_@
how u use the debug menu...T-T |
|
|
|
|
|
darknoob
|
Posted: Wed Mar 29, 2006 9:12 pm Post subject: (No subject) |
|
|
it says i got error on the second last line...at least that is what i think it says -_- |
|
|
|
|
|
HellblazerX
|
Posted: Wed Mar 29, 2006 9:19 pm Post subject: (No subject) |
|
|
try this code instead:
code: | var num1 : real
var num2 : real
var operator : string
var number : string
get number
for i : 1 .. length (number)
if strintok (number (i)) = false and ord (number (i)) not= 46 then
num1 := strreal (number (1 .. (i - 1)))
operator := number (i)
num2 := strreal (number ((i + 1) .. *))
end if
end for
if ord (operator) = 43 then
put num1 + num2
elsif ord (operator) = 45 then
put num1 - num2
elsif ord (operator) = 42 then
put num1 * num2
elsif ord (operator) = 47 then
put num1 / num2
end if
|
basically, this code takes in the String input, and divides the String up based on where the operator is, the substring that cannot be converted to int. The first half is the first number, and the second half is the second number, and the operator is recorded. Then, the appropriate operation is performed based on the what operator is used. This code doesnt have any error checking, so its easy to crash if you add in spaces and other stuff |
|
|
|
|
|
|