gpt4 book ai didi

c - 循环中的数组元素返回随机大数字(有时为负数)

转载 作者:行者123 更新时间:2023-11-30 14:40:07 25 4
gpt4 key购买 nike

我正在扫描输入文件中的字符并尝试打印它们在输出文件中出现的次数。

我尝试专门为循环创建一个新的文件流,因为它已在前一部分代码中使用。我还尝试将 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 %

这里发生了什么?

最佳答案

由于原帖使用了toupper,因此使用tolowerislowerisupper来避免多次读取文件并循环匹配字符。

#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;
}

关于c - 循环中的数组元素返回随机大数字(有时为负数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com