我正在学习C以及它在文本文件中读取的任务之一,并让它输出格式化的文本文件。最终产品应如下所示:
1)"I must not fear.[4,17]
2)Fear is the mind-killer.[4,24]
3)Fear is the little-death that brings total obliteration.[8,56]
4)I will face my fear.[5,20]
.
.
.
13)oneWord_allAlone[1,16]
13 lines, 94 words, 481 characters
Line 10 has the most words (16)
Line 7 has the most characters (68)
我已经编写了代码并且可以得到一些紧密的东西,但信息乱序且变量错误,它会切断每个句子的第一个字母。我明白了:
I must not fear0.)
[4, 16]
ear is the mind-killer.0)
[7 39]
ear is the little-death that brings total obliteration.0)
[14 92]
.
.
.
neWord_allAlone1)
[86 470]
1 lines, 20360 words, 110685 characters
line 1 has the most words with (86)
line 1 has the most characters with 470)
在哪里获得110685个字符超出我的范围。那么,说到这里,我做错了什么?据我所知,我已经正确设置了所有变量,但输出顺序错误,第一个字符被切断,并且计数很少。任何帮助深表感谢!这是我的代码:
#include <stdio.h>
#define IN 1
#define OUT 0
void main() {
int c = 0;
int numChars = 0;
int numWords = 0;
int numLines = 0;
int state = OUT;
int test = 0;
int largestNumChars = 0;
int largestNumWords = 0;
int totalNumChars = 0;
int totalNumWords = 0;
int lineWithMostChars = 0;
int lineWithMostWords = 0;
FILE *doesthiswork = fopen("testWords.in", "r");
while ((test = fgetc(doesthiswork)) != EOF) {
if ( test == '\n') {
++numLines;
}
while ((test = fgetc(doesthiswork)) != '\n') {
++numChars;
putchar(test);
if (test == ' ' || test == '\t' || test == '\n') {
state = OUT;
} else if (state == OUT){
state = IN;
++numWords;
}
totalNumWords = totalNumWords + numWords;
totalNumChars = totalNumChars + numChars;
}
if (largestNumChars == 0) {
largestNumChars = numChars;
} else if (largestNumChars < numChars) {
largestNumChars = numChars;
lineWithMostChars = numLines;
} else {
largestNumChars = largestNumChars;
lineWithMostChars = lineWithMostChars;
}
if (largestNumWords == 0) {
largestNumWords = numWords;
lineWithMostWords = numLines;
} else if (largestNumWords < numWords) {
largestNumWords = numWords;
lineWithMostWords = lineWithMostWords;
} else {
largestNumWords = largestNumWords;
}
printf("%d) %c [%d %d]\n",numLines, test, numWords, numChars);
}
printf("%d lines, %d words, %d characters\n",
numLines, totalNumWords, totalNumChars);
printf("line %d has the most words with (%d)\n",
lineWithMostWords, largestNumWords);
printf("line %d has the most characters with (%d)\n",
lineWithMostChars, largestNumChars);
}
答案 0 :(得分:2)
嗯,最初的信件是你在第一次fgetc
电话中读取它们的时间,但是你不会像第二次putchar
那样读取它们。 {1}}致电。
并且fgetc
非常大,因为您会定期向其添加totalNumChars
,但您无法将numChars
重置为零。
我希望这会有所帮助。找到并压制这些错误很有趣!
答案 1 :(得分:0)
首先,你putchar()
行号前面的字母。然后,在第二个while
中,test
始终存储'\n'
,因此[numWords, numChars]
始终位于字母的下一行。正如@aecolley所说,numWords
,numChars
应该重新归零。
lineWithMostWords = lineWithMostWords;
应该是
lineWithMostWords = numLines;
这是我的代码,也许可以帮到你。
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#define IN 1
#define OUT 0
#define MAXLINE 500
void main()
{
int numChars = 0;
int numWords = 0;
int numLines = 0;
int state = OUT;
int test = 0;
int largestNumChars = 0;
int largestNumWords = 0;
int totalNumChars = 0;
int totalNumWords = 0;
int lineWithMostChars = 0;
int lineWithMostWords = 0;
char line[MAXLINE+1], *lineTemp;
int lineLen;
FILE *doesthiswork;
doesthiswork = fopen("testWords.in", "r");
while (fgets(line, MAXLINE, doesthiswork) != NULL)
{
numChars = 0;
numWords = 0;
lineLen = strlen(line);
line[lineLen - 1] = '\0';
lineTemp = line;
state = OUT;
++numLines;
while ((test = *lineTemp++) != '\0')
{
++numChars;
if (test == ' ' || test == '\t')
{
state = OUT;
}
else if (state == OUT){
state = IN;
++numWords;
}
}
totalNumWords = totalNumWords + numWords;
totalNumChars = totalNumChars + numChars;
if (largestNumChars == 0)
{
largestNumChars = numChars;
}
else if (largestNumChars < numChars)
{
largestNumChars = numChars;
lineWithMostChars = numLines;
}
if (largestNumWords == 0)
{
largestNumWords = numWords;
lineWithMostWords = numLines;
}
else if (largestNumWords < numWords)
{
largestNumWords = numWords;
lineWithMostWords = numLines;
}
printf("%d) %s [%d %d]\n",numLines, line, numWords, numChars);
}
printf("%d lines, %d words, %d characters\n", numLines, totalNumWords, totalNumChars);
printf("line %d has the most words with (%d)\n", lineWithMostWords, largestNumWords);
printf("line %d has the most characters with (%d)\n", lineWithMostChars, largestNumChars);
}