#include <stdio.h>
#include <stdlib.h>
#define N 100
#define LENGTH 100
int get_size()
{
int n;
printf("please input the size of squaer:");
scanf("%d", &n);
if (n < 1)
return 0;
return n;
}
int get_input(char **p, int n)
{
if (NULL == p)
return 0;
int i, j;
for (i = 0; i < n; i++)
{
p[i] = (char*)malloc(sizeof(char) * n);
if (NULL == p[i])
return 0;
}
getchar(); /*get the return key*/
printf("please input the chars in order:\n");
for (i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%c", &p[i][j]);
}
getchar();
}
return 1;
}
int show_square(char **p, int n)
{
if (NULL == p)
return 0;
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
printf("%c", p[i][j]);
printf("\n");
}
return 1;
}
int get_words(char **p)
{
printf("please input the words by order:\n");
int i = 0;
for (; i < N; i++)
{
p[i] = (char*)malloc(sizeof(char) * LENGTH);
if (NULL == p[i])
{
fprintf(stderr, "stevens: malloc error!\n");
exit(1);
}
memset(p[i], 0, LENGTH);
gets(p[i]);
if ('\0' == *(p[i]))
break;
}
}
int show_words(char **p)
{
if (NULL == p)
return 0;
int i = 0;
for (; i < N; i++)
{
if (*(p[i]) != '\0')
puts(p[i]);
else
break;
}
return 1;
}
int match_to_right(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j;
int len = strlen(p);
if ((n - col) < len)
return 0;
for (i = col, j = 0; j < len; i++, j++)
if (p_arriy[row][i] != p[j])
return 0;
*a = row;
*b = --j;
return 1;
}
int match_to_left(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j;
int len = strlen(p);
if ((col + 1) < len)
return 0;
for (i = col, j = 0; j < len; i--, j++)
if (p_arriy[row][i] != p[j])
return 0;
*a = row;
*b = ++i;
return 1;
}
int match_to_up(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j;
int len = strlen(p);
if ((row + 1) < len)
return 0;
for (i = row, j = 0; j < len; i--, j++)
if (p_arriy[i][col] != p[j])
return 0;
*a = ++i;
*b = col;
return 1;
}
int match_to_down(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j;
int len = strlen(p);
if ((n - row) < len)
return 0;
for (i = row, j = 0; j < len; i++, j++)
if (p_arriy[i][col] != p[j])
return 0;
*a = --i;
*b = col;
return 1;
}
int match_to_up_left(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j, k;
int len = strlen(p);
if (((row + 1) < len) || ((col + 1) < len))
return 0;
for (i = row, j = col, k = 0; k < len; i--, j--, k++)
if (p_arriy[i][j] != p[k])
return 0;
*a = ++i;
*b = ++j;
return 1;
}
int match_to_up_right(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j, k;
int len = strlen(p);
if (((row + 1) < len) || ((n - col) < n))
return 0;
for (i = row, j = col, k = 0; k < len; i--, j++, k++)
if (p_arriy[i][j] != p[k])
return 0;
*a = ++i;
*b = --j;
return 1;
}
int match_to_down_left(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j, k;
int len = strlen(p);
if (((col + 1) < len) || ((n - row) < len))
return 0;
for (i = row, j = col, k = 0; k < len; i++, j--, k++)
if (p_arriy[i][j] != p[k])
return 0;
*a = --i;
*b = ++j;
return 1;
}
int match_to_down_right(char **p_arriy, char *p, int row, int col, int n, int *a, int *b)
{
int i, j, k;
int len = strlen(p);
if (((n - row) < len)|| ((n - col) < len))
return 0;
for (i = row, j = col, k = 0; k < len; i++, j++, k++)
if (p_arriy[i][j] != p[k])
return 0;
*a = --i;
*b = --j;
return 1;
}
int find_a_word(char **p_arriy, char *p, int n)
{
if (p_arriy == NULL || p == NULL)
return 0;
int i, j, len, a, b;
len = strlen(p);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
if (p_arriy[i][j] != p[0])
continue;
if(1 == match_to_right(p_arriy, p, i, j, n, &a, &b)
|| 1 == match_to_left(p_arriy, p, i, j, n, &a, &b)
|| 1 == match_to_up(p_arriy, p, i, j, n, &a, &b)
|| 1 == match_to_down(p_arriy, p, i, j, n, &a, &b)
|| 1 == match_to_up_left(p_arriy, p, i, j, n, &a, &b)
|| 1 == match_to_up_right(p_arriy, p, i, j, n, &a, &b)
|| 1 == match_to_down_left(p_arriy, p, i, j, n, &a, &b)
|| 1 == match_to_down_right(p_arriy, p, i, j, n, &a, &b) )
{
printf("the words \"%s\" is from [%d][%d] to [%d][%d]\n", p, i, j, a, b);
return 1;
}
}
return 0;
}
int find_words(char **p_arriy, char **p_words, int n)
{
int i = 0;
while (*(p_words[i]) != '\0')
{
find_a_word(p_arriy, p_words[i], n);
i++;
}
}
int main (int argc, char *argv[])
{
int n;
if (0 == (n = get_size()))
{
printf("the size of n should greater thant 1!\n");
exit(1);
}
char **p = (char**)malloc(sizeof(char*) * n);
if (NULL == p)
{
fprintf(stderr, "stevens: malloc error!\n");
exit(1);
}
if (0 == get_input(p, n))
{
fprintf(stderr, "stevens: input error!\n");
exit(1);
}
char **q = (char**)malloc(sizeof(char*) * N);
if (NULL == q)
{
fprintf(stderr, "stevens: malloc error!\n");
exit(1);
}
get_words(q);
find_words(p, q, n);
return 1;
}
|