以下示例场景:
我有列
A
Rowindex:4A
11向左移动,以便我所产生的列索引(
B
)为rowindex:2
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表索引到行 /列索引。
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
);
}
}
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();
Table index = 13
Row = 4, Column = 7
Subtract 11 from table index
Table index = 2
Row = 2, Column = 6