基本上我几乎已经解决了在文件中找到一个单词数组的问题,如:
aaaaaaaaaaaaaaaaaaaaa
setrsdfdsrrtdpyrinoeq
weraderelefantewwerrr
trtevhjujhaspescadito
rtxvfdghhgperrodrdvbh
ifghhfgaaasdserpiente
naendsdsadsasafrrsdft
nssdofgfgghghghdddddd
ttegatovvvfgfyhgggggg
rrrrrrrrrrrrrrrrrrrrr
并输出在其他文件中找到的坐标。
但是我不知道如何搜索垂直单词,我所做的代码是逐行搜索,如果我发现它重新调整其坐标。
我知道我必须实现其他函数来搜索垂直单词,但最佳方法是什么?
如何更改what_coor函数来搜索垂直单词? 所以它可以找到例如单词“ant”
aaaaaaaaaaaaaaaaa*a*aaa
bbbbbbbbbbbbbbbbb*n*bbb
rrrrrrrrrrrrrrrrr*t*rtr
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX_OF_LINE 180 //max length of a line
//search a word in a line if is there returns coordinate
int what_coor ( const char *line, const char *word ){
const char *p, *x,*y;
for ( p = line; *p != '\0'; p++ ) {
x = p;
y = word;
for ( ; *x != '\0' && *y != '\0' && *x == *y; x++, y++ )
;
if(*y == '\0')
return p - line + 1;
}
return -1;
}
//reservamos espacio para nuestro myArray 'bidimensional'
char **askMemory(int filas, int cols){
char **myArray;
int i;
/* espacio para el myArray de apuntadores a entero*/
myArray = (char**)malloc(filas * sizeof(char*));
if(myArray == NULL){
fprintf(stderr, "Error 1-d \n");
exit(-1);
}
/* espacio para los myArrays de letras */
for(i = 0; i < filas; i++){
myArray[i] = (char*)malloc(cols * sizeof(char));
if(myArray[i] == NULL){
fprintf(stderr, "Error 2-d\n");
exit(-1);
}
}
return myArray;
}
//returns bidimensional array, filling it with a file
char **read (int *ptrNumWords, char *fileName) {
char line[MAX_OF_LINE];
char **myArray;
int i;
FILE *f;
if ( ! ( f = fopen (fileName, "r") ) ) {
printf ("error opening %s\n", fileName);
exit (-1);
}
while ( fgets (line, MAX_OF_LINE, f) ) {
*ptrNumWords = *ptrNumWords+1;
}
myArray = askMemory (*ptrNumWords, MAX_OF_LINE);
rewind (f);
for ( i=0 ; i <*ptrNumWords ; i++ ) {
fgets (myArray[i], MAX_OF_LINE, f);
myArray[i][strlen(myArray[i])] = '\0';
}
fclose (f);
return (myArray);
}
//seacrh word and writes output
void searchForIt(int rows, char **array2d , char *target){
FILE *sal;
sal = fopen("out.txt","a+");
int i, col;
for ( i = 0; i < rows; i++ ) {
col = what_coor ( array2d[i], target );
if( col != -1 ){
fprintf(sal,"\"%s\" \t found on (%d,%d)\n",target, i+1, col);
printf ( "\"%s\" \t found on (%d,%d)\n", target, i+1, col );
}
}
fclose(sal);
}
int main (){
int ptrNumWords = 0;
int i=0;
char **array2d;
array2d = read(&ptrNumWords, "in.txt");
char *wordsBusca[] = {"gato", "perro", "raton", "elefante", "rino", "serpiente", "pescadito"};
int len = sizeof(wordsBusca) / sizeof(char *) ;
for(i =0; i< len; i++){
searchForIt(ptrNumWords,array2d, wordsBusca[i] );
}
getch();
return 0;
}
答案 0 :(得分:2)
您可以执行以下操作:
<强>要点:强>
步骤1:以2D矩阵读取文件中的数据。矩阵的每个元素都是char
第2步:对矩阵进行转置
第3步:使用函数my_coor
水平搜索单词。
<强>详细信息:强>
您可以通过获取每行和每行的字符数来执行第1步。行数,然后动态分配2D char
矩阵空间。然后通过char读取char并继续将每个保存在矩阵中的正确位置
第2步需要基本的Mathemetics&amp;交换。
你已经实施了第3步。
答案 1 :(得分:1)
我在回复另一个问题时实施了@ 0xF1建议的变体:请参阅我的解决方案here。在这里,我实际上将10x10矩阵转换为10x40矩阵,矩阵中的连续行对应于原始10x10中从左到右,从右到左,从上到下,从下到上读取的行。
这种方法的优势(或更简单的10x20 - 仅从左到右,从上到下)是您现在拥有可用于快速搜索的所有可能方向 - 您将每个单词与40(或20)行匹配在此期间进行任何转置等。
我认为这是一项有用的改进。