So I'm attempting to make a program that allows users to look up and solve physics formulas. Below is the code I use once a user has selected a formula and is trying to solve for an unknown in the formula. This works fine if the user only has one unknown in the formula.

So I am trying to add some code that allows users to search for a second, third, or fourth formula while solving the first formula, and then replace a variable in the first formula with the the value found for it in the second formula. (basic substitution of sorts)

For example, suppose I am trying to find the value of acceleration using the formula "A-V/T", which is Str1 in the code below (acceleration-velocity/time=0). Suppose I enter the value "4" for time, but don't know the value of "V", velocity, in this equation. So I look up a second formula, "V-X/T" (velocity-displacement/time=0), solve that for the value of "V" velocity, and then plug the value for "V" back into the initial formula "A-V/T" and am finally able to solve for acceleration.

Arrow Here is my issue: How the devil do I efficiently store all of the previously entered values for my letter variables, A-Z and theta, while I repeat the code block below to obtain the value for velocity in the second equation? When I come back to the initial equation, the value I previously entered for time would be gone. I had some ideas for storing all the variables in a list, but it didn't seem very efficient. Any ideas on how to write this code? Thank you!


Code:
DelVar A
DelVar B
DelVar C
DelVar D
DelVar E
DelVar F
DelVar G
DelVar H
DelVar I
DelVar J
DelVar K
DelVar L
DelVar M
DelVar N
DelVar O
DelVar P
DelVar Q
DelVar R
DelVar S
DelVar T
DelVar U
DelVar V
DelVar W
DelVar X
DelVar Y
DelVar Z
DelVar theta
ClrHome

//display formula (Str1) at top, note formula/equation is set to 0 of course
Disp " "
Disp " "
Output(1,1,"0="
Output(1,3,Str1
//eliminate any displayed comments in brackets in the actual formula
Str1->Str7
inString(Str1,"[",1->B
While B!=0
inString(Str1,"[",1->B
inString(Str1,"]",1->C
sub(Str1,1,B-1)+sub(Str1,C+1,length(Str1)-C)->Str1
inString(Str1,"[",1->B
End

//start prompting user for variables that appear in the formula.
//If the user enters "666" as a value, that value is interpreted as unknown to be solved for
//basically checks if the letter variable appears in the formula, if it does, prompts user for the value of that letter in the formula
inString(Str1,"A",1->A
If A!=0:Then:Input "A=",A:End:If A=666:Then:"A"->Str2:End:A->theta
inString(Str1,"X",1->A
If A!=0:Then:Input "X=",X:End:If X=666:Then:"X"->Str2:End
inString(Str1,"G",1->A
If A!=0:Then:Input "G=",G:End:If G=666:Then:"G"->Str2:End
inString(Str1,"V",1->A
If A!=0:Then:Input "V=",V:End:If V=666:Then:"V"->Str2:End
inString(Str1,"Q",1->A
If A!=0:Then:Input "Q=",Q:End:If Q=666:Then:"Q"->Str2:End
inString(Str1,"I",1->A
If A!=0:Then:Input "I=",I:End:If I=666:Then:"I"->Str2:End
inString(Str1,"B",1->A
If A!=0:Then:Input "B=",B:End:If B=666:Then:"B"->Str2:End
inString(Str1,"L",1->A
If A!=0:Then:Input "L=",L:End:If L=666:Then:"L"->Str2:End
inString(Str1,"Y",1->A
If A!=0:Then:Input "Y=",Y:End:If Y=666:Then:"Y"->Str2:End
inString(Str1,"C",1->A
If A!=0:Then:Input "C=",C:End:If C=666:Then:"C"->Str2:End
inString(Str1,"D",1->A
If A!=0:Then:Input "D=",D:End:If D=666:Then:"D"->Str2:End
inString(Str1,"P",1->A
If A!=0:Then:Input "P=",P:End:If P=666:Then:"P"->Str2:End
inString(Str1,"S",1->A
If A!=0:Then:Input "S=",S:End:If S=666:Then:"S"->Str2:End
inString(Str1,"K",1->A
If A!=0:Then:Input "K=",K:End:If K=666:Then:"K"->Str2:End
inString(Str1,"H",1->A
If A!=0:Then:Input "H=",H:End:If H=666:Then:"H"->Str2:End
inString(Str1,"E",1->A
If A!=0:Then:Input "E=",E:End:If E=666:Then:"E"->Str2:End
inString(Str1,"N",1->A
If A!=0:Then:Input "N=",N:End:If N=666:Then:"N"->Str2:End
inString(Str1,"M",1->A
If A!=0:Then:Input "M=",M:End:If M=666:Then:"M"->Str2:End
inString(Str1,"R",1->A
If A!=0:Then:Input "R=",R:End:If R=666:Then:"R"->Str2:End
inString(Str1,"W",1->A:
If A!=0:Then:Input "W=",W:End:If W=666:Then:"W"->Str2:End
inString(Str1,"T",1->A
If A!=0:Then:Input "T=",T:End:If T=666:Then:"T"->Str2:End
inString(Str1,"F",1->A
If A!=0:Then:Input "F=",F:End:If F=666:Then:"F"->Str2:End
theta->[D](1,1):0->theta
inString(Str1,"theta",1->A:If A!=0:Then:Input "theta=",theta:End:If theta=666:Then:"theta"->Str2:End
[D](1,1)->A
Str1->{Y1}

//find unknown variable, then solve for it. Display value of unknown.
//This code is VERY inefficient, look at the beauty of this copy and paste repetition...
//anyone have code optimization suggestions? haha
//I may make 2 versions of this app, one like this and one relying on the Celtic III library to save memory space
If "A"=Str2:Then:solve({Y1},A,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "B"=Str2:Then:solve({Y1},B,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "C"=Str2:Then:solve({Y1},C,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "D"=Str2:Then:solve({Y1},D,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "E"=Str2:Then:solve({Y1},E,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "F"=Str2:Then:solve({Y1},F,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "G"=Str2:Then:solve({Y1},G,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "H"=Str2:Then:solve({Y1},H,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "I"=Str2:Then:solve({Y1},I,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "J"=Str2:Then:solve({Y1},J,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "K"=Str2:Then:solve({Y1},K,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "L"=Str2:Then:solve({Y1},L,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "M"=Str2:Then:solve({Y1},M,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "N"=Str2:Then:solve({Y1},N,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "P"=Str2:Then:solve({Y1},P,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "Q"=Str2:Then:solve({Y1},Q,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "R"=Str2:Then:solve({Y1},R,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "S"=Str2:Then:solve({Y1},S,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "T"=Str2:Then:solve({Y1},T,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "U"=Str2:Then:solve({Y1},U,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "V"=Str2:Then:solve({Y1},V,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "W"=Str2:Then:solve({Y1},W,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "X"=Str2:Then:solve({Y1},X,0,{~1|E99,1|E99})->X:Disp X>Frac:Pause :prgmPHYSIKEY:End
If "Y"=Str2:Then:solve({Y1},Y,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
If "theta"=Str2:Then:solve({Y1},theta,0,{~1|E99,1|E99})->X:Disp X:Pause :prgmPHYSIKEY:End
I can't really help you with what you are asking, however..


Code:
DelVar ADelVar BDelVar CDelVar DDelVar EDelVar FDelVar GDelVar HDelVar IDelVar JDelVar KDelVar LDelVar MDelVar NDelVar ODelVar P elVar QDelVar RDelVar SDelVar TDelVar UDelVar VDelVar WDelVar XDelVar YDelVar ZDelVar theta


That will save you a little bit of space. DelVar doesn't need new lines or : inbetween them, and it's the only command that does this. Will save you.. 27 bytes? 26, something like that.
Wow... I did not know that trick. Thanks! I do wish I knew a way to use a string instead of a letter in the solve( feature though, that way I wouldn't have to use the code solve(Y1,[insert variable which I really wish could be accomplished with a string],0,{-1E99,1E99} for EVERY variable. I get the sneaky suspicion that there is a better way to do this... Beats me lol
As far as I can tell, you don't actually have to delete all of the previous variables because you reset their values when inputting them.

Also note that you can take apart and assemble strings - something you can use to automatically scan for variables via a For() loop - reducing the variable finder. "expr()" is a command that takes a string and executes it, putting its result in Ans for you - reducing the variable solver.
For your last code block:

Code:
:expr("solve({Y1},"+Str2+",0→X
:Pause Ans
:prgmPHYSIKEY
Wow, I feel like an imbecile... I forgot that there is an expr( command. Yikes. Thanks guys! This just sped up my program a gazillion times!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement