我想通过使用公式https://m.wikihow.com/Find-the-Distance-Between-Two-Points计算2点之间的距离。但是如何将公式写入其中。如何将var更改为double?因为我无法执行数学运算,因为数组是var 以下是我的示例代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console1
{
class Program
{
static void Main(string[] args)
{
DataTable dt1= ConvertToDataTable(@"C:\Users\manchunl\Desktop\Sample1.txt", 10);
DataTable dt2= ConvertToDataTable2(@"C:\Users\manchunl\Desktop\Sample2.txt", 10);
foreach (DataRow row in dt1.AsEnumerable())
{
string.Join(",", row.ItemArray.Select(x => x.ToString()));
}
foreach (DataRow row in dt2.AsEnumerable())
{
string.Join(",", row.ItemArray.Select(x => x.ToString()));
}
foreach (DataRow row1 in dt1.Rows)
{
foreach (DataRow row2 in dt2.Rows)
{
var array1 = row1.ItemArray;
var array2 = row2.ItemArray;
if (array2[4].Equals(array1[4]))
{
var x1 = array1[7];
var y1 = array1[8];
var x2 = array2[7];
var y2 = array2[8];
}
}
}
Console.WriteLine();
Console.WriteLine("Press enter to exit.");
Console.Read();
}
public static DataTable ConvertToDataTable(string filePath, int numberOfColumns)
{
DataTable tbl = new DataTable();
for (int col = 0; col < numberOfColumns; col++)
tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));
string[] lines = System.IO.File.ReadAllLines(filePath);
foreach (string line in lines)
{
var cols = line.Split(null);
DataRow dr = tbl.NewRow();
for (int cIndex = 0; cIndex < numberOfColumns; cIndex++)
{
dr[cIndex] = cols[cIndex];
}
tbl.Rows.Add(dr);
}
return tbl;
}
public static DataTable ConvertToDataTable2(string filePath, int numberOfColumns)
{
DataTable tbl = new DataTable();
for (int col = 0; col < numberOfColumns; col++)
tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));
string[] lines = System.IO.File.ReadAllLines(filePath);
foreach (string line in lines)
{
var cols = line.Split(',');
DataRow dr = tbl.NewRow();
for (int cIndex = 0; cIndex < numberOfColumns; cIndex++)
{
dr[cIndex] = cols[cIndex];
}
tbl.Rows.Add(dr);
}
return tbl;
}
}
}