C# 中二维数组的方程

发布于 2024-12-10 02:06:24 字数 297 浏览 0 评论 0原文

我有一个 310 行和 120 列的数组。

我每隔 10 秒就会从串行端口获取填充该数组的数据。

如何将此数据发送到数组的第一行,并在下一次迭代时连续发送到该行,直到操作完成?

完成后,取数组的最大值、最小值和平均值。最后是数组中所选单元格的平均值、最大值和最小值。

C# 中的数组可以实现吗?

([7,31]-xxxxx.MIN([$28$5:$45$95])/(xxxxx.MAX[$28$5:$46$95]-xxxxx.MIN[$28$5:$45$95])

I have an array with 310 lines and 120 columns.

I get the data that will populate this array from serial port at intervals of 10 seconds.

How to send this data to the first row of the array and on the next iteration to the line down and it continuously until the operation is complete?

Once completed, take the maximum, minimum and average of the array. And finally, the average, maximum and minimum of a selection of cells in the Array.

This is possible with array in C#?

([7,31]-xxxxx.MIN([$28$5:$45$95])/(xxxxx.MAX[$28$5:$46$95]-xxxxx.MIN[$28$5:$45$95])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临风闻羌笛 2024-12-17 02:06:24

您在问题下面发表的评论澄清了一点,但评论的扭曲性质使这一行变得混乱:

...接收这样的字符串  0#,22008,21930,00000, n / a, n / a !
但只能使用0#,22008,21930,00000。该...

所以我现在假设那些奇怪的字符是终点线。否则进行调整。

将 arraylength 设置为 120 是完全没有必要的,并且二维数组不会添加此处可用的新方法。我们将使用锯齿状数组来代替。 310的长度可能有某种原因,所以我们保留它。

因此,在每一行中,前 24 个“单元格”是来自流的输入。我不熟悉端口,但我可以放心地假设您可以将 RS485/USB 路由到 Stream 对象。
单元格 0、4、8、12、16 和 20 填充有 ID({01、02、03、04、05、06} 之一),以指示它来自哪个 LPC。

但稍后在评论中您指定想要这个:

([32,6]-MIN([28,5]:[41,96]))/(MAX([28,5]:[41,96])-MIN([28,5]: [41,96]))

即使在 Excel 中,这也会采用这些 ID 的最小值/最大值/平均值。不过,我们在这段代码中避免了这种情况,它忽略了这些列。

class LaserArray
{
    private int minimum;
    private int maximum;
    private double average;
    private bool finishedReading;
    private StreamReader sr;

    public int Minimum
    { get { return finishedReading ? minimum : -1; } }
    public int Maximum
    { get { return finishedReading ? maximum : -1; } }
    public double Average
    { get { return finishedReading ? average : -1; } }
    public bool FinishedReading
    { get { return finishedReading; } }
    public bool StreamInitialized
    { get { return sr != null; } }

    private int[][] arr;

    public LaserArray()
    {
        arr = new int[310][];
        finishedReading = false;
    }

    public bool InitStream(Stream s)
    {
        try
        {
            sr = new StreamReader(s);
            /*alternatively, as I have no clue about your Stream:
             * sr = new StreamReader(s, bool detectEncodingFromByteOrderMarks)
             * sr = new StreamReader(s, Encoding encoding)
             * sr = new StreamReader(s, Encoding encoding, bool detectEncodingFromByteOrderMarks)
             * sr = new StreamReader(s, Encoding encoding, bool detectEncodingFromByteOrderMarks, int buffersize)
             * */
        }
        catch(Exception)
        {
            Console.WriteLine("Invalid Stream object.");
            sr = null;
            return false;
        }
        return true;
    }

    public void ReadInData()
    {
        if (sr == null)
        {
            Console.WriteLine("Initialize a Stream with UseStream first.");
            return;
        }
        if (finishedReading)
        {
            Console.WriteLine("The stream is already read.");
            return;
        }

        minimum = int.MaxValue; maximum = 0;
        int currentTotal = 0;

        for (int rowCounter = 0; rowCounter < 310; rowCounter++)
        {
            arr[rowCounter] = new int[24];
            int indexCounter = 0;
            for (int i = 0; i < 6; i++)
            {                                   // 0#,22008,21930,00000, n / a, n / a !
                char[] buffer = new char[28];   //123456789012345678901234  5  67  8 makes 28 characters?
                try
                {
                    sr.ReadBlock(buffer, 0, 2 + 5 + 1); 
                }
                catch (IOException e)
                {
                    //some error occurred
                    Console.WriteLine("IOException: " + e.Message);
                }

                string input = new String(buffer);
                arr[rowCounter][indexCounter] = int.Parse(input.Substring(2, 2));
                indexCounter++;

                int currentNumber;

                currentNumber = int.Parse(input.Substring(6, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;

                currentNumber = int.Parse(input.Substring(12, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;

                currentNumber = int.Parse(input.Substring(18, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;
            }
        }

        average = currentTotal / (double) 310;
        //succesfully read in 310 lines of data
        finishedReading = true;
    }

    public int GetMax(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int max = 0;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                if (arr[i][j] > max)
                    max = arr[i][j];
            }

        return max;
    }
    public int GetMin(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int min = 99999;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                if (arr[i][j] < min)
                    min = arr[i][j];
            }

        return min;
    }
    public double GetAverage(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int total = 0;
        int counter = 0;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                counter++;
                total += arr[i][j];
            }

        return total / (double) 310;
    }

}

这应该是不言自明的。创建一个 LaserArray,初始化流,读取数据,然后享受结果。

另外,我很无聊并且正在度假,所以这就是为什么我要如此详细地回答一个一年前的问题。

亦名誉也。

The comments you made below your question clarified it a bit, but the screwy nature of comments jumbled this line:

...receive a string like this  0#,22008,21930,00000, n / a, n / a !
But only use the 0#,22008,21930,00000. The...

So I'll just assume for now that those weird characters are an endline. Adapt it otherwise.

A set arraylength of 120 is completely unnecessary, and a 2-dimensional array adds no new methods usable here. We'll just use a jagged array instead. The length of 310 probably has some reason, so we'll keep that.

So on each row, the first 24 "cells" are the input from the stream. I'm unfamiliar with ports, but I can safely assume you can route that RS485/USB to a Stream object.
Cells 0, 4, 8, 12, 16 and 20 are filled with IDs, one of {01, 02, 03, 04, 05, 06} to indicate what LPC it came from.

But a bit later in the comments you specify wanting this:

([32,6]-MIN([28,5]:[41,96]))/(MAX([28,5]:[41,96])-MIN([28,5]:[41,96]))

Even in Excel, this would take the mini/max/average over those IDs as well. We're avoiding that in this code though, it ignores those columns.

class LaserArray
{
    private int minimum;
    private int maximum;
    private double average;
    private bool finishedReading;
    private StreamReader sr;

    public int Minimum
    { get { return finishedReading ? minimum : -1; } }
    public int Maximum
    { get { return finishedReading ? maximum : -1; } }
    public double Average
    { get { return finishedReading ? average : -1; } }
    public bool FinishedReading
    { get { return finishedReading; } }
    public bool StreamInitialized
    { get { return sr != null; } }

    private int[][] arr;

    public LaserArray()
    {
        arr = new int[310][];
        finishedReading = false;
    }

    public bool InitStream(Stream s)
    {
        try
        {
            sr = new StreamReader(s);
            /*alternatively, as I have no clue about your Stream:
             * sr = new StreamReader(s, bool detectEncodingFromByteOrderMarks)
             * sr = new StreamReader(s, Encoding encoding)
             * sr = new StreamReader(s, Encoding encoding, bool detectEncodingFromByteOrderMarks)
             * sr = new StreamReader(s, Encoding encoding, bool detectEncodingFromByteOrderMarks, int buffersize)
             * */
        }
        catch(Exception)
        {
            Console.WriteLine("Invalid Stream object.");
            sr = null;
            return false;
        }
        return true;
    }

    public void ReadInData()
    {
        if (sr == null)
        {
            Console.WriteLine("Initialize a Stream with UseStream first.");
            return;
        }
        if (finishedReading)
        {
            Console.WriteLine("The stream is already read.");
            return;
        }

        minimum = int.MaxValue; maximum = 0;
        int currentTotal = 0;

        for (int rowCounter = 0; rowCounter < 310; rowCounter++)
        {
            arr[rowCounter] = new int[24];
            int indexCounter = 0;
            for (int i = 0; i < 6; i++)
            {                                   // 0#,22008,21930,00000, n / a, n / a !
                char[] buffer = new char[28];   //123456789012345678901234  5  67  8 makes 28 characters?
                try
                {
                    sr.ReadBlock(buffer, 0, 2 + 5 + 1); 
                }
                catch (IOException e)
                {
                    //some error occurred
                    Console.WriteLine("IOException: " + e.Message);
                }

                string input = new String(buffer);
                arr[rowCounter][indexCounter] = int.Parse(input.Substring(2, 2));
                indexCounter++;

                int currentNumber;

                currentNumber = int.Parse(input.Substring(6, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;

                currentNumber = int.Parse(input.Substring(12, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;

                currentNumber = int.Parse(input.Substring(18, 5));
                arr[rowCounter][indexCounter] = currentNumber;
                currentTotal += currentNumber;
                if (currentNumber > maximum)
                    maximum = currentNumber;
                if (currentNumber < minimum)
                    maximum = currentNumber;
                indexCounter++;
            }
        }

        average = currentTotal / (double) 310;
        //succesfully read in 310 lines of data
        finishedReading = true;
    }

    public int GetMax(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int max = 0;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                if (arr[i][j] > max)
                    max = arr[i][j];
            }

        return max;
    }
    public int GetMin(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int min = 99999;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                if (arr[i][j] < min)
                    min = arr[i][j];
            }

        return min;
    }
    public double GetAverage(int topRow, int leftColumn, int bottomRow, int rightColumn)
    {
        if (!finishedReading)
        {
            Console.WriteLine("Use ReadInData first.");
            return -1;
        }

        int total = 0;
        int counter = 0;

        for (int i = topRow; i <= bottomRow; i++)
            for (int j = leftColumn; j <= rightColumn; j++)
            {
                if (j == 0 || j == 4 || j == 8 || j == 12 || j == 16 || j == 20 || j == 24)
                    continue;

                counter++;
                total += arr[i][j];
            }

        return total / (double) 310;
    }

}

This should be self-explanatory. Create a LaserArray, init the stream, readIn the data, and then have fun with the results.

Also, I'm bored and on vacation, so that's why I'm answering a year-old question in such great detail.

Also reputation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文