Post Reply 
CNV: A Function to Convert Units
10-27-2015, 08:34 PM (This post was last modified: 04-24-2017 09:59 PM by epp.)
Post: #1
CNV: A Function to Convert Units
A program to convert units for length, volume, time, or weight. After installing CNV click on the Toolbox, select the USER tab, and choose CNV. The calculator may be in Textbook or RPN modes. Under Textbook specify the input as a function parameter:

CNV(5)

With RPN enter the number, press ENTER, then invoke CNV:

5 ENTER CNV

You will be prompted to indicate whether the conversion is for Length, Volume, Time, or Weight. Then indicate the units you are converting from and to. If units are specified on input then you will only be prompted to enter output units and the results will include units. The program remembers your selections for input and output units for the next time.

CNV(1) --> 2.54
CNV(1_inch) --> 2.54_cm

Code:

#pragma mode(separator(.,;) integer(h32))

// when changing menus update the following: cFr, cTo, unit, prog

cFr = {1,1,1,1};
cTo = {1,1,1,1};
iPr;

EXPORT CNV(x)
begin
  local unit = {
    {"mm", "cm", "m", "km", "inch", "ft", "yd", "mile"},
    {"ml", "l", "tsp", "tbsp", "cu", "qt", "ozfl", "inch^3", "ft^3"},
    {"s", "min", "h", "d", "yr"},
    {"g", "kg", "oz", "lb"}
  };
  local prog = {
    "Length",
    "Volume",
    "Time",
    "Weight"
  };

  local t = type(x);
  local x0 = x;
  local uFr, uTo, n;

  if (t <> 0) and (t <> 9) then msgbox("Input must be REAL or UNIT"); return x0; end;
  choose(iPr, "convert", prog);
  if (iPr == 0) then return x0; end;

  if (t == 0) then
    n := cFr[iPr];
    choose(n, "From", unit[iPr]);
    if (n == 0) then return x0; end;
    uFr := expr("1_" + unit[iPr,n]);
    x := x * uFr;
    cFr[iPr] := n;
  end;

  n := cTo[iPr];
  choose(n, "To", unit[iPr]);
  if (n == 0) then return x0; end;
  uTo := expr("1_" + unit[iPr,n]);
  x := convert(x, uTo);
  cTo[iPr] := n;

  if (t == 0) then x := x / uTo; else x; end;
end;
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
CNV: A Function to Convert Units - epp - 10-27-2015 08:34 PM



User(s) browsing this thread: 1 Guest(s)