我有两个函数,一个是算法,一个是主函数,它打印发送到算法的char矩阵中的所有单词。
当两个函数都在一个C文件中并且我的char矩阵以正确的方式填充时,我得到了预期的结果。当我将main函数和算法分成两个不同的C文件时,我的问题就出现了。发送到另一个C文件中的算法的矩阵充满了大量垃圾(在35左右的矩阵中只有大约5行是正确的而不是垃圾)。
为什么会发生这种情况?我想这可能是矩阵指针的发送方式。
这是我的代码(忽略O(n)问题):
算法文件:
#include <stdio.h>
#include <string.h>
#define MATRIX_SIZE 4
#define SMALLEST_WORD 2
#define LONGEST_WORD 6
#define MAX_AMOUNT_OF_WORDS 100
int goodDirection(char* s);
void startsfrom(int i,int j);
int isWord(char* s);
int printWords(char mat[MATRIX_SIZE][MATRIX_SIZE],
int i,
int j,
int boolean[MATRIX_SIZE][MATRIX_SIZE],
char foundWords[MAX_AMOUNT_OF_WORDS][LONGEST_WORD],
int currentCharIndex,
char untilNow[LONGEST_WORD],
int* numOfWords,
int usedPath[MATRIX_SIZE][MATRIX_SIZE],
int firstRecursive)
{
boolean[i][j] = 1;
char tmpUntilNow[currentCharIndex + 2];
int i2 = 0;
untilNow[currentCharIndex] = mat[i][j];
untilNow[currentCharIndex + 1]= '\0';
if(currentCharIndex > 0 ){
if(isWord(untilNow)){
for(i2 = 0 ;i2 < strlen(untilNow);i2++){
foundWords[*numOfWords][i2] = untilNow[i2];
//printf("numOfWords[i2] = %c , untilNow[i2] = %c , *numOfWords = %d ", foundWords[*numOfWords][i2] , untilNow[i2], *numOfWords);
//printf("\n");
}
foundWords[*numOfWords][i2] = '\0';
*numOfWords += 1;
foundWords[*numOfWords][0] = 'a';
foundWords[*numOfWords][1] = 'a';
foundWords[*numOfWords][2] = 'a';
//foundWords[*numOfWords][0] = '\n';
i2 =0;
}
}
i2 =0;
int j2;
for( ; i2 < strlen(untilNow); i2++){
tmpUntilNow[i2] = untilNow[i2];
}
tmpUntilNow[i2 + 1] = '\0';
int boolean2[MATRIX_SIZE][MATRIX_SIZE];
for(i2 = 0 ; i2 < MATRIX_SIZE ;i2++){
for(j2 = 0 ; j2 < MATRIX_SIZE ;j2++){
boolean2[i2][j2] = boolean[i2][j2];
}
}
if(currentCharIndex < LONGEST_WORD){
if( i-1 >= 0){
if(!(boolean2[i-1][j])){
//currentCharIndex + 1 is same as i2. I thought that this is more logical to write for coherency.
tmpUntilNow[currentCharIndex + 1] = mat[i-1][j];
if(goodDirection(tmpUntilNow) ){
printWords(mat,i-1,j,boolean2,foundWords,currentCharIndex + 1,tmpUntilNow, numOfWords, usedPath, 0);
}
}
}
for(i2 = 0 ; i2 < MATRIX_SIZE ;i2++){
for(j2 = 0 ; j2 < MATRIX_SIZE ;j2++){
boolean2[i2][j2] = boolean[i2][j2];
}
}
if( j-1 >= 0){
if(!(boolean2[i][j-1])){
tmpUntilNow[currentCharIndex + 1] = mat[i][j-1];
if(goodDirection(tmpUntilNow)){
printWords(mat,i,j-1,boolean2,foundWords,currentCharIndex + 1,tmpUntilNow, numOfWords, usedPath, 0);
}
}
}
for(i2 = 0 ; i2 < MATRIX_SIZE ;i2++){
for(j2 = 0 ; j2 < MATRIX_SIZE ;j2++){
boolean2[i2][j2] = boolean[i2][j2];
}
}
if(i+1 <= MATRIX_SIZE){
if(!(boolean2[i+1][j])){
tmpUntilNow[currentCharIndex + 1] = mat[i+1][j];
if(goodDirection(tmpUntilNow) ){
printWords(mat,i+1,j,boolean2,foundWords,currentCharIndex + 1,tmpUntilNow, numOfWords, usedPath, 0);
}
}
}
for(i2 = 0 ; i2 < MATRIX_SIZE ;i2++){
for(j2 = 0 ; j2 < MATRIX_SIZE ;j2++){
boolean2[i2][j2] = boolean[i2][j2];
}
}
if(j+1 <= MATRIX_SIZE){
//printf(" i is : %d , and j is %d and boolen is: %d\n",i, j+1,(boolean[i][j+1]));
if(!(boolean2[i][j+1])){
tmpUntilNow[currentCharIndex + 1] = mat[i][j+1];
if(goodDirection(tmpUntilNow) ){
printWords(mat,i,j+1,boolean2,foundWords,currentCharIndex + 1,tmpUntilNow, numOfWords, usedPath, 0);
}
}
}
}
}
主文件:
#include <stdio.h>
#include "matrixWords.h"
#include <string.h>
//#include <string.h>
#define MATRIX_SIZE 4
#define SMALLEST_WORD 3
#define LONGEST_WORD 5
#define MAX_AMOUNT_OF_WORDS 100
#include <stdio.h>
#include "matrixWords.h"
#include <string.h>
//#include <string.h>
#define MATRIX_SIZE 4
#define SMALLEST_WORD 3
#define LONGEST_WORD 5
#define MAX_AMOUNT_OF_WORDS 100
int main() {
char matrix [4] [4] = {{'C','A','R','T'},{'E','T','A','K'},{'E','S','M','E'},{'L','L','P','N'}};
//char test [1][1];
char foundWords[MAX_AMOUNT_OF_WORDS][LONGEST_WORD];
char untilNow[LONGEST_WORD + 1];
untilNow[0] = matrix[0][0];
int usedPath[MATRIX_SIZE][MATRIX_SIZE] = ;
int numOfWords = 0;
int i = 0, j = 0;
for(; i < MATRIX_SIZE ; i++){
for(; j< MATRIX_SIZE ;j++){
int boolean[MATRIX_SIZE][MATRIX_SIZE] = {{0}};
printWords(matrix,i,j,boolean,foundWords,0,untilNow,&numOfWords,usedPath,1);
}
j= 0;
}
int flag = 1;
int a = 0;
for(; a < MAX_AMOUNT_OF_WORDS && flag; a++){
if(foundWords[a][1] == 'a'){
flag = 0;
}
int b = 0;
char word[LONGEST_WORD];
//printf("\n \n");
for(;b<LONGEST_WORD - 1;b++){
word[b] = foundWords[a][b];
//printf("foundWords[a][b] is %c \n",foundWords[a][b] );
}
word[b] = '\0';
int j = 0, flag2 = 1;
for(; j < b && flag2 ; j++){
if(isWord(word)){
flag2 = 0;
b = 0;
for(;b < strlen(word); b++){
//Tomer chack
printf("%c",word[b]);
}
printf(",\n");
}
else{
//printf("%c and a is %d \n",foundWords[a][0], a);
--b;
word[b] = '\0';
}
}
}
return 0;
}
顺便说一下,这是我与C合作的第一个课程,因此,我是这个主题的新手。
输出:
良好的产出:
CAT,CATS,CAR,CART,ATE,ART,RAT,RATS,RAT,RATS,RAMP,RAKE, RAKE,TRAM,TRAMS,TAME,TAKE,TAKEN,TAKE,ART,ATE,STEEL,STAMP, 卖,卖,做,做,我,男,
输出错误:
CAT,ART,RAMP,TAME,ATE,MAKE,