在表

问题描述 投票:0回答:1

enter image description here

以下示例场景:

我有列

A
Rowindex:4
Columnindex:7

我想将列

A

11向左移动,以便我所产生的列索引(
B
)为
rowindex:2
ColumnIndex:6

更糟糕的是,该表是一个子桌子,因此不是在索引0、0开始,而是4、2,如本示例所示。 我很确定这可以通过Modulo计算来解决,但我的大脑拒绝给我正确的公式。 到目前为止,我从C#开始使用此编码,但这似乎不对(

https://dotnetfiddle.net/5ieqgd

):

public static void Main() { int minColumnIndex = 4; int maxColumnIndex = 8; int minRowIndex = 2; int maxRowIndex = 5; int cellARowIndex = 4; int cellAColumnIndex = 7; int shiftCellToTheLeft = 11; int cellBColumnIndex = cellAColumnIndex - ((maxColumnIndex - minColumnIndex + 1) % shiftCellToTheLeft); int cellBRowIndex = cellARowIndex - ((maxColumnIndex - maxColumnIndex + 1) % shiftCellToTheLeft); Console.WriteLine("cellBColumnIndex: " + cellBColumnIndex); Console.WriteLine("cellBRowIndex: " + cellBRowIndex); // Result: // cellBColumnIndex: 2 // cellBRowIndex: 3 }

我在这里做错了什么?

如果我们将问题分为3部分,这将变得更容易。

Conconvert行 /列索引到表索引。

从表索引提取11。 Contervert表索引到行 /列索引。
c# logic
1个回答
0
投票
可调用的代码:

static class Table { private const int FirstRowIndex = 2; private const int FirstColumnIndex = 4; private const int ColumnCount = 5; public static int CellToTable(int row, int colum) { return (row - FirstRowIndex) * ColumnCount + colum - FirstColumnIndex; } public static (int row, int column) TableToCell(int index) { return ( row: index / ColumnCount + FirstRowIndex, column: index % ColumnCount + FirstColumnIndex ); } }
  1. i使课程静态,但是如果您需要几个实例,则可以将其转换。
  2. 测试:
  3. int tableIndex = Table.CellToTable(4, 7); Console.WriteLine("Table index = " +tableIndex); var (row, column) = Table.TableToCell(tableIndex); Console.WriteLine($"Row = {row}, Column = {column}"); Console.WriteLine(); Console.WriteLine("Subtract 11 from table index"); tableIndex -= 11; Console.WriteLine("Table index = " + tableIndex); (row, column) = Table.TableToCell(tableIndex); Console.WriteLine($"Row = {row}, Column = {column}"); Console.ReadKey();
  4. Prints:

Table index = 13 Row = 4, Column = 7 Subtract 11 from table index Table index = 2 Row = 2, Column = 6

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.