一个问题是放置不当break;
,在:
}
printf("\n%d|%d\n", X,Y);
break;
}
//End of file opening correctly process
这会在一次迭代后终止外循环。然而,去除它并不是万能的。
另一个问题是空else { }
子句。如果你把它放进去,它会被执行很多printf("Ooops\n");
。
您似乎没有采取足够的措施来确保您的数组下标保持正数。您似乎还在扫描代码中编辑 X 和 Y,而没有再次将它们设置回去,这样您就弄乱了外部搜索循环。您有两个未使用的变量 M 和 N,也许您正在考虑使用它们。但是你必须停止以某种方式践踏 X 和 Y。
另一个问题是您没有检查您是否从用户那里获得了输入(if (fgets(string, sizeof(string), stdin) == 0) return 0;
检测 EOF 并退出)。另一个问题是您没有从输入字符串的末尾删除换行符。
我修改了代码来处理这些并回显正在搜索的单词。我输入hoover
并输出包括:
Please enter the word to be searched:
Searching for: [HOOVER]
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
Oops!
18|0
Oops!
Letter 1 of HOOVERis found at 1,1
Letter 2 of HOOVERis found at 2,0
Word found in Y+X+ direction
6|-4
Oops!
Oops!
Oops!
然后它变得非常无聊,不断重复自己(这是在我删除break;
顶部提到的之后。
你真的应该写一些函数,而不是把所有的代码都放在一个main()
像这样的整体中。你可以有一个search_from_here()
给定字符串、数组和一个 X、Y 坐标的起始点;它可能在找不到单词时返回 0,在找到时返回 1。
您将无法使用此代码,但它似乎可以工作。它确实使用了基本的 C99 功能,例如在需要时和在for
循环中声明变量。
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static int prompt_for(char *buffer, size_t buflen);
static int search_direction(char array[18][18], char *string, int length, int X, int Y, int dx, int dy);
static int search_from(char array[18][18], char *string, int length, int X, int Y);
int main(void)
{
int X, Y, length;
char array[18][18], string[50];
printf("WORDSEARCH SOLVER - PRESS CTRL-C TO QUIT\n\n");
FILE *wsch = fopen("wrdsearch.txt", "r");
if (!wsch)
{
printf("Error! File did not open.\n");
return 1;
}
for (Y = 0; Y < 18; Y++)
{
for (X = 0; X < 18; X++)
{
fscanf(wsch, " %c", &array[Y][X]);
array[Y][X] = toupper(array[Y][X]);
}
}
fclose(wsch);
printf(" ");
for (X = 0; X < 18; X++)
printf("%-2d", X);
printf("\n______________________________________\n");
for (Y = 0; Y < 18; Y++)
{
printf("%-2d|", Y);
for (X = 0; X < 18; X++)
printf("%c ", array[Y][X]);
printf("\n");
}
while ((length = prompt_for(string, sizeof(string))) != EOF)
{
printf("Searching for: [%s]\n", string);
int count = 0;
for (Y = 0; Y < 18; Y++)
{
for (X = 0; X < 18; X++)
{
if (array[Y][X] == (string[0]) && search_from(array, string, length, X, Y))
count++;
}
}
printf("Found %s %d times\n", string, count);
}
printf("\n");
return 0;
}
static int prompt_for(char *buffer, size_t buflen)
{
printf("\nPlease enter the word to be searched: ");
if (fgets(buffer, buflen, stdin) == 0)
return EOF;
size_t length = strlen(buffer);
if (buffer[length-1] == '\n')
buffer[--length] = '\0';
if (length == 0)
return EOF;
for (size_t i = 0; i < length; i++)
buffer[i] = toupper(buffer[i]);
return length;
}
static int search_from(char array[18][18], char *string, int length, int X, int Y)
{
struct yx { int dy; int dx; } directions[] =
{
{ +1, 0 }, { -1, 0 }, { +1, +1 }, { -1, +1 },
{ 0, +1 }, { 0, -1 }, { -1, -1 }, { +1, -1 },
};
enum { num_directions = sizeof(directions) / sizeof(directions[0]) };
int count = 0;
for (int i = 0; i < num_directions; i++)
{
if (search_direction(array, string, length, X, Y, directions[i].dx, directions[i].dy))
count++;
}
return count;
}
static int search_direction(char array[18][18], char *string, int length, int X, int Y, int dx, int dy)
{
for (int i = 1; i < length; i++)
{
int x = X + i * dx;
int y = Y + i * dy;
if (x < 0 || x >= 18 || y < 0 || y >= 18)
return 0;
if (array[y][x] != string[i])
return 0;
}
printf("Found word %s starting at (%d,%d) to (%d,%d)\n",
string, Y, X, Y + (length - 1) * dy, X + (length - 1) * dx);
/* Validating search! */
char *pad = "";
for (int i = 0; i < length; i++)
{
int x = X + i * dx;
int y = Y + i * dy;
printf("%s%c (%d,%d)", pad, array[y][x], y, x);
pad = ", ";
}
putchar('\n');
return 1;
}
给定一份(完整的)美国总统姓氏列表:
adams
arthur
buchanan
bush
carter
cleveland
clinton
coolidge
eisenhower
fillmore
ford
garfield
grant
harding
harrison
hayes
hoover
jackson
jefferson
johnson
kennedy
lincoln
madison
mckinley
monroe
nixon
obama
pierce
polk
reagan
roosevelt
taft
taylor
truman
tyler
vanburen
washington
wilson
当运行 asws < presidents
时,在回显之前不会在屏幕上看到名称,它会至少找到每个名称一次,因此:
WORDSEARCH SOLVER - PRESS CTRL-C TO QUIT
0 1 2 3 4 5 6 7 8 9 1011121314151617
______________________________________
0 |M N O S L I W E R E L Y T L E A G N
1 |A H O O V E R T A Y L O R V E N N A
2 |D F D R O O S E V E L T O N O M I M
3 |I N T P M H I E G D I L O O C O D U
4 |S O N L I J Q A D A M S S R N N R R
5 |O X L O G E F F M O I R E E G R A T
6 |N I B T S O R A O R M O V A W O H N
7 |F N H U R R B C R N L R E G B E W E
8 |R O S D C O E A E I O N L A U A J R
9 |K W U N L H H F N I O S T N S W A U
10|E R B A J B A C F S S M N H H R D B
11|N E W L O T O N K E C E I H T H A N
12|N T H E H L A C A K J N N H O I M A
13|E R G V N C A F I N G H U H A J S V
14|D A R E S J C N T T A R N B O Y A E
15|Y C A L O D L N O S I R R A H W E O
16|N E N C N E T N N O T N I L C O E S
17|D A T Y Y P O L K G A R F I E L D R
Please enter the word to be searched: Searching for: [ADAMS]
Found word ADAMS starting at (4,7) to (4,11)
A (4,7), D (4,8), A (4,9), M (4,10), S (4,11)
Found word ADAMS starting at (9,16) to (13,16)
A (9,16), D (10,16), A (11,16), M (12,16), S (13,16)
Found ADAMS 2 times
Please enter the word to be searched: Searching for: [ARTHUR]
Found word ARTHUR starting at (9,16) to (14,11)
A (9,16), R (10,15), T (11,14), H (12,13), U (13,12), R (14,11)
Found ARTHUR 1 times
Please enter the word to be searched: Searching for: [BUCHANAN]
Found word BUCHANAN starting at (6,2) to (13,9)
B (6,2), U (7,3), C (8,4), H (9,5), A (10,6), N (11,7), A (12,8), N (13,9)
Found BUCHANAN 1 times
Please enter the word to be searched: Searching for: [BUSH]
Found word BUSH starting at (7,14) to (10,14)
B (7,14), U (8,14), S (9,14), H (10,14)
Found word BUSH starting at (10,2) to (7,2)
B (10,2), U (9,2), S (8,2), H (7,2)
Found BUSH 2 times
Please enter the word to be searched: Searching for: [CARTER]
Found word CARTER starting at (15,1) to (10,1)
C (15,1), A (14,1), R (13,1), T (12,1), E (11,1), R (10,1)
Found CARTER 1 times
Please enter the word to be searched: Searching for: [CLEVELAND]
Found word CLEVELAND starting at (16,3) to (8,3)
C (16,3), L (15,3), E (14,3), V (13,3), E (12,3), L (11,3), A (10,3), N (9,3), D (8,3)
Found CLEVELAND 1 times
Please enter the word to be searched: Searching for: [CLINTON]
Found word CLINTON starting at (16,14) to (16,8)
C (16,14), L (16,13), I (16,12), N (16,11), T (16,10), O (16,9), N (16,8)
Found CLINTON 1 times
Please enter the word to be searched: Searching for: [COOLIDGE]
Found word COOLIDGE starting at (3,14) to (3,7)
C (3,14), O (3,13), O (3,12), L (3,11), I (3,10), D (3,9), G (3,8), E (3,7)
Found COOLIDGE 1 times
Please enter the word to be searched: Searching for: [EISENHOWER]
Found word EISENHOWER starting at (8,8) to (17,17)
E (8,8), I (9,9), S (10,10), E (11,11), N (12,12), H (13,13), O (14,14), W (15,15), E (16,16), R (17,17)
Found EISENHOWER 1 times
Please enter the word to be searched: Searching for: [FILLMORE]
Found word FILLMORE starting at (7,0) to (0,7)
F (7,0), I (6,1), L (5,2), L (4,3), M (3,4), O (2,5), R (1,6), E (0,7)
Found FILLMORE 1 times
Please enter the word to be searched: Searching for: [FORD]
Found word FORD starting at (5,6) to (8,3)
F (5,6), O (6,5), R (7,4), D (8,3)
Found FORD 1 times
Please enter the word to be searched: Searching for: [GARFIELD]
Found word GARFIELD starting at (17,9) to (17,16)
G (17,9), A (17,10), R (17,11), F (17,12), I (17,13), E (17,14), L (17,15), D (17,16)
Found GARFIELD 1 times
Please enter the word to be searched: Searching for: [GRANT]
Found word GRANT starting at (13,2) to (17,2)
G (13,2), R (14,2), A (15,2), N (16,2), T (17,2)
Found GRANT 1 times
Please enter the word to be searched: Searching for: [HARDING]
Found word HARDING starting at (6,16) to (0,16)
H (6,16), A (5,16), R (4,16), D (3,16), I (2,16), N (1,16), G (0,16)
Found HARDING 1 times
Please enter the word to be searched: Searching for: [HARRISON]
Found word HARRISON starting at (9,6) to (2,13)
H (9,6), A (8,7), R (7,8), R (6,9), I (5,10), S (4,11), O (3,12), N (2,13)
Found word HARRISON starting at (15,14) to (15,7)
H (15,14), A (15,13), R (15,12), R (15,11), I (15,10), S (15,9), O (15,8), N (15,7)
Found HARRISON 2 times
Please enter the word to be searched: Searching for: [HAYES]
Found word HAYES starting at (12,13) to (16,17)
H (12,13), A (13,14), Y (14,15), E (15,16), S (16,17)
Found HAYES 1 times
Please enter the word to be searched: Searching for: [HOOVER]
Found word HOOVER starting at (1,1) to (1,6)
H (1,1), O (1,2), O (1,3), V (1,4), E (1,5), R (1,6)
Found HOOVER 1 times
Please enter the word to be searched: Searching for: [JACKSON]
Found word JACKSON starting at (14,5) to (8,11)
J (14,5), A (13,6), C (12,7), K (11,8), S (10,9), O (9,10), N (8,11)
Found JACKSON 1 times
Please enter the word to be searched: Searching for: [JEFFERSON]
Found word JEFFERSON starting at (12,10) to (4,2)
J (12,10), E (11,9), F (10,8), F (9,7), E (8,6), R (7,5), S (6,4), O (5,3), N (4,2)
Found JEFFERSON 1 times
Please enter the word to be searched: Searching for: [JOHNSON]
Found word JOHNSON starting at (10,4) to (16,4)
J (10,4), O (11,4), H (12,4), N (13,4), S (14,4), O (15,4), N (16,4)
Found word JOHNSON starting at (13,15) to (7,9)
J (13,15), O (12,14), H (11,13), N (10,12), S (9,11), O (8,10), N (7,9)
Found JOHNSON 2 times
Please enter the word to be searched: Searching for: [KENNEDY]
Found word KENNEDY starting at (9,0) to (15,0)
K (9,0), E (10,0), N (11,0), N (12,0), E (13,0), D (14,0), Y (15,0)
Found KENNEDY 1 times
Please enter the word to be searched: Searching for: [LINCOLN]
Found word LINCOLN starting at (7,10) to (13,4)
L (7,10), I (8,9), N (9,8), C (10,7), O (11,6), L (12,5), N (13,4)
Found LINCOLN 1 times
Please enter the word to be searched: Searching for: [MADISON]
Found word MADISON starting at (0,0) to (6,0)
M (0,0), A (1,0), D (2,0), I (3,0), S (4,0), O (5,0), N (6,0)
Found MADISON 1 times
Please enter the word to be searched: Searching for: [MCKINLEY]
Found word MCKINLEY starting at (10,11) to (17,4)
M (10,11), C (11,10), K (12,9), I (13,8), N (14,7), L (15,6), E (16,5), Y (17,4)
Found MCKINLEY 1 times
Please enter the word to be searched: Searching for: [MONROE]
Found word MONROE starting at (2,15) to (7,15)
M (2,15), O (3,15), N (4,15), R (5,15), O (6,15), E (7,15)
Found MONROE 1 times
Please enter the word to be searched: Searching for: [NIXON]
Found word NIXON starting at (7,1) to (3,1)
N (7,1), I (6,1), X (5,1), O (4,1), N (3,1)
Found NIXON 1 times
Please enter the word to be searched: Searching for: [OBAMA]
Found word OBAMA starting at (8,5) to (4,9)
O (8,5), B (7,6), A (6,7), M (5,8), A (4,9)
Found OBAMA 1 times
Please enter the word to be searched: Searching for: [PIERCE]
Found word PIERCE starting at (3,3) to (8,8)
P (3,3), I (4,4), E (5,5), R (6,6), C (7,7), E (8,8)
Found PIERCE 1 times
Please enter the word to be searched: Searching for: [POLK]
Found word POLK starting at (17,5) to (17,8)
P (17,5), O (17,6), L (17,7), K (17,8)
Found POLK 1 times
Please enter the word to be searched: Searching for: [REAGAN]
Found word REAGAN starting at (4,13) to (9,13)
R (4,13), E (5,13), A (6,13), G (7,13), A (8,13), N (9,13)
Found REAGAN 1 times
Please enter the word to be searched: Searching for: [ROOSEVELT]
Found word ROOSEVELT starting at (1,12) to (9,12)
R (1,12), O (2,12), O (3,12), S (4,12), E (5,12), V (6,12), E (7,12), L (8,12), T (9,12)
Found word ROOSEVELT starting at (2,3) to (2,11)
R (2,3), O (2,4), O (2,5), S (2,6), E (2,7), V (2,8), E (2,9), L (2,10), T (2,11)
Found ROOSEVELT 2 times
Please enter the word to be searched: Searching for: [TAFT]
Found word TAFT starting at (11,5) to (14,8)
T (11,5), A (12,6), F (13,7), T (14,8)
Found TAFT 1 times
Please enter the word to be searched: Searching for: [TAYLOR]
Found word TAYLOR starting at (1,7) to (1,12)
T (1,7), A (1,8), Y (1,9), L (1,10), O (1,11), R (1,12)
Found TAYLOR 1 times
Please enter the word to be searched: Searching for: [TRUMAN]
Found word TRUMAN starting at (5,17) to (0,17)
T (5,17), R (4,17), U (3,17), M (2,17), A (1,17), N (0,17)
Found TRUMAN 1 times
Please enter the word to be searched: Searching for: [TYLER]
Found word TYLER starting at (0,12) to (0,8)
T (0,12), Y (0,11), L (0,10), E (0,9), R (0,8)
Found TYLER 1 times
Please enter the word to be searched: Searching for: [VANBUREN]
Found word VANBUREN starting at (13,17) to (6,17)
V (13,17), A (12,17), N (11,17), B (10,17), U (9,17), R (8,17), E (7,17), N (6,17)
Found VANBUREN 1 times
Please enter the word to be searched: Searching for: [WASHINGTON]
Found word WASHINGTON starting at (7,16) to (16,7)
W (7,16), A (8,15), S (9,14), H (10,13), I (11,12), N (12,11), G (13,10), T (14,9), O (15,8), N (16,7)
Found WASHINGTON 1 times
Please enter the word to be searched: Searching for: [WILSON]
Found word WILSON starting at (0,6) to (0,1)
W (0,6), I (0,5), L (0,4), S (0,3), O (0,2), N (0,1)
Found WILSON 1 times
Please enter the word to be searched: