好的,我的项目也是如此,我打了个屁股。我需要能够选择一个类别,然后让它指向一个单词列表,然后需要各种用途。
远远将我的单词分成各种类别,它们可以嵌入到我的2D数组中。但是,我现在需要一个是根据用户输入检查这些单词以查看它们是否匹配。
我做了一个函数来检查2数组的匹配,但是在函数中输入单词时遇到了麻烦。
以下是我的单词的排序方式。
printf("\n\n------------------------------");
printf("\n1. Animals");
printf("\n2. Colors");
printf("\n3. Planets");
printf("\n4. Weekdays");
printf("\n------------------------------");
int valid = 0;
while(valid == 0)
{
printf("\n\nYour selection: ");
int catagory;
scanf("%d", &catagory);
if(catagory == 1)
{
char word[] = "dog";
char word2[] = "cat";
char word3[] = "chicken";
char word4[] = "mouse";
char word5[] = "lion";
char word6[] = "rabbit";
char word7[] = "pig";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;
}
else if(catagory == 2)
{
char word[] = "red";
char word2[] = "blue";
char word3[] = "yellow";
char word4[] = "brown";
char word5[] = "pink";
char word6[] = "green";
char word7[] = "black";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;
}
else if(catagory == 3)
{
char word[] = "earth";
char word2[] = "neptune";
char word3[] = "venus";
char word4[] = "mars";
char word5[] = "jupiter";
char word6[] = "saturn";
char word7[] = "pluto";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;
}
else if(catagory == 4)
{
char word[] = "monday";
char word2[] = "tuesday";
char word3[] = "wednesday";
char word4[] = "thursday";
char word5[] = "friday";
char word6[] = "saturday";
char word7[] = "saturn";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;
}
else if(catagory > 4)
{
printf("\n\nInvalid input. Please select again.\n");
}
else if(catagory < 1)
{
printf("\n\nInvalid input. Please select again.\n");
}
}
在此之下我现在再次需要这些话:
clock_t start = clock(), diff;
int match = 0;
int correct = 0;
print_array(myArray);
printf("\nPlease type any words that you can find");
while (correct != 7)
{
printf("\n: ");
char word2[256];
fgets(word2,256,stdin);
match = checker("word from category",word2);
if(match == 1)
{
correct++;
printf("\nWeldone you found a match!");
}
}
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds\n", msec/1000, msec%1000);
如您所见,我的阵列超出了范围。有谁能给我一个解决方案?
答案 0 :(得分:0)
也许定义一个全局三维数组可能会有所帮助:
char words[4][8][];
words[0] = {"dog", "cat", "chicken", "mouse", "lion", "rabbit", "pig"};
//etc...
(you can of course define it in one line)
然后您可以使用以下方式访问它:
words[catagory][<WORD>]
(最后一个维度是单词本身)
或者我误解了你的问题?然后,我需要知道您的horizontal
vertical
和diagonal
函数正在做什么,以及整个代码。
答案 1 :(得分:0)
int horizontal(char *myArray, char * word)
{
int success = 0;
while(success == 0)
{
int lettercount = 0;
int length = strlen(word);
int rand = rand_num_gen();
//printf("i = %d\n", rand);
int a;
for(a=0; a < (length); a++)
{
if(myArray[rand] == '.')
{
myArray[rand] = word[a];
rand++;
lettercount++;
}
else
{
break;
}
}
//printf("lettercount = %d\n", lettercount);
//printf("length = %d\n", length);
if(lettercount == length)
{
success = 1;
}
}
}
答案 2 :(得分:0)
据我所知,您的第一个片段是接受用户输入并决定使用哪组词。
一个好的做法是分离关注点,因此您应该将数据与选择分开,然后调用函数将所有单词放在二维数组上(我猜测这是一个单词搜索?)。
void letsPlayAGame() {
char categories[4][8][20] ={
{"dog", "cat", "chicken", "mouse", "lion", "rabbit", "pig"},
{"red", "blue", "yellow", "brown", "pink", "green", "black"},
{"earth", "neptune", "venus", "mars", "jupiter",
"saturn", "pluto"},
{"monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "saturn"}
};
int category = getCategory();
//place words onto an array
//assuming board size is 10x10
char[10][10] myArray;
placeWords(myArray, categories[category]);
queryResponce(myArray, categories[category]);
}
int getCategory(){
int cagetory = -1;
while(category == -1){
//ask for category
}
return category;
}
char** placeWords(char** myArray, char** category){
//add your words to it with diagonal, etc
//return your array
}
void queryResponse(char** myArray, char** answers){
clock_t start = clock(), diff;
int match = 0;
int correct = 0;
print_array(myArray);
printf("\nPlease type any words that you can find");
while (correct != 7)
{
printf("\n: ");
char word[256];
fgets(word,256,stdin);
//check if word is in answers
match = word_in(word,answers);
if(match == 1)
{
correct++;
printf("\nWeldone you found a match!");
}
}
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds\n", msec/1000, msec%1000);
}