Yesterday two of my young "assistants" came visiting. And since we have a heat wave here in Norway now, we discussed how to make temerature conversion routines using CB.
I challenged them to do their own outines in a way so that the code was easy to use in other projects.
Graham posted a project many years ago: http://www.ionicwind.com/forums/index.php?topic=5113.msg38734#msg38734 (http://www.ionicwind.com/forums/index.php?topic=5113.msg38734#msg38734), where he stressed the importance of defining the variables correctly, so I was curious to see what the young boys came up with.
Their code surprised me. They use string variables and output formatting to present the results. And the output appears to be correct, without rounding errors!
Have fun!
Egil
'------------------------------------------------------------------------------
' temp_conv.cba
' Temperature conversions DEMO (Celcius to Farenheit and Farenheit to Celcius)
'------------------------------------------------------------------------------
DECLARE C2F(tc:string)
DECLARE F2C(tf:string)
DEF celcius,farenheit:string
OPENCONSOLE
INPUT "Degrees Celcius: ",celcius
print
print
farenheit = C2F(celcius)
Print using("####.#",val(celcius)) + "C =",
PRINT using("####.#",val(farenheit)) + "F"
PRINT
celcius = F2C(farenheit)
Print using("####.#",val(farenheit)) + "F =",
PRINT using("####.#",val(celcius)) + "C"
PRINT
PRINT
print"Press ANY KEY to end"
DO:UNTIL INKEY$<>""
CLOSECONSOLE
END
SUB C2F(tc:string)
'------------------------------------------------------------------------------
'Celcius to Farenheit: T(°Farenheit) = T(°Celcius) × 9/5 + 32
RETURN str$(((val(tc) * 9)/5) + 32)
SUB F2C(tf:string)
'------------------------------------------------------------------------------
'Farenheit to Celcius: T(°Celcius) = (T(°Farenheit) - 32) × 5/9
RETURN str$((val(tf)-32) * (5/9))
Egil,
Your assistants did very well, they deserve a pat on the back!.
Here is the IWB version of what they did:
'------------------------------------------------------------------------------
' temp_conv.iwb
' Temperature conversions DEMO (Celcius to Farenheit and Farenheit to Celcius)
'------------------------------------------------------------------------------
'DECLARE C2F(tc:string)
'DECLARE F2C(tf:string)
DEF celcius,farenheit:string
OPENCONSOLE
INPUT "Degrees Celcius: ",celcius
print
print
farenheit = C2F(celcius)
Print using("####.#",val(celcius)) + "C =",
PRINT using("####.#",val(farenheit)) + "F"
PRINT
celcius = F2C(farenheit)
Print using("####.#",val(farenheit)) + "F =",
PRINT using("####.#",val(celcius)) + "C"
PRINT
PRINT
print"Press ANY KEY to end"
DO:UNTIL INKEY$<>""
CLOSECONSOLE
END
SUB C2F(tc:string),string
'------------------------------------------------------------------------------
'Celcius to Farenheit: T(°Farenheit) = T(°Celcius) × 9/5 + 32
RETURN str$(((val(tc) * 9)/5) + 32)
endsub
SUB F2C(tf:string),string
'------------------------------------------------------------------------------
'Farenheit to Celcius: T(°Celcius) = (T(°Farenheit) - 32) × 5/9
RETURN str$((val(tf)-32) * (5/9))
endsub
Andy.
QuoteYour assistants did very well, they deserve a pat on the back!
I agree Andy!
Pity they are not allowed to use CB at school. But they have seen how I use CB for testing out new ideas and then convert the code to IWB, so they use the same way to develop algorithms in CB and then convert the code to Python, which is the compulsary coding language for use in norwegian schools.
BTW: Nice to see that you have taken up the habit yourself.... ;)
What puzles me now is why this "string" method works so good when comparing it to what I would have done myself (defined the variables as
float or
double).
Egil
Andy,
Finally came around to test your IWB version of the code. The IWB program gives wrong answers when calculating from F to C.
Replacing the F2C sub with the code below, rectifies the problem.
SUB F2C(tf:string),string
'------------------------------------------------------------------------------
'Farenheit to Celcius: T(°Celcius) = (T(°Farenheit) - 32) × 5/9
RETURN str$((val(tf) - 32) * 5/9)
endsub
CB is more "forgiving" when it comes to correct syntax compared to IWB. This corected code works equally well in both languages.
Egil
Egil,
Yes I probably would have used something like double and float too, I can only guess (and could be mistaken) but maybe the USING command is really IWB's version of something like C's SPRINTF command?
As for the F2C sub routine, strange, I didn't take any note of those results, I was just looking at the C to F figures, so well spotted!
Andy.