繁体   English   中英

我正在尝试输入 200 个 X、Y 坐标的列表,然后输入 output 一个包含这些坐标的命令列表 C#

[英]I am trying to input a list of 200 X,Y coordinates and then output a list of commands with those coordinates in them C#

我正在尝试制作一个简单的工具 Windows 表单应用程序,我可以粘贴 200 个 X,Y 坐标的列表,然后我可以单击一个按钮,该工具将 output 一个文本文件,每行都有一个带有 X 的命令,Y 坐标。

输入如下所示:

65737,163129
-21687,-27399
164089,50153
164649,63465
-28663,140057
-28951,110329
149833,-30231

output 应该是这样的:

waypoint:WLM 1:1:78168:~:1560:1:true:0:gui.xaero_default:false:0:false
waypoint:WLM 2:2:919432:~:154200:11:true:0:gui.xaero_default:false:0:false
waypoint:WLM 3:3:791080:~:15624:0:true:0:gui.xaero_default:false:0:false
waypoint:WLM 4:4:79288:~:16968:6:true:0:gui.xaero_default:false:0:false
waypoint:WLM 5:5:79064:~:155702:9:true:0:gui.xaero_default:false:0:false

这是我到目前为止所拥有的,我能够将输入解析为一个数组,但现在我不知道如何组合它并将其保存为文本文件。

private void button2_Click(object sender, EventArgs e)
        {
            var lines = richTextBox1.Text.Split((new char[] { ',' }), StringSplitOptions.RemoveEmptyEntries);
            foreach (var s in lines)
            {



                string[] result = lines.ToArray();
                outputwindow.Text = String.Join("\n", result); // parse string so they are all on new lines ( they are still in the array as one tho)

                var cords = outputwindow.Text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // we split the data again and now the result is cords by them selves
                foreach (var c in cords)
                {
                    string[] cordstoarray = cords.ToArray(); // cordstoarry should now be the proper data set

                    //waypoint:name:initials:x:y:z:color:disabled:type:set:rotate_on_tp:tp_yaw:global

                    

                    decimal startnumb = startingnumberb.Value;

                    
                    // wc[1] + wc[0] + name + wc[0] + inl + wc[0] + cordstoarray[0] + wc[0] + wc[3] + wc[0] + cordstoarray[1] + wc[0] + wc[4]

                    

                    for (int i = 0; i < cordstoarray.Length; i++)
                    {
                        string[] buildwaypoints = new string[13]; 

                        string[] wc = { ":", "waypoint", "~", "1:false:0:gui.xaero_default:false:0:false" };

                        string name = nametextbox.Text; // get the name

                        string inl = initialstextbox.Text; // get the initials


     
                        buildwaypoints[i] = { wc[1] , wc[0] , name , wc[0] , inl , wc[0] , cordstoarray[i] , wc[0] , wc[3] , wc[0] , cordstoarray[i + 1] , wc[0] , wc[4]};  // Build wapypoint array
                       
                        File.WriteAllLines("Triwaypt.txt", buildwaypoints);
                        using (StreamReader sr = new StreamReader("Triwaypt.txt"))
                        {
                            string res = sr.ReadToEnd();
                            Console.WriteLine(res);
                        }

                    }
                    

                    




                    

                }
                    
            }
        }

您可以使用 File.WriteAllLines() - 但方式略有不同

首先添加List<string> outputLines = new List<string>(); 到程序的开头(就在 var lines =... 之后),以便您可以开始保存生成的行(或者如果您想节省内存,可以使用 File.AppendAllText)

然后,当您生成 buildWayPoints 数组时 - 只需生成一个字符串以添加到列表中。 就像是

outputLines.Add($"{wc[1]}{wc[0]}{name}{wc[0]}{inl}{wc[0]}{cordstoarray[i]}{wc[0]}{wc[3]}{wc[0]}{cordstoarray[i + 1]}{wc[0]}{wc[4]}");//assuming your code up to this point was correct, also you can replace the {wc[n]} with the literal text if you feel like it

应该可以,但我肯定会建议清理该部分的代码。 最后,在循环完成后,您需要将新的行列表写入文件,例如File.WriteAllLines(@"Filepath", outputLines)

假设您的问题围绕创建 output 文件,如果您的问题实际上是关于正确格式化输入,您需要更清楚地解释您的逻辑 - 但作为在黑暗中的刺

counter++;//define counter as int counter = 0; earlier
var twoCords = c.Split(',');
var outputLine = $"waypoint:WLM {counter}:{counter}:{c[0]}:~:{c[1]}:{idk about this one sorry}:true:0:gui.xaero_default:false:0:false";

ps 我需要提到的一个直接错误是你做了一个 foreach(var c in cords) 但是你在代码中引用了 cords 而不是 c

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM