- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题是我之前问过的问题的延伸。
Setting a reference number and comparing that to other data in textfile
我在文本文件中有一组 X 和 Y 数据坐标。
Recorded Data 1
X: 1081.02409791506 Y:136.538121516361
Data collected at 208786.9115
Recorded Data 2
X: 1082.82841293328 Y:139.344405668078
Data collected at 208810.0446
Recorded Data 4
X: 1525.397051187 Y:1163.1786031393
Data collected at 245756.8823
Recorded Data 5
X: 1524.98201445054 Y:1166.38589429581
Data collected at 245769.489
Recorded Data 6
X: 509.002294087998 Y:913.213486303154
Data collected at 277906.6251
Recorded Data 7
X: 479.826998339658 Y:902.689393940613
Data collected at 277912.9958
我想将第一组数据 X: 1081.02409791506 Y:136.538121516361
设置为引用点,然后用下一组数据 X 和 Y 分别减去自身,并检查结果值是否X 和 Y 差值都在 100
以内,如果是,则继续操作。引用点应继续对以下数字执行此操作,直到它超出 ± 100 范围
。一旦超出 100 范围
,现在数据集是 X: 1525.397051187 Y:1163.1786031393
因为第一个数据和这个数据的差值超过了 100
,那么现在这组数据就是下一个引用点,做同样的事情,将下面的数据相减,检查结果值是否在100
之内。一旦超出 100 范围
,下一个数字是 X: 509.002294087998 Y:913.213486303154
,现在,这是新的引用点,并执行相同的操作。那是我的目标。简而言之,引用点应移至新文件中。
此代码能够执行上述操作,但仅适用于下面显示的数字。
278
299
315
360
389
400
568
579
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ReadTextFile
{
class Program
{
static void Main(string[] args)
{ string inputFile = @"C:\Users\Student\Desktop\ConsoleApp1\ConsoleApp1\Data\TextFile.txt"; // INPUT FILE
string outputFile = @"C:\Users\Student\Desktop\Test.txt"; // OUTPUT FILE
string[] data = File.ReadAllLines(inputFile); // READING FORM FILE
int TotalLine = data.Length; // COUNT TOTAL NUMBER OF ROWS
List<string> FinalList = new List<string>(); // INITIALIZE LIST FOR FINAL RESULT
double CurrentNumber = double.Parse(data[0]), NextNumber, diff; // INITIALIZE OF LOCAL VARIABLES, CURRENT NUMBER = FIRST NUMBER FROM FILE
for (int cntr = 1; cntr < TotalLine; cntr++) // FOR LOOP FOR EACH LINE
{
NextNumber = double.Parse(data[cntr]); //PARSING NEXT NUMBER
diff = CurrentNumber - NextNumber; // GETTING DIFFERENCE
if (diff <= 100 && diff >= -100) // MATCH THE DIFFERENCE
{
continue; // SKIP THE LOGIC IF DIFFERENCE IS LESS THEN 100
}
else
{
FinalList.Add(CurrentNumber.ToString()); // ADDING THE NUMBER TO LIST
CurrentNumber = NextNumber; // POINTING TO NEXT NUMBER
}
}
FinalList.Add(CurrentNumber.ToString()); // ADDING LAST NUMBER
foreach (string d in FinalList) // FOR EACH LOOP TO PRINT THE FINAL LIST
Console.WriteLine(d);
File.WriteAllLines(outputFile, FinalList); // SAVING TO THE FILE
}
我如何对 2 个坐标执行相同的操作?
第一个条件:X或Y至少有1个微分值在±100范围
之外,该组数据为新的引用数据。
第二个条件:如果x和Y的差值都在±100范围内
,我们必须继续操作。
最佳答案
这里有一个解决方案,前提是源文件内容如上所述:
Recorded Data 1
X: 1081.02409791506 Y:136.538121516361
Data collected at 208786.9115Recorded Data 2
X: 1082.82841293328 Y:139.344405668078
Data collected at 208810.0446..
目标文件如下:
X: 1081.02409791506 Y:136.538121516361
X: 1525.397051187 Y:1163.1786031393
..
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var points = ParseFromFile(
@"C:\Users\Student\Desktop\ConsoleApp1\ConsoleApp1\Data\TextFile.txt");
RenderToFile(
@"C:\Users\Student\Desktop\Test.txt",
Merge(points).ToArray());
}
static void RenderToFile(string fileName, (double x, double y)[] points)
{
var formatProvider = new CultureInfo("en-US", false);
var builder = new StringBuilder();
foreach (var point in points)
{
builder.Append(
$"X: {point.x.ToString(formatProvider)} Y:{point.y.ToString(formatProvider)}");
}
System.IO.File.WriteAllText(fileName, builder.ToString());
}
static (double x, double y)[] ParseFromFile(string fileName)
{
return Parse(System.IO.File.ReadAllText(fileName)).ToArray();
}
static IEnumerable<(double x, double y)> Merge((double x, double y)[] points)
{
points = points ?? throw new ArgumentNullException(nameof(points));
if (points.Length == 0) yield break;
var std = 100;
var current = points[0];
if (points.Length == 1)
{
yield return current;
yield break;
}
for (var i = 1; i < points.Length; i++)
{
var dx = Math.Abs(points[i].x - current.x);
var dy = Math.Abs(points[i].y - current.y);
if (dx <= std && dy <= std)
{
continue;
}
yield return current;
current = points[i];
}
yield return current;
}
static IEnumerable<(double x, double y)> Parse(string raw)
{
var formatProvider = new CultureInfo("en-US", false);
var pattern = new Regex(@"^[Xx][:] (?<x>\d*([.]\d+)?) [Yy][:](?<y>\d*([.]\d+)?)$");
raw = raw ?? throw new ArgumentNullException(nameof(raw));
foreach (var line in raw.Split(
Environment.NewLine, StringSplitOptions.RemoveEmptyEntries).Where(
line => line.ToLowerInvariant().StartsWith("x")))
{
var match = pattern.Match(line);
var xToken = match.Groups["x"].Value.Trim();
var yToken = match.Groups["y"].Value.Trim();
var x = double.Parse(xToken, formatProvider);
var y = double.Parse(yToken, formatProvider);
yield return (x: x, y: y);
}
}
}
}
首先我们需要解析数据。
需要格式提供程序来解析 double
从带有小数点分隔符的固定字符串中正确地 .
.
// can parse 1525.397051187, but not 1525,397051187
// en-US is the format you comply with
// 'false' is required to use the default culture settings, not any user overrided
var formatProvider = new CultureInfo("en-US", false);
该模式确保我们解析 x
和 y
正确协调。
// X: 1525.397051187 Y:1163.1786031393
// we use named groups to capture x (?<x>\d*([.]\d+)?)
// and y (?<y>\d*([.]\d+)?)
var pattern = new Regex(@"^[Xx][:] (?<x>\d*([.]\d+)?) [Yy][:](?<y>\d*([.]\d+)?)$");
解析后,我们可以根据您的规范合并 (x,y) 坐标。 std
是我们的增量允许的标准偏差(dx
,dy
)。
var dx = Math.Abs(points[i].x - current.x);
var dy = Math.Abs(points[i].y - current.y);
if (dx <= std && dy <= std)
{
continue;
}
关于 IEnumerable<T>
的注释:
使用它作为返回值允许我们使用 yield
句法。这称为生成器函数。
关于值元组的注释 (double x, double y)
:
我们可以使用命名元组来避免创建“愚蠢的”中间类。
关于c# - 引用 2 个坐标并检查差值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862434/
我的 C 代码有问题。我所做的就是这样: #include int main() { float zahlen[2]; for (int i = 0; i < 2; i++) {
我是一名优秀的程序员,十分优秀!