Last month we created a small utility to perform common US/Metric Conversions. http://justbasic.conforums.com/index.cgi?board=tutorial&action=display&num=1133734936
If you happen to be working in astronomy (temperature of stellar bodies), physics (gas/volume equations), gas turbines or any other field where you need K° or R° temperatures, our little metric utility with only C° and F° will not be sufficient. This code performs ALL the conversions and displays ALL values for you.
To begin, let's define the units of measurements.
Celsius: Metric unit of temperature (water freezes at 0°C; boils at 100°C) Fahrenheit: US unit of temperature (water freezes at 32°F; boils at 212°F) Kelvin: Metric unit of absolute temperature (water freezes at 273°K; boils at 373°K) Rankine: US unit of absolute temperature (water freezes at 491.7°R; boils at 671.7°R)
NOTE¹ Absolute zero is the total absence of heat energy. There can be no negative values of K° or R° according to current physics, but new laws of physics are proposed with amazing frequency.
NOTE² The boiling point of pure water implies sea level air pressure, or 29.92 inches of mercury.
The CELSIUS scale was invented by Swedish astronomer Andres Celsius in the 17th century. Because the scale is evenly divided into 100 degrees between the freezing point and boiling point of water, it was originally called CENTIGRADE, but was changed to CELSIUS by an international committee on weights and measures in 1948. Today, CELSIUS is the most widely used unit of measure in the world.
The FAHRENHEIT scale was created by Gabriel Daniel Fahrenheit in 1714. Using the FAHRENHEIT scale, water freezes at 0° and boils at 212°.
The KELVIN scale was invented by William Thomson, also know as Lord Kelvin. On the KELVIN scale, 0° is equal to absolute zero, the coldest possible temperature. The KELVIN scale is essentially (temp° CELSIUS-273) or 273.15 if you want to be finicky.
In some texts, I have seen Rankin used, but this is an error. The RANKINE scale was proposed by William John Macquorn Rankine in 1859, therefore the correct spelling is RANKINE. Occasionally the abbreviation Ra° is used, to distinguish RANKINE units from REAUMUR units, abbreviated Re°, first proposed in the 18th century by R.A.F. de Reaumur of France. On the REAUMUR scale, water freezes at 0° and boils at 80°. REAUMUR units are rarely used in the scientific or engineering community today, so R is an acceptable abbreviation for RANKINE measurement.
The code below could be written with as few as five lines if using the MAINWIN and INPUT is degrees Celsius. Here, much of the code is used to setup the GUI or trap errors.
'GUI for Temperature Conversions
'by Welo, 1/08/06
tempOut$="########.#" 'Format for output values, max 99,999,999.9
NOMAINWIN
WindowWidth=400
WindowHeight=400
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)
STATICTEXT #conv.st1, "Please enter a temperature value:", 20, 10, 350, 20
STATICTEXT #conv.st2, "My known temperature units are:", 20, 70, 350, 20
STATICTEXT #conv.st3, "", 40, 170, 350, 20
STATICTEXT #conv.st4, "", 40, 200, 350, 20
STATICTEXT #conv.st5, "", 40, 230, 350, 20
STATICTEXT #conv.st6, "", 40, 260, 350, 20
TEXTBOX #conv.tb1, 140, 35, 100, 28
GROUPBOX #conv.gb1, "", 10, 95, 365, 55
RADIOBUTTON #conv.rb1, "C°", [cel],[nil],20,120,40,20
RADIOBUTTON #conv.rb2, "F°", [fah],[nil],120,120,40,20
RADIOBUTTON #conv.rb3, "K°", [kel],[nil],220,120,40,20
RADIOBUTTON #conv.rb4, "R°", [ran],[nil],320,120,40,20
BUTTON #conv.btn1, "NEW", [redo], UL, 80, 300, 60, 30
BUTTON #conv.btn2, "QUIT", [quit], UL, 240, 300, 60, 30
OPEN "Temperature Converter" FOR WINDOW AS #conv
PRINT #conv, "font verdana 12"
PRINT #conv.gb1, "!FONT TIMES_NEW_ROMAN 10 BOLD"
PRINT #conv.gb1, "Units"
PRINT #conv, "trapclose [quit]"
[redo] 'Return to here for NEW conversion
PRINT #conv.tb1, ""
PRINT #conv.tb1, "!setfocus"
'===== Do temperature conversions below here
WAIT
[cel] 'Input is CELSIUS
PRINT #conv.tb1, "!contents? temp$"
IF temp$="" THEN 'Check for valid input
GOSUB [oops]
GOTO [redo]
END IF
CALL checkValue temp$, temp 'Get numerical value of temp$
IF temp<-273.15 THEN 'Check again
GOSUB [neg]
GOTO [redo] 'Check again
END IF
C=temp
F=(temp*(9/5))+32
GOSUB [showConv]
PRINT #conv.rb1, "RESET" 'Reset radiobutton
WAIT
[fah] 'Input is FAHRENHEIT
PRINT #conv.tb1, "!contents? temp$"
IF temp$="" THEN 'Check for valid input
GOSUB [oops]
GOTO [redo]
END IF
CALL checkValue temp$, temp 'Get numerical value of temp$
IF temp<-459.67 THEN 'Check again
GOSUB [neg]
GOTO [redo]
END IF
F=temp
C=(F-32)*(5/9)
GOSUB [showConv]
PRINT #conv.rb2, "RESET" 'Reset radiobutton
WAIT
[kel] 'Input is KELVIN
PRINT #conv.tb1, "!contents? temp$"
IF temp$="" THEN 'Check for valid input
GOSUB [oops]
GOTO [redo]
END IF
CALL checkValue temp$, temp 'Get numerical value of temp$
IF temp<0 THEN 'Check for negative input
GOSUB [neg]
GOTO [redo]
END IF
C=(temp-273.15)
F=(C*(9/5))+32
GOSUB [showConv]
PRINT #conv.rb3, "RESET" 'Reset radiobutton
WAIT
[ran] 'Input is RANKINE
PRINT #conv.tb1, "!contents? temp$"
IF temp$="" THEN 'Check for valid input
GOSUB [oops]
GOTO [redo]
END IF
CALL checkValue temp$, temp 'Get numerical value of temp$
IF temp<0 THEN 'Check for negative input
GOSUB [neg]
GOTO [redo]
END IF
F=temp-459.67
C=(F-32)*(5/9)
GOSUB [showConv]
PRINT #conv.rb4, "RESET" 'Reset radiobutton
WAIT
[quit]
CLOSE #conv
END
'===== SUBS and error messages below here
[oops] 'If there has been no input
NOTICE "ERROR"+CHR$(13)+_
"Response not understood."+CHR$(13)+_
"Please try again."
RETURN
[neg] 'If the temperature is below absolute zero
NOTICE "ERROR"+CHR$(13)+_
"Your temperature is less than"+CHR$(13)+_
"absolute zero and is invalid."+CHR$(13)+CHR$(13)+_
"Please try again!"
RETURN
[showConv]
PRINT #conv.st3, USING(tempOut$,C); " degrees CELSIUS"
PRINT #conv.st4, USING(tempOut$,F); " degrees FAHRENHEIT"
PRINT #conv.st5, USING(tempOut$,(C+273.15)); " degrees KELVIN"
PRINT #conv.st6, USING(tempOut$,(F+459.67)); " degrees RANKINE"
RETURN
SUB checkValue temp$, byref temp
temp=VAL(temp$) 'Convert temp$ to numerical value
END SUB
RADIOBUTTONS are a safe way to get the user to branch to a selected routine and, using a GROUPBOX neatly arranges the buttons in a specific category. After I have made a conversion, I don't like to have the button continue to display a selected status, and Janet conveniently showed me how to RESET, or blank these buttons, when the action is complete. For "Everything You've Always Wanted to Know About Buttons, Checkboxes and Radiobuttons but Were Afraid to Ask," see Janet's article in LB NL#122. (http://babek.info/libertybasicfiles/lbnews/nl122/index.htm)
Resetting the buttons will not affect any conversions, it's simply the way I prefer to see the options presented. Whether the buttons are set or reset, you can click different buttons in sequence and the value entered in the textbox will be converted and the results displayed below.
One question the user might have is "Why get the user input as temp$ instead of the numerical value temp?" In VB, all input values must be a string. The only reason I chose to use a string here was to allow me to check for an error if the user had entered nothing. Values zero or less are permitted when the unit of measurement is C° or F°, but are not permitted when the unit is K° or R°. If the user forgets to enter a temperature value, temp$ will equal "" and an error is displayed. If a temperature less than absolute zero is entered, a different error message is displayed. If a user should accidentally enter "212°F", input will still be accepted because the VAL() function will truncate the string and see only 212, the numerical value we wish to use for conversion, and program execution will not continue until one of the RADIOBUTTONS has been selected.
To reduce the amount of coding needed in each branch routine, I used a SUB or GOSUB as much as possible. There will probably be many other ways a programmer might write this utility, but this approach is workable. Feel free to modify the code to suit your own needs.