C#将数据与其他数据关联 [英] C# Associate datas to other datas

查看:74
本文介绍了C#将数据与其他数据关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我想在同一个字符串[]中关联不同的字符串[]。

我的不同字符串:

- 字符串[] 名称

- 字符串[]

- 字符串[] 数据



数据的每3个数据对应轴的3个数据的每组3个数据对应于名称的唯一数据。它在订单中。



我该怎么做?



这是我当前的程序,它允许我按我的意愿创建不同的字符串:



Hello !

I want to associate different strings [] in a same string [].
My different strings :
- string[] names
- string[] axes
- string[] datas

Every 3 datas of datas corresponds to 3 datas of axes and every group of 3 datas of axes corresponds to an only data of name. It's in the order.

How can I do that ?

Here my current programm which allows me to create my different strings as I want :

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using System;

public class Recup_donnees_6 : MonoBehaviour {

    // When the script instance is being loaded
    void Awake ()
    {
        Application.targetFrameRate = 25; // To indicate : 25 Fps (Frame per rate)
    }

            // Use this for initialization
         void Start() // We need to get datas from a text file (so only once time)
        {

            // Create an instance of StreamReader to read from a file
            StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt");

            using (reader) { // Automatic Closing of the Stream after working with it , otherwise we would have to write "reader.Close" at the end to close the stream, beneath the last command's line of this part "class FileReader"

                string line = " ";
                int lineNumber = 10; // The first line of the text file is the number 0
                // skip first 10 lines
                for (int i = 0; i < lineNumber; i++) {


                    line = reader.ReadLine();
                    if (line == null) return; // this ensures that the file has at least 12 lines
                }

                // At this point our line contains row #10 (Suj01:RIAS,,,Suj01:LIAS,,,Suj01:LIPS,,,Suj01:RIPS,,,Suj01:DSTR,,,Suj01:C7,,,Suj01:RACR,,,)
                string line_10;
                line_10 = line; // To indicate that the line #10 we read with the string "line" corresponds to the string "line_10"
                string[] names = line_10.Split (new String[] {",",",,,"},StringSplitOptions.RemoveEmptyEntries); // Every name is separating by ',,,' and the first is writing behind ','

                line = reader.ReadLine(); // Here it's line #11 (Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X)
                // To parse it and use it however you need
                string line_11;
                line_11 = line;
                string[] axes = line_11.Split(new String[] {"Field #",","},StringSplitOptions.RemoveEmptyEntries); // Every axe is separating by ',' and the first is writing behind 'Field #,'
           

                int counter = 0;
                while (line != null) { // Until the end of the text file
                    counter++;
                    line = reader.ReadLine(); // Here the loop starts getting data
                    if ((counter %3) != 1)
                    continue;
                    // if it is exactly 1 (lines 1, 5, 9 etc) continue working
                    // here you're beginning with the lines you need to enter into the matrix
                    // (1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,)
                    string lines_datas; // To create a matrix which contains 1 line / 4 from text file
                    lines_datas = line;
                    string[] datas;
                    datas = lines_datas.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries); 

                    line = reader.ReadLine(); // This needs to be last line in the while so you can continue the loop


                }
            }

    }
}









我需要一些帮助。

我想了解我如何编程!



谢谢:)





I need some helps.
I want to understand to how I can programm !

Thank you :)

推荐答案

我在这里说实话,说......嗯......那不是很好。



不要这样做:

I'm going to be honest here, and say...um...that's not very good.

Don't do this:
StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt");

 using (reader) { // Automatic Closing of the Stream after working with it , otherwise we would have to write "reader.Close" at the end to close the stream, beneath the last command's line of this part "class FileReader"



使用块的的优点是,它将变量超出范围并在完成时将其保留。所以总是这样使用块:


The advantage of the using block is that it takes the variable out of scope and destorys it on completion. So always do using blocks this way:

using (StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt"))
   {
   ...
   }





不要评论代码的作用,评论它的用途解释代码。您显示的使用块不需要评论来说明您使用它的原因:每个学过C#show的人都知道它的作用!



根本不打扰用户使用StreamReader:使用



Don't comment what the code does, comment where it is going to explain code. The using block you show doesn't need comments to say why you are using it: everyone who has learned C# show know what it does!

Don't bother user a StreamReader at all: use

string[lines] = File.ReadAllLines("Suj01_PI_DP_C00_1.txt");

相反,并使用它返回的数据数组。然后你只需要使用for循环来分组,如果它们是如何组织的那样。



如果你正在创建一个类,并且需要一次性 - 执行代码,然后为类创建构造函数。创建实例时,将调用构造函数。如果你需要每个应用程序执行一次,那么创建一个静态构造函数:这将在你第一次使用它时调用,而不会再次调用。



关联数据是什么类都是关于:所以创建一个具有Name,三个Axes和三个Datas的类 - 然后为每个需要创建的Name创建一个类的实例并填充它的数据。然后保留新类的集合以在创建它们时保存所有实例。

instead, and work with the array of data it returns. Then you just use a for loop to run through the lines, in groups if that's how they are organised.

If you are creating a class, and need one-time-executed code, then create a constructor for the class. When an instance is created, the constructor is called. If you need it executed once per application, then create a static constructor: that will be called the first time you class it used, and never again.

"Associating data" is what classes are all about: So create a class that has a Name, and three Axes, and three Datas - then you create an instance of the class for each Name you need to create and fill in it's data. You then keep a collection of the new class to hold all the instances as you create them.


感谢您的澄清。我认为你需要创建两个类:

Thank you for clarification. I think you need to create two classes:
class Axe
{
    private double x = .0;
    private double y = .0;
    private double z = .0;

    public Axe(double _x, double _y, double _z)
    {
        x = _x;
        y = _y;
        z = _z;
    }

    public double X
    {
        get{return x;}
        set{x = value;}
    }

    public double Y
    {
        get{return y;}
        set{y = value;}
    }

    public double Z
    {
        get{return z;}
        set{z = value;}
    }

}


class MyData
{
    private string aName = string.Empty;
    private List<Axe> axes = new List<Axe>();

    public MyData(string _name, List<Axe> _axes)
    {
        aName = _name;
        axes = _axes;
    }

    public string Name
    {
        get{return aName;}
        set {aName = value;}
    }

    public List<Axe> Axes
    {
        get{return axes;}
        set{axes = value;}
    }

}



如您所见,第一个( Ax )用于存储单个 X Y Z 值。第二个( MyData )是单个名称 Suj01:RIAS 的持有者和的列表Ax 's;)



用法:


As you can see, the first one (Axe) is used to store single X, Y and Z values. The second one (MyData) is a holder for single name Suj01:RIAS and list of Axe's ;)

Usage:

List<MyData> datas = new List<MyData>
{
    new MyData("Suj01:RIAS", new List<Axe>{new Axe(.22, 5.12, 2.33), new Axe(8.22, 15.12, 22.33), new Axe(4.22, 9.12, 9.33)}),
    new MyData("Suj02:RIAS", new List<Axe>{new Axe(.11, 5.32, 2.55), new Axe(8.11, 15.32, 22.55), new Axe(4.11, 9.32, 9.55)})
};







想象一下,现在你可以通过Linq [ ^ ]查询,例如:




Imagine, that now you're able to get statistical data via using Linq[^] queries, for example:

var qry = from d in datas
    select new
    {
        Name = d.Name,
        AvgX = d.Axes.Average(a=>a.X).ToString("#0.00"),
        AvgY = d.Axes.Average(a=>a.Y).ToString("#0.00"),
        AvgZ = d.Axes.Average(a=>a.Z).ToString("#0.00"),
        SumX = d.Axes.Sum(a=>a.X).ToString("#0.00"),
        SumY = d.Axes.Sum(a=>a.Y).ToString("#0.00"),
        SumZ = d.Axes.Sum(a=>a.Z).ToString("#0.00"),
        Total = d.Axes.Sum(a=>a.X+a.Y+a.Z).ToString("#0.00")
    };





结果:



Result:

Name       AvgX AvgY AvgZ  SumX  SumY  SumZ  Total
Suj01:RIAS 4.22 9.79 11.33 12.66 29.36 33.99 76.01
Suj02:RIAS 4.11 9.99 11.55 12.33 29.96 34.65 76.94





[/编辑]



我想读这个:演练:创建自己的集合类 [ ^ ]





请注意,我不确定我是否理解正确,但我希望我的答案能够很好地开始讨论OOP编程。



[/EDIT]

I'd sugest to read this: Walkthrough: Creating Your Own Collection Class[^]


Note that i'm not sure i understand you correctly, but i hope that my answer will be good point to start discuss about OOP programming.


这篇关于C#将数据与其他数据关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆