0

我在 C++ 中创建了一个数独求解器。但是我需要一个 GUI。由于我不熟悉 VC++,我无法用它创建 GUI,而是用 C# 创建它。我已经了解了 c# 的基础知识,但需要一个先机。如果我创建一个 Windows 窗体应用程序并在窗体中创建一个数据网格视图,我应该如何在网格中实现该功能。下面是我的 C++ 代码。

#include<iostream.h>
#include<conio.h>




int a[9][9],b[9][9];

int inputvalue(int x, int y, int value)
{
    for(int i = 0; i < 9; i++)
    {
        if(value == b[x][i] || value == b[i][y])
            return 0;
    }

    for (i = (x / 3) * 3; i <= ((x / 3) * 3) + 2; i++)
        for (int j = (y / 3) * 3; j <= ((y / 3) * 3) + 2; j++)
            if(b[i][j] == value)
                return 0;
    return value;
}

int solve(int x, int y)
{
    int temp;
    if(b[x][y] == 0)
    {
        for(int i = 1;i < 10; i++)
        {
        temp = inputvalue(x, y, i);
        if(temp > 0)
        {
            b[x][y] = temp;
            if (x == 8 && y == 8)
                return 1;
                else if (x == 8)
                {
                if (solve(0, y + 1))
                return 1;
            }
            else
            {
            if (solve(x + 1, y))
                    return 1;
            }
        }
         }
         if (i == 10)
         {
             if (b[x][y] != a[x][y])
             b[x][y] = 0;
             return 0;
         }
    }
    if (x == 8 && y == 8)
        return 1;
    else if (x == 8)
    {
        if (solve(0, y + 1))
            return 1;
    }
    else
    {
        if (solve(x + 1, y))
            return 1;
    }
}



void main()
{
    clrscr();
    for(int i = 0;i < 9;i++)
        for(int j = 0;j < 9;j++)
        {
            gotoxy(i + 1,j + 1);
            cin >> a[i][j];
        }
    for(i = 0;i < 9;i++)
        for(j = 0;j < 9;j++)
            b[i][j] = a[i][j];
    if(solve(0,0))
    {
        for(i = 0;i < 9;i++)
            for(j = 0;j < 9;j++)
            {
            gotoxy(i + 1,j + 1);
            cout << b[i][j];
            }
    }
    else
        cout<<"no solution";
    getch();
}
4

1 回答 1

1

好吧,我猜你会有一个按钮或者说“开始解决”表格的东西,所以你需要注册按钮点击:

this.button1.Click += new System.EventHandler(this.button1_Click);

以及button1_Click您需要执行逻辑的方法,正如您在代码中显示的那样。您可能想知道如何寻址datagridview. 这很容易:

dataGridView1[CurrentColumn, CurrentRow]

返回一个DataGridViewCell你将转换为你的单元格的(我猜DataGridViewTextBoxColumn)像这样

(DataGridViewTextBoxColumn)dataGridView1[CurrentColumn, CurrentRow]

或者

dataGridView1[CurrentColumn, CurrentRow] as DataGridViewTextBoxColumn

然后使用编辑该单元格Text

(dataGridView1[CurrentColumn, CurrentRow] as DataGridViewTextBoxColumn).Text = MyText
于 2013-07-30T07:26:07.800 回答