Ale*_*lex 7 c# performance file-io
我做了一个家庭作业,这里是问题陈述:
您的程序应该如下工作:
使用程序员编写的方法计算风寒因子,并以下列形式显示结果:
对于t =来自file的温度和v =来自文件的风速Wind chill index =计算结果华氏度.
显示小数点后两位数的所有数字.(记住 - 没有神奇的数字!)
重复这些步骤,直到遇到文件结尾.
我已经完成了作业,我的代码在下面,我只是想知道是否有任何方法可以提高效率,或者如果有一些不同的创造性方法来解决这个问题,我已经把它转到了50/50 ,但我很好奇你们有些高级和熟练的程序员会如何解决这个问题.
using System;
using System.IO;
class Program
{
// declare constants to use in wind chill factor equation - no magic numbers
const double FIRST_EQUATION_NUMBER = 35.74;
const double SECOND_EQUATION_NUMBER = 0.6215;
const double THIRD_EQUATION_NUMBER = 35.75;
const double FOURTH_EQUATION_NUMBER = 0.4275;
const double EQUATION_EXPONENT = 0.16;
const int DEGREE_SYMBOL_NUMBER = 176;
static void Main()
{
// declare and initialize some variables
string filePath = "";
string line = "";
double temperature = 0.0;
double windSpeed = 0.0;
double windChillFactor = 0.0;
char degreeSymbol = (char)DEGREE_SYMBOL_NUMBER;
// ask user for a file path
Console.Write("Please enter a valid file path: ");
filePath = Console.ReadLine();
// create a new instance of the StreamReader class
StreamReader windChillDoc = new StreamReader(@filePath);
// start the read loop
do
{
// read in a line and save it as a string variable
line = windChillDoc.ReadLine();
// is resulting string empty? If not, continue execution
if (line != null)
{
string[] values = line.Split();
temperature = double.Parse(values[0]);
windSpeed = double.Parse(values[1]);
windChillFactor = WindChillCalc(temperature, windSpeed);
Console.WriteLine("\nFor a temperature {0:f2} F{1}", temperature, degreeSymbol);
Console.WriteLine("and a wind velocity {0:f2}mph", windSpeed);
Console.WriteLine("The wind chill factor = {0:f2}{1}\n", windChillFactor, degreeSymbol);
}
} while (line != null);
windChillDoc.Close();
Console.WriteLine("\nReached the end of the file, press enter to exit this program");
Console.ReadLine();
}//End Main()
/// <summary>
/// The WindChillCalc Method
/// Evaluates a wind chill factor at a given temperature and windspeed
/// </summary>
/// <param name="temperature">A given temperature</param>
/// <param name="ws">A given windspeed</param>
/// <returns>The calculated wind chill factor, as a double</returns>
static double WindChillCalc(double temperature, double ws)
{
double wci = 0.0;
wci = FIRST_EQUATION_NUMBER + (SECOND_EQUATION_NUMBER * temperature) - (THIRD_EQUATION_NUMBER * (Math.Pow(ws, EQUATION_EXPONENT))) + (FOURTH_EQUATION_NUMBER * temperature * (Math.Pow(ws, EQUATION_EXPONENT)));
return wci;
}
}//End class Program
Run Code Online (Sandbox Code Playgroud)
随意告诉我你的想法.
你的方式很好,但是:
while((line = windChillDoc.ReadLine()) != null)
{
...
}
[Darn格式化无法正常工作!]
除此之外,我不知道,因为我不熟悉天气计算:)
你的大部分评论都是无关紧要的.代码应告诉您如何......评论应告诉您原因.
string filePath = "";
...
filePath = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
不要使用从未使用过的值进行初始化; 并保持声明和初始化紧密相连:
string filePath = Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
using已被提及 - 但不要@不必要地使用:
new StreamReader(@filePath);
Run Code Online (Sandbox Code Playgroud)
应该只是:
new StreamReader(filePath);
Run Code Online (Sandbox Code Playgroud)
就个人而言,我会使用LINQ作为读卡器,但那只是我; -p