我在此处制作个人资料之前先搜索了一下,试图缓解上述两个问题。
这是为了评估目的,我从一个网站上获得一个CSV文件,其中包含邻居房屋价格列表以及这些房产建造的年份。格式化为货币的价格列是另一种。代码的目的是在文本文件中返回建造年份和价格的高,低和平均值。通过手动将价格列格式化为常规并搜索异常的年份构建值(即一年构建的0或971),我可以得到我想要的结果。我必须将货币格式更改为常规才能使代码生效。我想通过执行异常值的过滤和代码中的格式化(如果可能)来简化过程。
这是我到目前为止所做的:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Prodominate_Values
{
class Program
{
static void Main(string[] args)
{
args = new[] { "C:/Users/Yeti/Desktop/Firm_Inventory.csv" };
var transactions = ReadTransactions(args[0]);
var averagePrice = CalculateAveragePrice(transactions);
var averageYear = CalculateAverageYear(transactions);
var lowPrice = transactions.Min(Transactions => Transactions.Price);
var highPrice = transactions.Max(Transactions => Transactions.Price);
var lowYear = transactions.Min(Transactions => Transactions.YearBuilt);
var highYear = transactions.Max(Transactions => Transactions.YearBuilt);
string[] lines = { lowPrice + " " + lowYear, highPrice + " " + highYear, averagePrice + " " + averageYear };
File.WriteAllLines(@"C:/Users/Yeti/Desktop/Firm_Inventory.txt", lines);
Console.WriteLine("Operation Completed");
Console.ReadLine();
}
static IList<Transaction> ReadTransactions (string path)
{
var list = new List<Transaction>();
foreach (var line in File.ReadLines(path).Skip(1))
{
Console.WriteLine(line);
list.Add(Transaction.FromLine(line));
}
return list;
}
static int CalculateAveragePrice(IEnumerable<Transaction> transactions)
{
var count = 0;
var total = 0;
foreach (var transaction in transactions)
{
count += 1;
total += transaction.Price;
}
return total / count;
}
static int CalculateAverageYear(IEnumerable<Transaction> transactions)
{
var count = 0;
var total = 0;
foreach (var transaction in transactions)
{
count += 1;
total += transaction.YearBuilt;
}
return total / count;
}
class Transaction
{
public int Price { get; set; }
public int YearBuilt { get; set; }
public static Transaction FromLine(string line)
{
var data = line.Split(',');
Console.WriteLine(Convert.ToString(int.Parse(data[1])));
return new Transaction()
{
Price = int.Parse(data[0]),
YearBuilt = int.Parse(data[1]),
};
}
}
}
}