(C )以Specfic格式从用户中获取输入

(C++) Taking in inputs from a user in a specfic format

本文关键字:用户 获取 输入 格式 Specfic      更新时间:2023-10-16

我做了一个sudoku求解器,但是需要能够在解决之前就可以进行输入以设置拼图。输入将以1,2,3,4格式为3,4格式,其中2个逗号之间的空间表示空白(显然,这一切都会按行进行)。我还设置了代码以将空白点作为0使用,并想知道如何读取输入,将每个数字解析为int,然后将空白分析为0。

如上所述,这是针对C 的,在Visual Studio Express 2013中。

        // ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "cstdlib"
#include "stdio.h"
#include "iostream"
#include "sstream"
using namespace std;
//find the first empty slot on the puzzle
bool FindEmptyCell(int puzzle[9][9], int &row, int &colm);
//check if the inserted number is a legal move
bool isLegal(int puzzle[9][9], int row, int colm, int num);
//being solving through backtracking
bool solve(int puzzle[9][9])
{
    int row, colm; //establish rows and columns
    //check if there are any empty slots
    if (!FindEmptyCell(puzzle, row, colm))
    {
        return true; //puzzle is assumed solved
    }
    else
    {
        //start backtracking with the number 1
        for (int i = 1; i<10; i++)
        {
            if (isLegal(puzzle, row, colm, i))
            {
                puzzle[row][colm] = i;
                if (solve(puzzle) == true)
                {
                    return true; //if there is no problem with the first number, move on
                }
                else
                {
                    puzzle[row][colm] = 0;
                }
            }
        }
    }
    return false; //start recursion to try next number
}

//check if the move is legal in the 3x3 square, needs the row and column of the square
bool CheckSquare(int puzzle[9][9], int sqRow, int sqColm, int chkNum)
{
    for (int row = 0; row < 3; row++)
    {
        for (int colm = 0; colm < 3; colm++)
        {
            if (puzzle[row + sqRow][colm + sqColm] == chkNum)
            {
                return true; //the number is there and the move is illegal
            }
        }
    }
    return false; //the number is not there and the move is assumed legal
}
//check if the move is legal in the row
bool CheckRow(int puzzle[9][9], int row, int chkNum)
{
    for (int colm = 0; colm <9; colm++)
    {
        if (puzzle[row][colm] == chkNum)
        {
            return true; //the number is there and the move is illegal
        }
    }
    return false; // the number is not there and the move is assumed legal
}
//check if the move is legal in the column
bool CheckColm(int puzzle[9][9], int colm, int chkNum)
{
    for (int row = 0; row <9; row++)
    {
        if (puzzle[row][colm] == chkNum)
        {
            return true; //the number is there and the move is illegal
        }
    }
    return false; // the number is not there and the move is assumed legal
}

//definition of finding empty slot method
bool FindEmptyCell(int puzzle[9][9], int &row, int &colm)
{
    for (colm = 0; colm < 9; colm++)
    {
        for (row = 0; row < 9; row++)
        {
            if (puzzle[row][colm] == 0)
            {
                return true;
            }
        }
    }
    return false;
}
//definition of is legal method
bool isLegal(int p[9][9], int r, int c, int cN)
{
    if (CheckRow(p, r, cN) == false)
    {
        if (CheckColm(p, c, cN) == false)
        {
            if (CheckSquare(p, r - r % 3, c - c % 3, cN) == false) //use % to find the beginning row/column of the square
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
int main()
{
    int puzzle[9][9];
    string input;
    for (int i=1; i <10; i++)
    {
        for (int j=1;j <10; j++)
        {        
        cout << "Please insert the number for ["<< i << "," << j << "] (0 for no number)" << endl;
        getline(cin, input);
        int value = atoi(input.c_str());
        puzzle[i-1][j-1] = value;
        //get puzzle into correct format
        }
    }

    if (solve(puzzle) == true)
    {
        string s;
        cout << "Solving the puzzle..." << endl;
        //print the puzzle to the screen
        for (int i = 0; i<9;i++)
        {
            for (int j = 0; j<9; j++)
            {
                cout << puzzle[i][j] << ","; 
            }
            cout << endl;
        }
        cout << "Press any button to escape";
        getline(cin, s);
    }
    else
    {
        cout << "Can not solve the puzzle" << endl;
    }
    return 0;
}

不确定您为什么需要代码,但是在这里。一切正常,目前没有正确的格式输入。

,因为您的数字仅为1位,您可以:

int puzzle[9][9] = {};
for (int i = 1; i < 10; ++i)
{
   cout << "Please input row #" << i << ": ";
   getline(cin, input);
   int j = 0;
   const char *ptr = input.c_str();
   while (*ptr)
   {
     if (isspace(*ptr)) ; // do nothing
     else if (*ptr == ',') ++j; // new column
     else puzzle[i - 1][j] = *ptr - '0';
     ++ptr;
   }
}

以将空的空间解析为Zerors,您需要:

  • 将矩阵初始化为零int puzzle[9][9] = { };

  • 按行读取输入(每行代表整个行)

  • 使用函数strtok()使用分配者`',','

  • 对于每个分离的值,将其修剪(即删除空格)。然后,使用函数 atoi将其掩盖为整数

请注意,您应该检查空的分离字符串if (strlen(str))