code_junkie

Best way to write a conversion function

Nov 10th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. convertTemperature (TempScale.CELSIUS, TempScale.FAHRENHEIT, 22)
  2.  
  3. enum Conversion
  4. {
  5. CelsiusToFahrenheit,
  6. FahrenheitToCelsius,
  7. KilosToPounds
  8. }
  9.  
  10. Convert(Conversion conversion, X from);
  11.  
  12. enum Units
  13. {
  14. Pounds,
  15. Kilos,
  16. Celcius,
  17. Farenheight
  18. }
  19.  
  20. Convert(Unit from, Unit to, X fromAmount);
  21.  
  22. Convert(Pounds, Celcius, 5, 10);
  23.  
  24. public void ConvertTemperature(TemperatureTypeEnum SourceTemp,
  25. TemperatureTypeEnum TargetTemp,
  26. decimal Temperature)
  27. {}
  28.  
  29. interface ITemperature
  30. {
  31. CelciusTemperature ToCelcius();
  32. FarenheitTemperature ToFarenheit();
  33. }
  34.  
  35. struct FarenheitTemperature : ITemperature
  36. {
  37. public readonly int Value;
  38. public FarenheitTemperature(int value)
  39. {
  40. this.Value = value;
  41. }
  42.  
  43. public FarenheitTemperature ToFarenheit() { return this; }
  44. public CelciusTemperature ToCelcius()
  45. {
  46. return new CelciusTemperature((this.Value - 32) * 5 / 9);
  47. }
  48.  
  49. }
  50.  
  51. struct CelciusTemperature
  52. {
  53. public readonly int Value;
  54. public CelciusTemperature(int value)
  55. {
  56. this.Value = value;
  57. }
  58.  
  59. public CelciusTemperature ToCelcius() { return this; }
  60. public FarenheitTemperature ToFarenheit()
  61. {
  62. return new FarenheitTemperature(this.Value * 9 / 5 + 32);
  63. }
  64. }
  65.  
  66. // Freezing
  67. Debug.Assert(new FarenheitTemperature(32).ToCelcius().Equals(new CelciusTemperature(0)));
  68. Debug.Assert(new CelciusTemperature(0).ToFarenheit().Equals(new FarenheitTemperature(32)));
  69.  
  70. // crossover
  71. Debug.Assert(new FarenheitTemperature(-40).ToCelcius().Equals(new CelciusTemperature(-40)));
  72. Debug.Assert(new CelciusTemperature(-40).ToFarenheit().Equals(new FarenheitTemperature(-40)));
  73.  
  74. CelciusTemperature theOutbackInAMidnightOilSong = new CelciusTemperature(45);
  75. FarenheitTemperature x = theOutbackInAMidnightOilSong; // ERROR: Cannot implicitly convert type 'CelciusTemperature' to 'FarenheitTemperature'
  76.  
  77. float LinearConvert(float in, float scale, float add, bool invert);
  78.  
  79. public class Temperature
  80. {
  81. private double celcius;
  82.  
  83. public static Temperature FromFarenheit(double farenheit)
  84. {
  85. return new Temperature { Farhenheit = farenheit };
  86. }
  87.  
  88. public static Temperature FromCelcius(double celcius)
  89. {
  90. return new Temperature { Celcius = celcius };
  91. }
  92.  
  93. public static Temperature FromKelvin(double kelvin)
  94. {
  95. return new Temperature { Kelvin = kelvin };
  96. }
  97.  
  98. private double kelvinToCelcius(double kelvin)
  99. {
  100. return 1; // insert formula here
  101. }
  102.  
  103. private double celciusToKelvin(double celcius)
  104. {
  105. return 1; // insert formula here
  106. }
  107.  
  108. private double farhenheitToCelcius(double farhenheit)
  109. {
  110. return 1; // insert formula here
  111. }
  112.  
  113. private double celciusToFarenheit(double kelvin)
  114. {
  115. return 1; // insert formula here
  116. }
  117.  
  118. public double Kelvin
  119. {
  120. get { return celciusToKelvin(celcius); }
  121. set { celcius = kelvinToCelcius(value); }
  122. }
  123.  
  124. public double Celcius
  125. {
  126. get { return celcius; }
  127. set { celcius = value; }
  128. }
  129.  
  130. public double Farhenheit
  131. {
  132. get { return celciusToFarenheit(celcius); }
  133. set { celcius = farhenheitToCelcius(value); }
  134. }
  135. }
  136.  
  137. $ units 'tempF(-40)' tempC
  138. -40
  139.  
  140. use Convert::Temperature;
  141.  
  142. my $c = new Convert::Temperature();
  143.  
  144. my $res = $c->from_fahr_to_cel('59');
  145.  
  146. double fahrtocel(double tempF){
  147. return ((tempF-32)*(5/9));
  148. }
  149.  
  150. double celtofahr(double tempC){
  151. return ((9/5)*tempC + 32);
  152. }
  153.  
  154. function Temperature($value, $input, $output)
  155. {
  156. $value = floatval($value);
  157.  
  158. if (isset($input, $output) === true)
  159. {
  160. switch ($input)
  161. {
  162. case 'K': $value = $value - 273.15; break; // Kelvin
  163. case 'F': $value = ($value - 32) * (5 / 9); break; // Fahrenheit
  164. case 'R': $value = ($value - 491.67) * (5 / 9); break; // Rankine
  165. }
  166.  
  167. switch ($output)
  168. {
  169. case 'K': $value = $value + 273.15; break; // Kelvin
  170. case 'F': $value = $value * (9 / 5) + 32; break; // Fahrenheit
  171. case 'R': $value = ($value + 273.15) * (9 / 5); break; // Rankine
  172. }
  173. }
  174.  
  175. return $value;
  176. }
Add Comment
Please, Sign In to add comment