C# C语言中的二维数组方程组#

C# C语言中的二维数组方程组#,c#,multidimensional-array,C#,Multidimensional Array,我有一个310行120列的数组 我每隔10秒从串行端口获取将填充此阵列的数据 如何将此数据发送到数组的第一行,并在下一次迭代时将其发送到下一行,并持续发送,直到操作完成 完成后,获取阵列的最大值、最小值和平均值。最后是数组中选定单元格的平均值、最大值和最小值 这在C#中的数组中是可能的 你在问题下方的评论稍微澄清了一点,但评论的扭曲性质混淆了这一行: …接收这样的字符串 0#,220082193000000,不适用,不适用! 但只使用0#,220082193000000。这个 所以我现在就假设

我有一个310行120列的数组

我每隔10秒从串行端口获取将填充此阵列的数据

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

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

这在C#中的数组中是可能的


你在问题下方的评论稍微澄清了一点,但评论的扭曲性质混淆了这一行:

…接收这样的字符串 0#,220082193000000,不适用,不适用! 但只使用0#,220082193000000。这个

所以我现在就假设这些奇怪的角色是一条终点线。以其他方式进行调整

设置arraylength为120是完全不必要的,并且二维数组没有添加任何新方法。我们将只使用锯齿状数组。310的长度可能有一些原因,所以我们将保留它

因此,在每一行上,前24个“单元”是来自流的输入。我不熟悉端口,但我可以安全地假设您可以将RS485/USB路由到流对象。 单元格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中,这也会将mini/max/average置于这些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;
    }

}
类激光阵列
{
私有整数最小值;
私有整数最大值;
私人双平均;
私人布尔完成恐惧;
私人流动阅读器sr;
公共整数最小值
{get{返回FinisheFearding?最小值:-1;}
公共整数最大值
{get{返回FinisheFearding?最大值:-1;}
公众双倍平均
{get{返回FinisheFearding?平均值:-1;}
公共布尔完成恐惧
{get{return finishedraveling;}}
公共布尔流
{get{return sr!=null;}}
私有int[]arr;
公共激光阵列()
{
arr=新整数[310][];
完成恐惧=错误;
}
公共布尔初始化流(流s)
{
尝试
{
sr=新的流读卡器;
/*或者,由于我对您的流一无所知:
*sr=新的StreamReader(从字节顺序标记进行布尔检测编码)
*sr=新的StreamReader(s,编码)
*sr=新的StreamReader(s,编码,bool detectEncodingFromByteOrderMarks)
*sr=新的StreamReader(s,编码,bool detectEncodingFromByteOrderMarks,int buffersize)
* */
}
捕获(例外)
{
WriteLine(“无效流对象”);
sr=null;
返回false;
}
返回true;
}
public void ReadInData()
{
if(sr==null)
{
WriteLine(“首先使用UseStream初始化流”);
返回;
}
如果(完成恐惧)
{
WriteLine(“流已被读取”);
返回;
}
最小值=int.MaxValue;最大值=0;
int currentTotal=0;
对于(int rowCounter=0;rowCounter<310;rowCounter++)
{
arr[rowCounter]=新整数[24];
int indexCounter=0;
对于(int i=0;i<6;i++)
{                                   // 0#,220082193000000,不适用,不适用!
char[]buffer=new char[28];//1234567890012345678901234 5 67 8生成28个字符?
尝试
{
sr.ReadBlock(缓冲区,0,2+5+1);
}
捕获(IOE异常)
{
//发生了一些错误
Console.WriteLine(“IOException:+e.Message”);
}
字符串输入=新字符串(缓冲区);
arr[rowCounter][indexCounter]=int.Parse(input.Substring(2,2));
indexCounter++;
int-currentNumber;
currentNumber=int.Parse(input.Substring(6,5));
arr[rowCounter][indexCounter]=当前编号;
currentTotal+=currentNumber;
如果(当前编号>最大值)
最大值=当前数量;
if(当前编号<最小值)
最大值=当前数量;
indexCounter++;
currentNumber=int.Parse(input.Substring(12,5));
arr[rowCounter][indexCounter]=当前编号;
currentTotal+=currentNumber;
如果(当前编号>最大值)
最大值=当前数量;
if(当前编号<最小值)
最大值=当前数量;
indexCounter++;
currentNumber=int.Parse(input.Substring(18,5));
arr[rowCounter][indexCounter]=当前编号;
currentTotal+=currentNumber;
如果(当前编号>最大值)
最大值=当前数量;
if(当前编号<最小值)
最大值=当前数量;
indexCounter++;
}
}
平均值=当前总计/(双)310;
//成功读入310行数据
完成恐惧=真实;
}
public int GetMax(int topRow、int leftColumn、int bottomRow、int rightColumn)
{
如果(!完成恐惧)
{
Console.WriteLine(“首先使用ReadInData”);
返回-1;
}
int max=0;

对于(inti=topRow;i您在qu下面的评论
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;
    }

}