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:

DelVar LFor(M,1,1
If L=M
   Input ":",Str1
   Str1+" ->Str1
   Ans+" ->Str1
   DelVar |LLDelVar |LM
   If sub(Str1,P,1)="(
      If sub(Str1,P,1)=")
         If 1=dim(L2
         Goto C
         If inString("abcdefghijklmnopqrstuvwxyz",sub(Str1,P+1,1
         .5(1+inString("H HeLiBeB C N O F NeNaMgAlSiP S ClArK CaScTiV CrMnFeCoNiCuZnGaGeAsSeBrKrRbSrY ZrNbMoTcRuRhPdAgCdInSnSbTeI XeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaW ReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaU NpPuAmCmBkCfEsFmMdNoLr",Ans
         If fPart(Ans
         Goto C
      While inString("0123456789",sub(Str1,P+1+Ans,1
      If S
If 1=dim(L2
Goto E
Lbl C
Lbl E
assuming you can modify the program to have the user input lists like this: "{1,2,3,...,999}", You can do this:


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?


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...

Input "CF=",Str3
Str3+" ->Str3
While Str3!="
   If not(inString("abcdefghiklmnoprstuvy",Ans
   3-(0 or inString(Str2,"-->A
   If inString(Ans,sub(Str3,1,1:Then
      If inString(Ans,sub(Str3,2,1:Then
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