I actually wrote this a few days ago and uploaded it to the archives, but was too busy to make a post.

This is a molar mass calculator, for calculating the mass in grams of one mole of a substance.
It supports parenthesis, and you can even nest them.
There is also a silent mode where, if you put a string in Ans, the program will use that rather than prompting the user.
Unfortunately, I haven't found a way to distinguish between a string and a list. Please let me know if there is a way to do this.

Here's the source if anyone wants to obsessively optimize this:


Code:
DelVar LFor(M,1,1
End
Ans->L
Ans->M
If L=M
Then
   Input ":",Str1
   Str1+" ->Str1
Else
   Ans+" ->Str1
   DelVar |LLDelVar |LM
End
{1.00794,4.0026,6.941,9.01218,10.81,12.0111,14.0067,15.9994,18.998403,20.179,22.98977,24.305,26.98154,28.0855,30.97376,32.06,35.453,39.948,39.0983,40.08,44.9559,47.88,50.9415,51.996,54.938,55.847,58.9332,58.69,63.546,65.39,69.72,72.59,74.9216,78.96,79.904,83.8,85.4678,87.62,88.9059,91.224,92.9064,95.94,98,101.07,102.906,106.42,107.868,112.41,114.82,118.71,121.75,127.6,126.905,131.29,132.905,137.33,138.906,140.12,140.908,144.24,145,150.36,151.96,157.25,158.925,162.5,164.93,167.26,168.934,173.04,174.967,178.49,180.948,183.85,186.207,190.2,192.22,195.08,196.967,200.59,204.383,207.2,208.98,209,210,222,223,226.025,227.028,232.038,231.036,238.029,237.048,244,243,247,247,251,252,257,258,259,260,261,262,263,262->L1
length(Str1->L
{0->L2
For(P,1,L-1
   If sub(Str1,P,1)="(
   Then
      1+dim(L2->dim(L2
   Else
      If sub(Str1,P,1)=")
      Then
         If 1=dim(L2
         Goto C
         L2(dim(L2->M
         dim(L2)-1->dim(L2
      Else
         If inString("abcdefghijklmnopqrstuvwxyz",sub(Str1,P+1,1
         Then
            P+1->P
            sub(Str1,P-1,2
         Else
            sub(Str1,P,1)+"
         End
         .5(1+inString("H HeLiBeB C N O F NeNaMgAlSiP S ClArK CaScTiV CrMnFeCoNiCuZnGaGeAsSeBrKrRbSrY ZrNbMoTcRuRhPdAgCdInSnSbTeI XeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaW ReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaU NpPuAmCmBkCfEsFmMdNoLr",Ans
         If fPart(Ans
         Goto C
         L1(Ans->M
      End
      0
      While inString("0123456789",sub(Str1,P+1+Ans,1
         Ans+1
      End
      Ans->S
      M
      If S
      Ans*expr(sub(Str1,P+1,S
      Ans+L2(dim(L2->L2(dim(L2
      P+S->P
   End
End
If 1=dim(L2
Goto E
Lbl C
{~1->L2
Lbl E
L2(1
assuming you can modify the program to have the user input lists like this: "{1,2,3,...,999}", You can do this:


Code:

if inString(Ans,"{") //Ans is a list
expr(Ans->ʟINPUT  //expr it and store it into list INPUT
//else Ans is a string
//process it here
Well, the program takes a string formatted like "H2O" or "NaCl" as an input. The only reason I want to tell between a list and a string is that if Ans is a string, it should use Ans as input, and if it is a list (or a number, etc.), it should prompt the user for input.
I don't know what's going to be in the Ans var when the program starts, as the user might have run a program outputting a list to Ans and then run the program, expecting it to do a prompt as Ans is not a string.
It would be neat if toString(Strn) would return Strn, as then I could tell a list and valid input apart without crashing.
Command what about making this the first line of your code dealing with Ans?


Code:

If(!(inString(toString(Ans),"{"))    // Lists converted to strings preserve the {}
Stop // Error out of program here
or just avoid all the confusion and have the user enter it in, or have an option that reads Ans, then asks the user if Ans is the input or not.
That defeats the point of using Ans as the input, which is to allow you to use the program as a subroutine in a larger program. I guess I could have a switch variable that changes whether it reads from Ans or from the user, but if that got set accidentally, it could be really confusing to the user. I also couldn't set the variable to the "user input" value since that would clear Ans, making it impossible to read from the string.

Also, reading from Ans and then asking if we should use Ans would cause crashes if trying to store a list to a string.

Acag, toString of a string will crash.
Your code is nearly identical to the Molar Mass Calculator programs I uploaded to the archives about 4 years ago...

Code:
{1.01,4,6.94,9.01,10.81,12.01,14.01,16,19,20.18,22.99,24.31,26.98,28.09,30.97,32.07,35.45,39.95,39.1,40.08,44.96,47.87,50.94,52,54.94,55.85,58.93,58.69,63.55,65.39,69.72,72.61,74.92,78.96,79.9,83.8,85.47,87.62,88.91,91.22,92.91,95.94,98,101.07,102.91,106.42,107.87,112.41,114.82,118.71,121.76,127.6,126.9,131.29,132.91,137.33,138.91,140.12,140.91,144.24,145,150.36,151.96,157.25,158.93,162.5,164.93,167.26,168.93,173.04,174.97,178.49,180.95,183.84,186.21,190.23,192.22,195.08,196.97,200.59,204.38,207.2,208.98,209,210,222,223,226,227,232.04,231.04,238.03,237,244,243,247,247,251,252,257,258,259,266,267,268,269,270,269,278,281,282,285,286,289,289,293,294,294->|LW
{0->|LI
Ans->|LE
Input "CF=",Str3
Str3+" ->Str3
"sub(Str3,A,length(Str3)-A+1->|u
While Str3!="
   sub(Str3,1,1->Str2
   sub(Str3,2,1
   If not(inString("abcdefghiklmnoprstuvy",Ans
   "-
   Str2+Ans->Str2
   1+inString("H-HeLiBeB-C-N-O-F-NeNaMgAlSiP-S-ClArK-CaScTiV-CrMnFeCoNiCuZnGaGeAsSeBrKrRbSrY-ZrNbMoTcRuRhPdAgCdInSnSbTeI-XeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaW-ReOsIrPtAuHgTiPbBiPoAtRnFrRaAcThPaU-NpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCnNhFlMcLvTsOg",Ans
   |LW(Ans/2)->|LE(1+dim(|LE
   3-(0 or inString(Str2,"-->A
   |u->Str3
   "0123456789
   If inString(Ans,sub(Str3,1,1:Then
      If inString(Ans,sub(Str3,2,1:Then
         expr(sub(Str3,1,2->|LI(1+dim(|LI
         3->A
         |u->Str3
      Else
         expr(sub(Str3,1,1->|LI(1+dim(|LI
         2->A
         |u->Str3
      End
   Else
      1->|LI(1+dim(|LI
   End
End
Pause sum(|LE|LI
DelVar |LEDelVar |LIDelVar |LW
commandblockguy: http://tibasicdev.wikidot.com/68k:ans
You can use ans(num) and put it in quotes and it will be a string automatically.
ACagliano wrote:
commandblockguy: http://tibasicdev.wikidot.com/68k:ans
You can use ans(num) and put it in quotes and it will be a string automatically.

that's 68k
  
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