c#中具有数组二维的方程
本文关键字:二维 方程 数组 | 更新日期: 2023-09-27 18:11:29
我有一个310行120列的数组。
我每隔10秒从串行端口获得填充这个数组的数据。
如何将此数据发送到数组的第一行,并在下一次迭代到下一行,并持续直到操作完成?
一旦完成,取数组的最大值、最小值和平均值。最后是数组中选定单元格的平均值、最大值和最小值。
这是可能的数组在c# ?
([7,31]-xxxxx.MIN([$28$5:$45$95])/(xxxxx.MAX[$28$5:$46$95]-xxxxx.MIN[$28$5:$45$95])
您在问题下面所做的注释稍微澄清了一点,但是注释的奇怪性质使这一行变得混乱:
…接收这样的字符串0#,22008,21930,00000,n/a, n/a !但只使用0#,22008,21930,00000。…
所以我现在就假设这些奇怪的字符是一个结尾。
set 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]:[96]))/(MAX([28日5]:[96])分钟([28日5]:[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,初始化流,读取数据,然后享受结果。
还有,我很无聊,又在度假,所以我才这么详细地回答一个一年前的问题。
也声誉。