Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: XVicarious on June 30, 2011, 09:13:38 pm

Title: [TI-8x] Help Optimizing Conversion Program
Post by: XVicarious on June 30, 2011, 09:13:38 pm
I really didn't know how to do this, so I did it the long way, how could optimizing this be done? Its to convert Octodecimal (Base 18) to Decimal (Base 10):

Code: [Select]
length(Str1)->O
For(I,1,O)
sub(Str1,I,1)->Str2
If (Str2="0")
R+(0*18^(O-I))->R
If (Str2="1")
R+(1*18^(O-I))->R
If (Str2="2")
R+(2*18^(O-I))->R
If (Str2="3")
R+(3*18^(O-I))->R
If (Str2="4")
R+(4*18^(O-I))->R
If (Str2="5")
R+(5*18^(O-I))->R
If (Str2="6")
R+(6*18^(O-I))->R
If (Str2="7")
R+(7*18^(O-I))->R
If (Str2="8")
R+(8*18^(O-I))->R
If (Str2="9")
R+(9*18^(O-I))->R
If (Str2="A")
R+(10*18^(O-I))->R
If (Str2="B")
R+(11*18^(O-I))->R
If (Str2="C")
R+(12*18^(O-I))->R
If (Str2="D")
R+(13*18^(O-I))->R
If (Str2="E")
R+(14*18^(O-I))->R
If (Str2="F")
R+(15*18^(O-I))->R
If (Str2="G")
R+(16*18^(O-I))->R
If (Str2="H")
R+(17*18^(O-I))->R
End
Title: Re: [TI-8x] Help Optimizing Conversion Program
Post by: fb39ca4 on June 30, 2011, 09:20:09 pm
Well, if the character in the string is a [decimal] number, then you can just use expr(Str2) to get the digit, so what you want to do is put the code for A-H first and if none of those match, use expr. Oh, and you can use else ifs instead of all but the first if.
Title: Re: [TI-8x] Help Optimizing Conversion Program
Post by: XVicarious on June 30, 2011, 09:34:18 pm
Thanks Runer112, your code works so much (in IRC)

Code: [Select]
Input "Octodec: ",Str1
length(Str1)->O
For(I,1,O)
inString("0123456789ABCDEFGH",sub(Str1,I,1))-1->A
(A*18^(O-I))+R->R
End
Disp R
Pause
Title: Re: [TI-8x] Help Optimizing Conversion Program
Post by: calc84maniac on June 30, 2011, 09:34:46 pm
Code: [Select]
0
For(I,1,length(Str2
18Ans-1+inString("0123456789ABCDEFGH",sub(Str2,I,1
End
Title: Re: [TI-8x] Help Optimizing Conversion Program
Post by: leafy on June 30, 2011, 09:34:49 pm
A slightly cheap way that I use is make a string called "0123456789ABCDEFGH" then use another for loop with sub( to find it in that string, then that should be the equivalent number.

EDIT: ninja'd
Title: Re: [TI-8x] Help Optimizing Conversion Program
Post by: XVicarious on June 30, 2011, 09:36:33 pm
Wow. Super optimization.