如何运行这段代码C语言 [英] How to run this code C language
问题描述
int findWord(char *chosenWord)
{
FILE *dico = NULL;
int wordsNumber = 0;
int chosenWordNumber = 0;
int readCharacter = 0;
int i = 0;
dico = fopen("dico.txt","r");
if(dico == NULL)
{
printf("\nImpossible to load words dictionary !");
return 0;
}
do
{
readCharacter = fgetc(dico);
if( readCharacter == '\n' )
wordsNumber++;
} while( readCharacter != EOF );
chosenWordNumber = aleatoryNumber(wordsNumber);
rewind(dico);
while( chosenWordNumber > 0 )
{
readCharacter = fgetc(dico);
if(readCharacter == '\n')
chosenWordNumber--;
}
fgets(chosenWord,100,dico);
chosenWord[strlen(chosenWord) - 1] = '\0';
fclose(dico);
return 1;
}
int aleatoryNumber(int maxNumber)
{
srand(time(NULL));
return (rand() % maxNumber);
}
我的尝试:
我想详细解释每一行负责选择随机词。
What I have tried:
I want a detailed explanation for each line responsible for choosing the random word.
推荐答案
你知道解释代码行的工作量是多少吗?线是?
每一行都需要一段解释!例如:
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();
创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。
可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?
不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 你想坐下45分钟然后输入逐行描述没有充分的理由吗?
Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.
Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?
No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
你必须自己理解代码,但我可以给出你的概述。阅读一些学习语言的教程和谷歌的功能。
代码正在读取文件并计算\ n所谓的新行。读取范围内的值后,随机选择。比从开始重新读取文件,并将选择的单词复制到缓冲区中。我会用 printf 添加一些输出。
You must understand the code yourself, but I can give you an overview. Read some tutorial for learning the language and google for the functions.
The code is reading a file and counting the "\n" so called new lines. After reading a values in the range count is selected by random. Than the file is re-read from the start and the choosen word is copied into a buffer. I would add some output with printf of that.
printf ("The chosen word is: %s\n", chosenWord);
负责选择随机单词的行是
The line responsible for choosing the random word is
chosenWordNumber = aleatoryNumber(wordsNumber);
它调用 aleatoryNumber
将找到的单词的数量作为参数传递。
It calls aleatoryNumber
passing the count of found words as argument.
The
int aleatoryNumber(int maxNumber)
{
srand(time(NULL));
return (rand() % maxNumber);
}
函数返回 0
和(maxNumber-1)
,因为
rand [ ^ ] C
库函数返回一个数字 0
和 RAND_MAX
,其结果被限制在 0 ... maxNumber
范围由提醒操作员。
其他解决方案已经广泛解释了完整的代码行为。
function returns a random number between 0
and (maxNumber-1)
, because
the rand[^] C
library function returns a number between 0
and RAND_MAX
and its result is clamped in the 0...maxNumber
range by the reminder operator.
Other solutions already explained broadly the full code behaviour.
这篇关于如何运行这段代码C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!