mccaugh@s.cs.uiuc.edu (10/06/89)
> I need a simple piece of code to convert a string celsius/fahrenheit > to a fahrenheit/celsius. The input is of the form [digit][F|C] ie 32C, 0F > For some reason... the code below does not work! I need this by Monday morning > Can anyone out in netland help me out? :-) > ----------------------------------------------(code follows....) Shawn: > if ( input@ = 'F' ) then > begin > writeln( 'fahrenheit '); > convert := value; > end > else > begin > writeln( 'celsius '); > convert := value; > end; > end; > > begin > writeln( convert ); > end. > Your code correctly converts a string to an integer number, but that's all. In 1948, the Ninth General Conferenace on Weights & Measures decided to ex- change the name 'Centigrade' for 'Celsius' after the Swede who invented the scale in 1742. Since Fahrenheit ranges from 32 to 212 (= 180 pts) compared to 100 pts for Celsius (hence the original name 'Centigrade'), there exist 180/100 = 9/5 as many F values as C values, leaving each F interval 5/9 of a C interval. This, together with the necessity to correct for 0 yields: C = 5/9*(F - 32) Thus if you are given a value V known to be Fahrenheit and wish to convert it to Celsius: convert = 0.555555555*(V - 32.0). Solving for F: F = 9/5*C + 32 so if value V is know to be Celsius, convert = 1.8*C + 32.0. Hope this has helped! Scott McCaughrin University of Illinois Urbana, Illinois (mccaugh@s.cs.uiuc.edu)