(C )以Specfic格式从用户中获取输入
(C++) Taking in inputs from a user in a specfic format
我做了一个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))
相关文章:
- 如何从用户那里获取输入并将其存储在向量中?
- 从用户那里获取数组大小的值
- 如何获取用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?
- 用于获取枚举值的用户输入
- 如何获取当前用户的默认RAS电话簿路径?
- C++如何使用表达式获取用户输入?
- 代码在执行之前终止,并且没有从用户那里获取输入
- 从用户获取输入/输出
- 从用户获取输入
- 如何从用户获取数组输入
- 我在C++从函数中的用户获取输入、添加到数组和打印该数组时遇到问题
- 如何在从用户获取输入后在函数中使用常量值
- 如何从非管理员用户获取当前登录的用户名、域名和用户 sid
- 通过用户获取 cin 来命名输出文件
- 如何在Windows中为当前用户获取库文件夹列表
- 字符串类型从用户获取输入'space'
- 从 twitcurl 时间线获取未授权错误用户获取方法
- 我如何让我的程序从用户获取5个浮点值并按递增顺序对它们进行排序?
- 使用Switch c++从用户获取输入
- 如何使用C++为特定用户获取文件夹的ACL权限