Author |
Topic: Temperatures converter (Read 199 times) |
|
Optimax
New Member
member is offline
Posts: 11
|
|
Temperatures converter
« Thread started on: May 11th, 2017, 09:13am » |
|
There must be a lot of such programs out there. The aim here was to try the method <SUB ButtonHandler handle$>. I read a very interessant article over this on the Liberty Basic Programmer's Encyclopedia. Multiple SUBs can be packed together into a single SUB. I wrote 6 separate functions for the calculations, but as they are only one-line arithmetics, I packed those also in the ButtonHandler. We need though an extra-SUB 'Quit' for the "trapclose Quit". Here is also a handle$ required, at least in LB4.5. I noticed also that a number like - 100 produces an error, there must be no space after the minus sign: -100; I was not aware of this this.
Code:
'TEMPERATURES.BAS LBB 3.07 / LB 4.5 11-05-2017
'================
' Converting temperatures between Celsius (°C), Fahrenheit (°F) and Kelvin (°K)
' Only two variables: T for any input Temperature, R for any conversion Result
' Minimalist GUI for easy code reading
' GLOBAL T, R 'for possible later use
' ---------------- First dimension and center the window, which will be opened later, after creating the buttons
NOMAINWIN
WindowHeight = 600
WindowWidth = 400
UpperLeftX = INT((DisplayWidth - WindowWidth) / 2)
UpperLeftY = INT((DisplayHeight - WindowHeight) / 2)
' ---------------- Create the buttons now before the window opens; they all branch to <SUB ButtonHandler handle$>
BUTTON #w.c2f, "Celsius => Fahrenheit", ButtonHandler, UL, 80, 20, 200, 50
BUTTON #w.f2c, "Fahrenheit => Celsius", ButtonHandler, UL, 80, 90, 200, 50
BUTTON #w.c2k, "Celsius => Kelvin ", ButtonHandler, UL, 80, 160, 210, 50
BUTTON #w.k2c, "Kelvin => Celsius ", ButtonHandler, UL, 80, 230, 200, 50
BUTTON #w.f2k, "Fahrenheit => Kelvin ", ButtonHandler, UL, 80, 300, 200, 50
BUTTON #w.k2f, "Kelvin => Fahrenheit ", ButtonHandler, UL, 80, 370, 200, 50
BUTTON #w.out, "E&XIT" , ButtonHandler, UL, 300, 470, 70, 70 'exits also by pressing ALT-X
' ---------------- Now open the window and wait for a buttonclick
OPEN "Celsius to Fahrenheit to Kelvin" FOR WINDOW AS #w
PRINT #w, "trapclose Quit"
WAIT
' ---------------- And here the ButtonHandler, including the needed simple temperature arithmetics (no functions)
SUB ButtonHandler handle$
Extension$ = RIGHT$(handle$, 3) 'this means the extension "abc" of a button-definition like "BUTTON #w.abc"
SELECT CASE Extension$
CASE "c2f"
PROMPT "Convert C° to F° " + CHR$(13) + "Enter here Celsius degrees :"; T ''' input Temperature
IF T < -273.15 THEN NOTICE "Mistake, T° is below absolute zero !": EXIT SUB ''' error message
R = T * 9 / 5 + 32 ''' conversion Result
NOTICE STR$(T) +" °C converted to °F = " + CHR$(13); R; " °F" ''' display Result
CASE "f2c"
PROMPT "Convert °F to °C " + CHR$(13) + "Enter here Fahrenheit degrees :"; T
IF F < -459.7 THEN NOTICE "Mistake, T° is below absolute zero !": EXIT SUB
R = (T -32) * 5 / 9
NOTICE STR$(T) + " °F converted to °C = " + CHR$(13); R; " °C"
CASE "c2k"
PROMPT "Convert °C to °K" + CHR$(13) + "Enter here Celsius degrees :"; T
IF T < -273.15 THEN NOTICE "Mistake, T° is below absolute zero !": EXIT SUB
R = T + 273.15
NOTICE STR$(T) + "°C converted to °K = " + CHR$(13); R; " °K"
CASE "k2c"
PROMPT "Convert °K to °C" + CHR$(13) +"Enter here Kelvin degrees :"; T
IF T < 0 THEN NOTICE "Mistake, T° is below absolute zero !": EXIT SUB
R = T -273.15
NOTICE STR$(T) + "°K converted to °C = "; R; " °C"
CASE "f2k"
PROMPT "Convert °F to °K" + CHR$(13) + "Enter here Fahrenheit degrees :"; T
IF T < -459.7 THEN NOTICE "Mistake, T° is below absolute zero !": EXIT SUB
R = (T - 32) * 5 / 9 + 459.7
NOTICE STR$(T) + "°F converted to °K = " + CHR$(13); R; " °K"
CASE "k2f"
PROMPT "Convert °K to °F" + CHR$(13) + "Enter here Kelvin degrees :"; T
IF T < 0 THEN NOTICE "Mistake, T° is below absolute zero !": EXIT SUB
R = (T - 273.15) * 9 / 5 + 32
NOTICE STR$(K) + "K° to F° = " + CHR$(13); R; " °F"
CASE "out"
CLOSE #w
END
END SELECT
END SUB
SUB Quit handle$
CLOSE #handle$
END
END SUB
Error notifications about coding, style or calculations are sincerely welcome.
|
« Last Edit: May 11th, 2017, 09:51am by Optimax » |
Logged
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
Re: Temperatures converter
« Reply #1 on: May 11th, 2017, 2:39pm » |
|
Since all calculations (end error checks) are one-liners it all could be calculated with EVAL
The only problem I seriously don't like these two lines Code: cond$=word$("T < -273.15|T < -459.7|T < -273.15|T < 0|T < -459.7|T < 0", n, "|")
fml$=word$("T * 9 / 5 + 32|(T -32) * 5 / 9|T + 273.15|T -273.15|(T - 32) * 5 / 9 + 459.7|(T - 273.15) * 9 / 5 + 32", n, "|")
Code:'TEMPERATURES.BAS LBB 3.07 / LB 4.5 11-05-2017
'================
' Converting temperatures between Celsius (°C), Fahrenheit (°F) and Kelvin (°K)
' Only two variables: T for any input Temperature, R for any conversion Result
' Minimalist GUI for easy code reading
' GLOBAL T, R 'for possible later use
' ---------------- First dimension and center the window, which will be opened later, after creating the buttons
NOMAINWIN
WindowHeight = 600
WindowWidth = 400
UpperLeftX = INT((DisplayWidth - WindowWidth) / 2)
UpperLeftY = INT((DisplayHeight - WindowHeight) / 2)
' ---------------- Create the buttons now before the window opens; they all branch to <SUB ButtonHandler handle$>
BUTTON #w.c2f, "c2f", ButtonHandler, UL, 80, 20, 200, 50
BUTTON #w.f2c, "f2c", ButtonHandler, UL, 80, 90, 200, 50
BUTTON #w.c2k, "c2k", ButtonHandler, UL, 80, 160, 210, 50
BUTTON #w.k2c, "k2c", ButtonHandler, UL, 80, 230, 200, 50
BUTTON #w.f2k, "f2k", ButtonHandler, UL, 80, 300, 200, 50
BUTTON #w.k2f, "k2f", ButtonHandler, UL, 80, 370, 200, 50
BUTTON #w.out, "E&XIT" , ButtonHandler, UL, 300, 470, 70, 70 'exits also by pressing ALT-X
' ---------------- Now open the window and wait for a buttonclick
OPEN "Celsius to Fahrenheit to Kelvin" FOR WINDOW AS #w
PRINT #w, "trapclose Quit"
for i = 1 to 6
lbl$=word$("c2f f2c c2k k2c f2k k2f", i)
h$="#w.";lbl$
#h$ Name$(left$(lbl$,1));" => "; Name$(right$(lbl$,1))
next
WAIT
' ---------------- And here the ButtonHandler, including the needed simple temperature arithmetics (no functions)
SUB ButtonHandler handle$
Extension$ = RIGHT$(handle$, 3) 'this means the extension "abc" of a button-definition like "BUTTON #w.abc"
if Extension$ ="out" then
CLOSE #w
END
end if
fr$=left$(Extension$,1)
to$=right$(Extension$,1)
pos=instr("c2f f2c c2k k2c f2k k2f",Extension$)
n=1+(pos-1)/4 'actually I need word number here. To get words from strings below.
cond$=word$("T < -273.15|T < -459.7|T < -273.15|T < 0|T < -459.7|T < 0", n, "|")
fml$=word$("T * 9 / 5 + 32|(T -32) * 5 / 9|T + 273.15|T -273.15|(T - 32) * 5 / 9 + 459.7|(T - 273.15) * 9 / 5 + 32", n, "|")
msg$="Convert ";nm$(fr$);" to ";nm$(to$);" " + CHR$(13) + "Enter here ";Name$(fr$);" degrees :"
PROMPT msg$; T ''' input Temperature
IF eval(cond$) THEN NOTICE "Mistake, ";nm$(fr$);" is below absolute zero !": EXIT SUB ''' error message
R = eval(fml$) ''' conversion Result
NOTICE STR$(T) +" ";nm$(fr$);" converted to ";nm$(to$);" = " + CHR$(13); R; " ";nm$(to$) ''' display Result
END SUB
SUB Quit handle$
CLOSE #handle$
END
END SUB
function Name$(a$)
Name$=word$("Fahrenheit Celsius Kelvin", instr("fck", a$))
end function
function nm$(a$)
nm$ = upper$(a$)+"°"
end function EDIT: got rid of last SELECT CASE in function Name$(a$)
|
« Last Edit: May 11th, 2017, 2:44pm by tsh73 » |
Logged
|
|
|
|
Optimax
New Member
member is offline
Posts: 11
|
|
Re: Temperatures converter
« Reply #2 on: May 11th, 2017, 4:08pm » |
|
Strong code ! Works fine. Thanks for your interest.
|
|
Logged
|
|
|
|
|