【发布时间】:2019-09-22 15:02:57
【问题描述】:
我正在尝试从 CSV 文件生成数独网格。 我为这种文件编写的代码:
1,2,3,4,5,6,7,8,9
1,2,3,4,5,6,7,8,9
...
但我需要它来用这种文件创建网格:
; ; 5; ;;1; ;;1
3; ; ;;;;6; ;
;7;;;;2;;;4
;;1;3;;5;;8;
...
这是我的代码:
int grid[9][9];
ifstream inputFile;
inputFile.open(inputFileName);
for (int row = 0; row <9 ; row++) {
string line;
getline(inputFile, line);
if (!inputFile.good())
break;
stringstream ss(line);
for (int col = 0; col < 9; col++) {
size_t si = line.find_first_not_of("\n 123456789");
string ch = line.substr(0, si);
if (ch.length() == 0) break;
istringstream converter(ch);
converter >> grid[row][col];
if ((si + 1u) >= line.length() || si == std::string::npos) break;
line = line.substr(si + 1u, std::string::npos);
}
}
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
if (grid[i][j] == 0) {
cout << " ";
}
else {
cout << grid[i][j] << " ";
}
if ((j + 1) % 3 == 0) {
cout << "|";
}
if (j == 8)
cout << " " << (i + 1);
}
cout << endl;
if ((i + 1) % 3 == 0) {
for (int s = 0; s <= 20; s++)
cout << "-";
cout << endl;
}
}
cout << endl;
return 0;
}
你有什么想法用几个连续的分隔符来做吗?
非常感谢
【问题讨论】: