将华氏温度转换为摄氏温度
本文关键字:温度 转换 | 更新日期: 2023-09-27 18:29:38
我正在尝试将华氏温度转换为摄氏温度
做以下事情,我总是得到零:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Celcius_Farenheit_Converter
{
class Program
{
static double Celcius(double f)
{
double c = 5/9*(f - 32);
return c;
}
static void Main(string[] args)
{
string text = "enter a farenheit tempature";
double c = Celcius(GetTempature(text));
Console.WriteLine("the tempature in Celicus is {0}", c);
Console.ReadKey(true);
}
static double GetTempature(string text)
{
Console.WriteLine(text);
bool IsItTemp = false;
double x = 0;
do
{
IsItTemp = double.TryParse(Console.ReadLine(), out x);
} while (!IsItTemp);
return x;
}
}
}
你能帮我修一下吗?
5/9
执行整数除法—也就是说,它总是丢弃小数部分—因此它将始终返回0。
5.0/9.0
执行浮点除法,并将返回预期的0.55555…
试试这个:
static double Celcius(double f)
{
double c = 5.0/9.0 * (f - 32);
return c;
}
进一步阅读
- /运算符(C#参考)
将整数除法更改为浮点除法like;
double c = (5.0 / 9.0) * (f - 32);
来自C# Specification $7.7.2 Division operator
;
整数除法:
除法将结果四舍五入为零结果是小于两个操作数的商的绝对值。结果为零或当两个操作数具有相同符号且为零时为正,或当两个操作数具有相反的符号时为负数。
正如Frederik所说,只将其中一个变量更改为浮点类型也足以进行计算。(5 / 9f
或5f / 9
给出了正确的解决方案)
在前面的评论中,我建议使用struct
来封装温度转换逻辑。如果感兴趣的话,我在下面列出了一个粗略的散列(仅部分测试):
public struct Temperature: IComparable
{
#region fields
double value;
TemperatureUnit unit;
#endregion fields
#region constructors
public Temperature(double value, TemperatureUnit unit)
{
AssertUnitNotNull(unit);
AssertValueNotBelowAbsZero(value, unit);
this.value = value;
this.unit = unit;
}
public Temperature(Temperature temp, TemperatureUnit unit)
{
AssertUnitNotNull(unit);
this.value = ConvertUnit(temp.value, temp.unit, unit);
this.unit = unit;
}
#endregion constructors
#region properties
public double Value
{
get { return this.value; }
set
{
AssertValueNotBelowAbsZero(value, this.unit);
this.value = value;
}
}
public TemperatureUnit Unit
{
get { return this.unit; }
set
{
AssertUnitNotNull(value);
if (this.unit != value)
{
this.value = ConvertUnit(this.value, this.unit, value);
this.unit = value;
}
}
}
#endregion properties
#region methods
#region overridden methods
public override bool Equals(object obj)
{
return this.CompareTo(obj) == 0;
}
public override int GetHashCode()
{
return this.unit.ToKelvin(value).GetHashCode();
}
const string OutputFormat = "{0}{1}";
public override string ToString()
{
return string.Format(OutputFormat, this.value, this.unit.Symbol);
}
#endregion overridden methods
public int CompareTo(object obj)
{
Temperature? t = obj as Temperature?;
if (!t.HasValue) return -1;
return this.unit.ToKelvin(value).CompareTo(t.Value.unit.ToKelvin(t.Value.value));
}
#region operator overloads
public static Temperature operator +(Temperature a, double b)
{
return new Temperature(a.value + b, a.unit);
}
public static Temperature operator +(Temperature a, Temperature b)
{
return a + ConvertUnit(b.value, b.unit, a.unit);
}
public static Temperature operator +(Temperature a, TemperatureUnit b)
{
return new Temperature(a, b);
}
public static Temperature operator -(Temperature a, double b)
{
return new Temperature(a.value - b, a.unit);
}
public static Temperature operator -(Temperature a, Temperature b)
{
return a - ConvertUnit(b.value, b.unit, a.unit);
}
public static Temperature operator ++(Temperature a)
{
return new Temperature(a.value + 1.0, a.unit);
}
public static Temperature operator --(Temperature a)
{
return new Temperature(a.value - 1.0, a.unit);
}
public static Temperature operator /(Temperature a, double b)
{
return new Temperature(a.value / b, a.unit);
}
public static Temperature operator *(Temperature a, double b)
{
return new Temperature(a.value * b, a.unit);
}
#endregion operator overloads
#region helper methods
private static double ConvertUnit(double value, TemperatureUnit from, TemperatureUnit to)
{
return to.FromKelvin(from.ToKelvin(value));
}
#endregion helper methods
#endregion methods
#region static validation methods
private static void AssertUnitNotNull(TemperatureUnit unit)
{
if (unit == null) throw new ArgumentNullException();
}
private static void AssertValueNotBelowAbsZero(double value, TemperatureUnit unit)
{
if (unit.ToKelvin(value) < 0.0) throw new TemperatureIsBelowAbsoluteZeroException(value,unit);
}
#endregion static validation methods
}
public sealed class TemperatureUnit
{
#region delegate definitions
delegate double Conversion(double source);
#endregion delegate definitions
#region attributes
readonly string name;
readonly string symbol;
//base all functions around Kelvin since that allows us to restrict values to zero and above
readonly Conversion fromKelvin;
readonly Conversion toKelvin;
#endregion attributes
#region constructors
private TemperatureUnit(string name, string symbol, Conversion fromKelvin, Conversion toKelvin)
{
this.name = name;
this.symbol = symbol;
this.fromKelvin = fromKelvin;
this.toKelvin = toKelvin;
}
#endregion constructors
#region properties
public string Name { get { return this.name; } }
public string Symbol { get { return this.symbol; } }
#region defined units
public static TemperatureUnit Kelvin = new TemperatureUnit("Kelvin", "'u212A", delegate(double d) { return d; }, delegate(double d) { return d; });
public static TemperatureUnit Celcius = new TemperatureUnit("Celcius", "'u2103", KelvinToCelcius, CelciusToKelvin);
public static TemperatureUnit Farenheit = new TemperatureUnit("Farenheit", "'u2109", KelvinToFarenheit, FarenheitToKelvin);
public static TemperatureUnit Rankine = new TemperatureUnit("Rankine", "'u00B0Ra", KelvinToRankine, RankineToKelvin);
public static TemperatureUnit Romer = new TemperatureUnit("R'u03B8mer", "'u00B0R'u03B8", KelvinToRomer, RomerToKelvin);
public static TemperatureUnit Newton = new TemperatureUnit("Newton", "'u00B0N", KelvinToNewton, NewtonToKelvin);
public static TemperatureUnit Delisle = new TemperatureUnit("Delisle", "'u00B0D", KelvinToDelisle, DelisleToKelvin);
public static TemperatureUnit Reaumur = new TemperatureUnit("R'u00E9amur", "'u00B0R'u00E9", KelvinToReaumur, ReaumurToKelvin);
#endregion defined units
#endregion properties
#region functions
public double FromKelvin(double kelvin)
{
return this.fromKelvin(kelvin);
}
public double ToKelvin(double value)
{
return this.toKelvin(value);
}
#endregion functions
#region overridden methods
public override bool Equals(object obj)
{
TemperatureUnit tu = obj as TemperatureUnit;
if (tu == null) return false;
return this.name.Equals(tu.name);
}
public override int GetHashCode()
{
return this.name.GetHashCode();
}
public override string ToString()
{
return this.name.ToString();
}
#endregion overridden methods
#region static conversion functions
#region Celcius
const double KelvinToCelciusOffset = -273.15;
public static double CelciusToKelvin(double celcius)
{
return celcius - KelvinToCelciusOffset;
}
public static double KelvinToCelcius(double kelvin)
{
return kelvin + KelvinToCelciusOffset;
}
#endregion Celcius
#region Fahrenheit
//Fahrenheit [°F] = [K] × 9⁄5 − 459.67 [K] = ([°F] + 459.67) × 5⁄9
const double KelvinToFarenheitMultiplier = 9.0 / 5.0;
const double KelvinToFarenheitOffset = -459.67;
public static double FarenheitToKelvin(double farenheit)
{
return (farenheit - KelvinToFarenheitOffset) / KelvinToFarenheitMultiplier;
}
public static double KelvinToFarenheit(double kelvin)
{
return kelvin * KelvinToFarenheitMultiplier + KelvinToFarenheitOffset;
}
#endregion Fahrenheit
#region Rankine
const double KelvinToRankineMultiplier = KelvinToFarenheitMultiplier;
public static double RankineToKelvin(double rankine)
{
return rankine / KelvinToRankineMultiplier;
}
public static double KelvinToRankine(double kelvin)
{
return kelvin * KelvinToRankineMultiplier;
}
#endregion Rankine
#region Romer
//[K] = ([°Rø] − 7.5) × 40⁄21 + 273.15 [°Rø] = ([K] − 273.15) × 21⁄40 + 7.5
const double KelvinToRomerMultiplier = 21.0 / 40.0;
const double KelvinToRomerOffset1 = KelvinToCelciusOffset;
const double KelvinToRomerOffset2 = 7.5;
public static double RomerToKelvin(double romer)
{
return (romer - KelvinToRomerOffset2) / KelvinToRomerMultiplier - KelvinToRomerOffset1;
}
public static double KelvinToRomer(double kelvin)
{
return (kelvin + KelvinToRomerOffset1) * KelvinToRomerMultiplier + KelvinToRomerOffset2;
}
#endregion Romer
#region Newton
//[K] = [°N] × 100⁄33 + 273.15 [°N] = ([K] − 273.15) × 33⁄100
const double KelvinToNewtonMultiplier = 33.0 / 100.0;
const double KelvinToNewtonOffset = KelvinToCelciusOffset;
public static double NewtonToKelvin(double newton)
{
return (newton / KelvinToNewtonMultiplier) - KelvinToNewtonOffset;
}
public static double KelvinToNewton(double kelvin)
{
return (kelvin + KelvinToNewtonOffset) * KelvinToNewtonMultiplier;
}
#endregion Newton
#region Delisle
//[K] = 373.15 − [°De] × 2⁄3 [°De] = (373.15 − [K]) × 3⁄2
const double KelvinToDelisleMultiplier = 1.5;
const double KelvinToDelisleOffset = 373.15;
public static double DelisleToKelvin(double delisle)
{
return KelvinToDelisleOffset - delisle / KelvinToDelisleMultiplier;
}
public static double KelvinToDelisle(double kelvin)
{
return (KelvinToDelisleOffset - kelvin) * KelvinToDelisleMultiplier;
}
#endregion Delisle
#region Reaumur
//[K] = [°Ré] × 5⁄4 + 273.15 [°Ré] = ([K] − 273.15) × 4⁄5
const double KelvinToReaumurMultiplier = 4.0 / 5.0;
const double KelvinToReaumurOffset = KelvinToCelciusOffset;
public static double ReaumurToKelvin(double reaumur)
{
return reaumur / KelvinToReaumurMultiplier - KelvinToReaumurOffset;
}
public static double KelvinToReaumur(double kelvin)
{
return (kelvin + KelvinToReaumurOffset) * KelvinToReaumurMultiplier;
}
#endregion Reaumur
#endregion static conversion functions
}
public class TemperatureIsBelowAbsoluteZeroException : Exception
{
public TemperatureIsBelowAbsoluteZeroException() : base() { }
public TemperatureIsBelowAbsoluteZeroException(string message) : base(message) { }
public TemperatureIsBelowAbsoluteZeroException(string message, Exception innerException) : base(message,innerException) { }
public TemperatureIsBelowAbsoluteZeroException(System.Runtime.Serialization.SerializationInfo info,System.Runtime.Serialization.StreamingContext context) : base(info,context) { }
const string ErrorMessageFormat = "Value '{0}{1}' is below absolute zero!";
public TemperatureIsBelowAbsoluteZeroException(double value, TemperatureUnit unit) : base(string.Format(ErrorMessageFormat, value, unit.Symbol)) { }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class TempConvert
{
static void Main(string[] args)
{
//Variables declared to hold and test conversion
//floats used to avoid int calculation errors
float originalFarenheit;
float centigrade;
float returnFarenheit;
Console.Write("Enter Temperature (Farenheit): "); //take temp to be converted
originalFarenheit = float.Parse(Console.ReadLine()); //hold as float var
centigrade = ((originalFarenheit - 32) / 9) * 5; //Convert to centrigrade
returnFarenheit = ((centigrade / 5) * 9) + 32; //test conversion by reversing
Console.WriteLine("Centigrade = :" + centigrade); //Display result
Console.WriteLine("Return Farenheit = :" + returnFarenheit); //Test result
Console.ReadKey();
}
}
}
这是一种通用方法,将执行所有温度单位转换
public static double ConvertTemperatureUnits(TemperatureUnit toConvert, TemperatureUnit from, double value)
{
double convertedValue = 0.0;
if (toConvert == from)
return value;
switch (toConvert)
{
case TemperatureUnit.FAHRENHEIT:
{
switch (from)
{
case TemperatureUnit.CELSIUS:
convertedValue = (value * 9) / 5 + 32;
break;
case TemperatureUnit.KELVIN:
convertedValue = 1.8 * (value - 273.15) + 32;
break;
}
}
break;
case TemperatureUnit.KELVIN:
switch (from)
{
case TemperatureUnit.CELSIUS:
convertedValue = value + 273.15;
break;
case TemperatureUnit.FAHRENHEIT:
convertedValue = (value + 459.67) * 5 / 9;
break;
}
break;
case TemperatureUnit.CELSIUS:
switch (from)
{
case TemperatureUnit.KELVIN:
convertedValue = value - 273.15;
break;
case TemperatureUnit.FAHRENHEIT:
convertedValue = (value - 32) * 5 / 9;
break;
}
break;
}
return convertedValue;
}
创建类似的TemperatureUnit枚举
enum TemperatureUnit
{
FAHRENHEIT,
KELVIN,
CELSIUS
}