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