c - 确定平均测验成绩的程序

标签 c

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

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


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");
}
Smith, Alex    98 100 90 82 92.5
Adams, John    100 90 82 90 90.5

//with four spaces between the name and the averages
//92.5 and 90.5 being the average.

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

Alex Smith
john adams
98 100 90 
100 90 82

etc...

最佳答案

But my code just displays the name altogether and the grades underneath the names. like:

恐怕发布的代码不再有此输出:您复制了 my answer's code作为 main 函数的替代品,它的输出应该比以前更接近预期。但你应该:

  • 删除其他无用的功能
  • 稍微调整 printf 以匹配更新的问题陈述:

    fprintf(output, "%s, %s   ", lastname, firstname);
    
  • 调整平均值printf以输出单个小数:

    fprintf(output, " %.1f\n", n == 0 ? 0.0 : (double)total / n);
    

但请注意,问题陈述不一致:

Smith, Alex    98 100 90 82 92.5
Adams, John    100 90 82 90 90.5

with four spaces between the name and the averages
92.5 and 90.5 being the average.

名字和第一年级之间出现 4 个空格,平均出现在所有年级之后,与最后一个年级隔开一个空格。

关于c - 确定平均测验成绩的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56013461/

相关文章:

c - 如何使用 goto 模拟 C 中的异常?

创建 C 子字符串 : looping with assignment operator VS strncopy, 哪个更好?

c - 链接 md5.h 库以实现 HTTP Digest 示例

将全局 char** 复制到新的 char**

c - Visual Studio 错误 C2226 'unexpected type'

c - 简单的套接字库需要一些检查

c - C编程中使用循环和scanf的字符串数组

c - 尝试二分查找

c - 等待输入后再继续 - Arduino Uno

c 清理未使用的线程