Turing-Help with Fractions
Author |
Message |
skyterror999
|
Posted: Wed Nov 17, 2010 4:34 pm Post subject: Turing-Help with Fractions |
|
|
What is it you are trying to achieve?
I'm have an assignment that requires me to make a 'fraction calculator'
What is the problem you are having?
The thing is, it only works for fractions with a single digit numerator and denominator. If the numerator or denominator has 2 digits or more, the output looks bad. I'll show you what I mean:
Turing: |
Good: Bad:
3 4 7 33 44 77
- + - = - - + - = -
5 5 5 5 5 5
|
How do I make it so that the line between the numerator and denominators expands with the length of the digits of the numerator and denominator?
Describe what you have tried to solve this problem
<Answer Here>
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
procedure Add
var numerator12, numerator22, denominator, sumnumerator : int := 0
var numlength1, numlength2, denlength1, denlength2, gcdlength, space1, space2 : int
numerator12 := numerator1 * denominator2
numerator22 := numerator2 * denominator1
denominator := denominator1 * denominator2
sumnumerator := numerator12 + numerator22
GCD (sumnumerator, denominator) %performs procedure GCD to calculate GCD
var gcdnum : int := sumnumerator div gcd
var gcdden : int := denominator div gcd
put "**************************** ADDITION **************************"
put ""
put "The sum is: "
put ""
put numerator1 : 6, numerator2 : 6, gcdnum : 6
put " " : 5, "-" : 3, "+" : 3, "-" : 3, "=" : 2, "-", "-" : 3
put denominator1 : 6, denominator2 : 6, gcdden : 6
end Add
|
Please specify what version of Turing you are using
Turing 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Wed Nov 17, 2010 7:15 pm Post subject: RE:Turing-Help with Fractions |
|
|
use an if statement before you draw your option.
if length(number) > 1 then
you know you'll need more "-"
else ....etc
thats one way you could do it, ofcourse theres an easier method, but try it out first! |
|
|
|
|
|
skyterror999
|
Posted: Wed Nov 17, 2010 7:25 pm Post subject: Re: Turing-Help with Fractions |
|
|
here's my code so far (not sure if u can understand it...) :
Turing: |
procedure Add
var numerator12, numerator22, denominator, sumnumerator : int := 0
var numlength1, numlength2, denlength1, denlength2, gcdlength, spacenum1, spacenum2, spaceden1, spaceden2, spacegnum, spacegden, numorden1, numorden2, gcdor, gcdnumlength, gcddenlength : int := 0
numerator12 := numerator1 * denominator2
numerator22 := numerator2 * denominator1
denominator := denominator1 * denominator2
sumnumerator := numerator12 + numerator22
GCD (sumnumerator, denominator ) %GCD
var gcdnum : int := sumnumerator div gcd
var gcdden : int := denominator div gcd
numlength1 := length (intstr (numerator1 )) %finds the length of first numerator
numlength2 := length (intstr (numerator2 )) %length of second numerator
denlength1 := length (intstr (denominator1 )) %length of first denominator
denlength2 := length (intstr (denominator2 )) %length of second denominator
gcdlength := length (intstr (gcdnum )) %length of numerator of result
gcdnumlength := gcdlength %length of numerator of result (alternate)
gcddenlength := length (intstr (gcdden )) %length of denominator of result
put "**************************** ADDITION **************************"
put ""
put "The sum is: "
put ""
%%%%%%%%%%%%%%%%%%% spaces %%%%%%%%%%%%%%
for i : 1 .. numlength1 %adds spaces for num of second fraction
spacenum1 := spacenum1 + 1
end for
for i : 1 .. denlength1 %adds spaces for den of second fraction
spaceden1 := spaceden1 + 1
end for
for i : 1 .. numlength2 %adds spaces for num of second fraction
spacenum2 := spacenum2 + 1
end for
for i : 1 .. denlength2 %adds spaces for den of second fraction
spaceden2 := spaceden2 + 1
end for
for i : 1 .. gcdnumlength %adds spaces for num of result
spacegnum := spacegnum + 1
end for
for i : 1 .. gcddenlength %adds spaces for den of result
spacegden := spacegden + 1
end for
if denlength1 > numlength1 then %adds spaces for num of second fraction
spacenum1 := spacenum1 + denlength1 + 1 - numlength2
end if
if denlength1 < numlength1 then %adds spaces for den of second fraction
spaceden1 := spaceden1 + numlength1 + 1 - denlength2
end if
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% outputs result %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
put numerator1, numerator2 : 5 + spacenum1 + spacenum2 - numlength1, gcdnum : 4 + spacegnum - numlength2 + spacenum2
if numlength1 >= denlength1 then
numorden1 := numlength1
elsif denlength1 > numlength1 then
numorden1 := denlength1
end if
if numlength2 >= denlength2 then
numorden2 := numlength2
elsif denlength2 > numlength2 then
numorden2 := denlength2
end if
if gcdnumlength >= gcddenlength then
gcdor := gcdnumlength
elsif gcddenlength > gcdnumlength then
gcdor := gcddenlength
end if
for i : 1 .. numorden1 - 1
put "-" ..
end for
put "-" : 3, "+" : 3 ..
for i : 1 .. numorden2 - 1
put "-" ..
end for
put "-" : 3, "=" : 2 ..
for i : 1 .. gcdor - 1
put "-" ..
end for
put "-"
put denominator1, denominator2 : 5 + spaceden1 + spaceden2 - denlength1, gcdden : 3 + spacegden - denlength2 + spaceden2 + spacenum2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end Add
|
|
|
|
|
|
|
|
|