我正在扫描输入文件中的字符,并尝试打印它们在输出文件中出现的次数。
我已经尝试为循环专门创建一个新的文件流,因为它已在上一部分代码中使用。我还尝试过将long long int
强制转换为数组和print语句,这只会给我更大的数字。
FILE* input;
FILE* output;
input = fopen("input.txt", "r");
output = fopen("report.txt", "w");
...
char letterTemp;
float letterMax;
int upperCounter = 0;
int lowerCounter = 0;
char alphaChars[26];
int charCounter[26];
for (char c = 'a'; c <= 'z'; ++c) {
alphaChars[c - 'a'] = c;
}
for (int i = 0; i < 26; i++) {
fopen("input.txt", "r");
while ((letterTemp = fgetc(input)) != EOF) {
if (alphaChars[i] == letterTemp) {
lowerCounter++;
charCounter[i]++;
}
else if (toupper(alphaChars[i]) == letterTemp) {
upperCounter++;
charCounter[i]++;
}
}
fclose(input);
}
letterMax = lowerCounter + upperCounter;
fprintf(output, "LETTER STATISTICS\n\nCategory How many in file %% of all letters\n----------------------------------------------------------------------\nUppercase%31d%20.2f %%\nLowercase%31d%20.2f %%\n", upperCounter, 100 * upperCounter / letterMax, lowerCounter, 100 * lowerCounter / letterMax);
for (int i = 0; i < 25; i++) {
fprintf(output, "%c%39d%20.2f %%\n", alphaChars[i], charCounter[i], 100 * charCounter[i] / letterMax);
}
哪个会给我...
LETTER STATISTICS
Category How many in file % of all letters
----------------------------------------------------------------------
Uppercase 9 12.16 %
Lowercase 65 87.84 %
a 2080375142 25393028.00 %
b 32766 44278.38 %
c 2080375153 25393042.00 %
d 32767 44279.73 %
e -677299551 13371914.00 %
f 32734 44235.14 %
g 0 0.00 %
h 2 2.70 %
i 7 9.46 %
j 0 0.00 %
k 0 0.00 %
l 7 9.46 %
m 2 2.70 %
n 1 1.35 %
o 7 9.46 %
p 1 1.35 %
q 2081514112 26932176.00 %
r 32770 44283.79 %
s -677300459 13370687.00 %
t 32735 44236.49 %
u 2 2.70 %
v 1 1.35 %
w 2 2.70 %
x 1 1.35 %
y 5 6.76 %
这是怎么回事?
答案 0 :(得分:1)
由于原始帖子使用toupper
,因此请使用tolower
islower
和isupper
以避免多次读取文件并循环匹配字符。
#include <stdio.h>
#include <ctype.h>
int main ( ) {
FILE* input = NULL;
FILE* output = NULL;
int letterTemp;
float letterMax;
int upperCounter = 0;
int lowerCounter = 0;
int charCounter[256] = { 0};//256 for all 8 bit values
if ( NULL != ( input = fopen ( "input.txt", "r"))) {
while ( ( letterTemp = fgetc ( input)) != EOF) {
if ( islower ( letterTemp)) {//lower case
lowerCounter++;
charCounter[letterTemp]++;//increment count
}
if ( isupper ( letterTemp)) {//upper case
upperCounter++;
charCounter[tolower ( letterTemp)]++;//increment lower case count
}
}
fclose ( input);
letterMax = lowerCounter + upperCounter;
if ( NULL != ( output = fopen ( "report.txt", "w"))) {
fprintf ( output , "LETTER STATISTICS\n\n");
fprintf ( output , "Category How many in file %% of all letters\n");
fprintf ( output , "--------------------------------------------------------------\n");
fprintf ( output , "Uppercase%31d%20.2f %%\nLowercase%31d%20.2f %%\n"
, upperCounter
, 100 * upperCounter / letterMax
, lowerCounter
, 100 * lowerCounter / letterMax);
for (int i = 0; i < 256; i++) {
if ( islower( i)) {//only print lower case values
fprintf ( output, "%c%39d%20.2f %%\n"
, i
, charCounter[i]
, 100 * charCounter[i] / letterMax);
}
}
fclose ( output);
}
else {
fprintf ( stderr, "could not open output file\n");
}
}
else {
fprintf ( stderr, "could not open input file\n");
}
return 0;
}