gpt4 book ai didi

c - 决定平均类(class)成绩的计划

转载 作者:行者123 更新时间:2023-11-30 18:44:59 24 4
gpt4 key购买 nike

该程序应该将包含学生测验成绩的文本文件写入另一个包含学生姓名的文件,并为学生分配成绩

#include <stdio.h>
#include <stdlib.h>

void format(FILE *outputFile);
void copyNames(FILE *inputFile, FILE *outputFile);
void copyScores(FILE *inputFile, FILE *outputFile);

int main(int arc, char* argv[])
{
FILE *inputScores, *averageScores;
inputScores = fopen("quiz.txt", "r");
averageScores = fopen("average.txt", "w");

if (inputScores == NULL || averageScores == NULL)
{
printf("ERROR: The file(s) could not be opened!");
return(1);
}


copyNames(inputScores, averageScores);
copyScores(inputScores, averageScores);

fclose(inputScores);
fclose(averageScores);

return 0;
}


void copyNames(FILE *inputFile, FILE *outputFile){
char firstName[10], lastName[10], ch;
ch = fgetc(inputFile); //sets ch to a place in the file
fseek(inputFile, 0, SEEK_SET); //resets ch so it is at the beginning of the file
while (ch != EOF){
int i = 0, j = 0; //resets values in the array so you can overwrite it
for (ch = fgetc(inputFile); ch != ' ' && ch != EOF; ch = fgetc(inputFile)){ //gets the first name and puts it into an array
firstName[i] = ch;
i++;
}
for (ch = fgetc(inputFile); ch != ' ' && ch != EOF; ch = fgetc(inputFile)){ //gets last name and puts it into array
lastName[j] = ch;
j++;
}
lastName[j] = '\0'; //truncates the arrays
firstName[i] = '\0';
while (ch != '\n' && ch != EOF){ //moves the placement of ch to avoid all the grades to get the next name
ch = fgetc(inputFile);
}
fprintf(outputFile, "%s, %s \n", lastName, firstName); //prints the names to the output file
}
}

void copyScores(FILE *inputFile, FILE *outputFile){
fseek(inputFile, 0, SEEK_SET); //resets fgetc again
char lineMemory[60], sc = fgetc(inputFile);
while (sc != EOF){
int i = 0, num = 0, scores[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (sc = fgetc(inputFile); sc != '\n' && sc != EOF; sc = fgetc(inputFile)){ //writes the whole line into an array
lineMemory[i] = sc;
i++;
}
lineMemory[i] = '\0'; //truncates the array
for (int check = 0; lineMemory[check] != '\0'; check++){ //walks through the string
if (isdigit(lineMemory[check]) != 0){ //looks for the digits in the string
int j = lineMemory[check] - '0'; //turns the characters into integers
scores[num] = j; //puts the integer into the array
num++;
}
}
float avg, total = 0;
for (int indx = 0; indx < 10; indx++){
total += scores[indx];
}
avg = total / 10; //finds average of the grades
for (int x = 0; x < 10; x++){
fprintf(outputFile, "%2d", scores[x]); //prints the quiz grades
}
fprintf(outputFile, "%10g\n", avg); //prints the average
}
}

void copyAll(FILE *inputFile, FILE *outputFile){
char ch = fgetc(inputFile);

while (ch != EOF){
ch = fgetc(inputFile);
fputc(ch, outputFile);
}
printf("Data successfully written.\n");
}

结果应该是这样的:

 Alex Smith 98 100 90 82 92.5
john Adams 100 90 82 90 90.5
//92.5 and 90.5 being the average.

但我的代码只是完全显示姓名以及姓名下方的成绩。像:

 Alex Smith
john adams
98 100 90
100 90 82 etc...

最佳答案

您的代码中存在多个问题:

  • 您无法在 2 个单独的循环中处理姓名和分数,为了获得预期的输出,您必须一次处理一行。
  • fgetc() 返回一个 int,不要将其分配给 char,否则您将无法可靠地检测 EOF
  • 您调用fgetc()次数过多。您应该使用组合的读取和测试习惯用法:

    void copyAll(FILE *inputFile, FILE *outputFile){
    int ch ;

    while ((ch = fgetc(inputFile)) != EOF) {
    fputc(ch, outputFile);
    }
    printf("Data successfully written.\n");
    }
  • 您从不检查潜在的缓冲区溢出:无效的输入将导致未定义的行为。

  • copyScores 中用于将分数转换为数字的代码不正确:它只能处理单位数分数。
  • 您使用“%2d”输出分数,这不会分隔大于9的分数。请改用“%d”
  • 您读取了 num 个分数,但始终输出 10 个分数及其平均值。
  • 调用fseek(inputFile, 0, SEEK_SET);是没有用的。只有在更新模式下打开的流才需要它们,正确使用它非常棘手。

这是一个更简单的版本:

#include <stdio.h>

int main(int argc, char *argv[]) {
FILE *input, *output;
char firstname[50], lastname[50];
int score, n, total;

input = fopen("quiz.txt", "r");
output = fopen("average.txt", "w");
if (input == NULL || output == NULL) {
printf("ERROR: The file(s) could not be opened!");
return 1;
}

while (fscanf(input, "%49s%49s", firstname, lastname) == 2) {
fprintf(output, "%s %s", firstname, lastname);
for (n = 0, total = 0; fscanf(input, "%d", &score) == 1; n++) {
fprintf(output, " %d", score);
total += score;
}
fprintf(output, " %.2f\n", n == 0 ? 0.0 : (double)total / n);
}
fclose(input);
fclose(output);

return 0;
}

关于c - 决定平均类(class)成绩的计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55988293/

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