【发布时间】:2012-08-24 13:12:25
【问题描述】:
我的任务是编写一个程序,它应该在前面的特定序列之后省略一个单词。
我已经准备了一个有效的 getword 程序(返回 char *),现在我只有在 main 有以下 sn-p 代码的问题,它允许我检测应该在哪里删除这个词。但我不知道如何从 outfile 中实际省略/删除该词。
int main(int argc, char **argv)
{
FILE *infile = NULL, *outfile = NULL;
char *word = NULL;
int c;
int yes = 0;
int counter = 0;
/* completely irrelevant - opening, writing to files, error messages etc. */
while (1) {
c = fgetc(infile);
word = getword(infile);
if (counter == 2) {
counter = 0;
yes = 0;
/* here it should somehow omit the word */
continue;
}
if (choose(word, strlen(word))) {
fputs(word, outfile);
counter++;
yes = 1;
} else {
fputs(word, outfile);
if (yes == 1) {
counter--;
}
}
free(word);
}
/* completely irrelevant */
}
编辑:添加以澄清
"getword 只是读取一个单词,它不执行任何检查是否是我要查找的单词。main() 进行检查。当 if (choose) 满足时,则表示该单词包含序列我正在寻找的字母,并且应该省略该特定单词之后的第二个单词。变量“counter”和“yes”可能不是完美的算法,但起初我希望它工作,然后我会尝试简化一下。“计数器”最多计数 2 以确定要省略哪个单词,而“是”有助于在我们移动到不满足 if (choose) 条件的单词后递增计数器。
提前致谢!
【问题讨论】: