此代码如果假设首先获取一行由空格分隔的字符组合输入,然后不超过 10 行输入,全部长度相等但不超过 10 个字符,然后从二维数组中的第一行。这是代码。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define WORDNUM 100
#define SIZE 10
int getWords(char wordMatrix[WORDNUM][100]){
char c;
int i=0, j=0;
while ((c=fgetc(stdin)) != '\n'){
if (isalpha(c)){
wordMatrix[i][j]=c;
j++;
}
else if(c==' '){
i++;
j=0;
}
}
return i+1;
}
int getMatrix( char matrix[SIZE][SIZE]){
char c;
int i=0, j;
while ((c=fgetc(stdin)) != EOF && i < (SIZE)){
if (isalpha(c)){
matrix[i][j]=c;
j++;
}
else if(c=='\n'){
i++;
j=0;
}
}
return i;
}
int checkExist(int rowM , int xM, int rowNum, int rowLen, char *word, char matrix[SIZE][SIZE]){
int xW=1;
int direction;
direction = -1;
while((rowM + direction) > 0){
if(matrix[xM][rowM+direction] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction--;
}
else break;
}
direction = -1;
while((xM + direction) > 0){
if(matrix[xM+direction][rowM] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction--;
}
else break;
}
direction = 1;
while((rowM + direction) < rowNum){
if(matrix[xM][rowM+direction] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction++;
}
else break;
}
direction = 1;
while((xM + direction) < rowLen ){
if(matrix[xM+direction][rowM] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction++;
}
else break;
}
return 0;
}
int checkMatrix(int rowNum, int rowLen, char *word, char matrix[SIZE][SIZE]){
int xM, rowM, check;
for(rowM=0; rowM < rowNum; rowM++){
for(xM=0; xM < rowLen; xM++){
if(word[0] == matrix[rowM][xM]){
if(strlen(word) == 1) return 1;
else if (strlen(word)>1 && (check = checkExist(rowM, xM, rowNum, rowLen, word, matrix)) == 1) return 1;
}
}
}
return 0;
}
int main() {
char word[WORDNUM][100], matrix[SIZE][SIZE];
int logRowNumW, logRowNumM, logRowLen, rowW, x, check;
logRowNumW = getWords(word);
logRowNumM = getMatrix(matrix);
logRowLen = strlen(matrix[0]);
for(rowW=0; rowW <logRowNumW; rowW++){
if((check = checkMatrix(logRowNumM, logRowLen, word[rowW], matrix)) == 1){
for(x=0; x<strlen(word[rowW]); x++){
printf("%c", word[rowW][x]);
}
}
}
return 0;
}
问题是,它不输出任何内容,无论正确或不正确,什么也没有输出。感谢您的任何建议
getWords()
、getMatrix()
:fgetc()
返回int
,当您检查EOF
中的getMatrix()
时,这一点特别重要。
您不能使用 ' 终止
wordMatrix
或 matrix
,因此任何依赖于这些字符串 (strlen()
) 的内容都将触发未定义的行为。
getWords()
可能应该跳过前导空格。
getMatrix()
您需要初始化j
。
(不固定)为第二个 100 魔法值引入 MAX_WORD_LEN 或类似值,并在迭代矩阵时使用该上限以避免缓冲区溢出。
通过这 5 项更改中的 4 项:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define WORDNUM 100
#define SIZE 10
int getWords(char wordMatrix[WORDNUM][100]){
int c;
int i=0, j=0;
while ((c=fgetc(stdin)) != '\n') {
if (isalpha(c)){
wordMatrix[i][j]=c;
j++;
} if(c==' ') {
if(!j) continue;
wordMatrix[i][j] = '\0';
i++;
j=0;
}
}
return i+1;
}
int getMatrix( char matrix[SIZE][SIZE]){
int c;
int i=0, j = 0;
while ((c=fgetc(stdin)) != EOF && i < (SIZE)){
if (isalpha(c)){
matrix[i][j]=c;
j++;
}
else if(c=='\n'){
matrix[i][j] = '\0';
i++;
j=0;
}
}
return i;
}
int checkExist(int rowM , int xM, int rowNum, int rowLen, char *word, char matrix[SIZE][SIZE]){
int xW=1;
int direction;
direction = -1;
while((rowM + direction) > 0){
if(matrix[xM][rowM+direction] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction--;
}
else break;
}
direction = -1;
while((xM + direction) > 0){
if(matrix[xM+direction][rowM] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction--;
}
else break;
}
direction = 1;
while((rowM + direction) < rowNum){
if(matrix[xM][rowM+direction] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction++;
}
else break;
}
direction = 1;
while((xM + direction) < rowLen ){
if(matrix[xM+direction][rowM] == word[xW]){
xW++;
if(xW == strlen(word)) return 1;
else direction++;
}
else break;
}
return 0;
}
int checkMatrix(int rowNum, int rowLen, char *word, char matrix[SIZE][SIZE]){
for(int rowM=0; rowM < rowNum; rowM++){
for(int xM=0; xM < rowLen; xM++){
if(word[0] == matrix[rowM][xM]){
if(strlen(word) == 1) return 1;
else if (strlen(word)>1 && (checkExist(rowM, xM, rowNum, rowLen, word, matrix)) == 1) return 1;
}
}
}
return 0;
}
int main() {
char word[WORDNUM][100], matrix[SIZE][SIZE];
int logRowNumW = getWords(word);
int logRowNumM = getMatrix(matrix);
int logRowLen = strlen(matrix[0]);
for(int rowW=0; rowW <logRowNumW; rowW++){
if((checkMatrix(logRowNumM, logRowLen, word[rowW], matrix)) == 1){
for(int x=0; x<strlen(word[rowW]); x++){
printf("%c", word[rowW][x]);
}
}
}
return 0;
}
您的程序现在生成输出:
a
ab
cd
<EOF>
a