【问题标题】:C programing,reading integers from a file and over write them to letterC编程,从文件中读取整数并将它们重写为字母
【发布时间】:2017-03-22 23:33:27
【问题描述】:

iv 有一个 xls 文件要在 c 中打开。在这个文件中有 10 行(每行代表一个学生),每行有 12 个数字(每个数字代表每个科目的学生成绩,共 12 个)。我需要扫描这些数据并计算每个学生的 GPA 和分类程度。所以我需要在屏幕上显示每个学生(10)的 12 年级(从数字转换为字母)以及他们的 GPA 和资格分类。波纹管是我到目前为止所做的。我能够将 10 行显示在屏幕上,每行有 12 个数字,但我不知道如何在阅读这些数字后进行计算。感谢您的帮助或建议。

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

int main()  
{
    FILE * fPointer;
    fPointer=fopen("hello.xls", "r");

    int singleLine[150];

    while(!feof(fPointer)) {;
        fgets (singleLine,150,fPointer);
        puts(singleLine);
    }

    fclose(fPointer);

    return 0;
}

【问题讨论】:

  • 您需要将值解析为单个变量。使用strtok+strtol 或sscanf。进行搜索,因为那里有很多问题和示例。
  • 建议,替换循环中的前两行:`while( fgets (singleLine, sizeof(singleLine), fPointer);
  • 在调用fopen() 时,始终检查 (!=NULL) 返回值以确保操作成功。如果不成功,请使用:{ perror( "fopen for hello.xls for read failed" ); exit( EXIT_FAILURE ); 其中exit() 和EXIT_FAILURE 在stdlib.h 头文件中
  • 在调用puts() 之后,调用一个新函数,将一个指向char 数组singleLine[] 的指针传递给它。该新函数可以将输入行解析为各个字段。然后计算学生平均/ GPA。使用包含以下函数的循环可以轻松提取 12 个等级:strtok() 和函数:strtol()

标签: c arrays file for-loop fgets


【解决方案1】:

一些初步事项。 main() 的函数签名应为 int main(void)、int main(int argc, char **argv) 或 int main(int argc, char *argv[])。

当您打开文件时,总是需要检查是否失败。 fopen() 失败时返回一个空指针,因此您可以检查这一点,并适当地处理错误。

在循环读取文件内容时,使用feof() 控制循环几乎总是错误的。这是因为feof() 检查文件结束指针,该指针本身仅在 IO 操作失败时设置。在贴出代码的情况下,进入循环,如果没有什么可读取的,singleLine缓冲区内容保持不变,将文本文件的最后一行加倍。

解决您的问题的最简单方法是假定每行包含 12 个等级,并在检索到的每行输入时使用sscanf() 对其进行解析。这不是一个优雅的解决方案:

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

#define MAX_STUDENTS  100

int main(void)
{
    /* Open a file */
    FILE *fPointer = fopen("hello.xls", "r");

    /* Make sure file opened successfully */
    if (fPointer == NULL) {
        fprintf(stderr, "Unable to open file\n");
        exit(EXIT_FAILURE);
    }

    char singleLine[500];
    double studentGPA[MAX_STUDENTS];
    int grade[12];
    size_t studentIndex = 0;

    /* Loop to fetch data and calculate GPAs */
    while ((fgets(singleLine, sizeof singleLine, fPointer)) != NULL &&
           studentIndex < MAX_STUDENTS) {
        if (sscanf(singleLine, "%d %d %d %d %d %d %d %d %d %d %d %d",
                   &grade[0], &grade[1], &grade[2], &grade[3],
                   &grade[4], &grade[5], &grade[6], &grade[7],
                   &grade[8], &grade[9], &grade[10], &grade[11]) != 12) {
            fprintf(stderr, "Incorrect number of grades on line %zu\n",
                    studentIndex);
            exit(EXIT_FAILURE);
        }
        int sum = 0;
        for (size_t i = 0; i < 12; i++) {
            sum += grade[i];
        }
        studentGPA[studentIndex] = sum / 12.0;
        ++studentIndex;
    }

    /* Display GPAs */
    for (size_t i = 0; i < studentIndex; i++) {
        printf("Student %zu GPA: %.2f\n", i, studentGPA[i]);
    }

    return 0;
}

注意检查fgets()的返回值,控制文件读取循环;当返回空指针或达到最大学生数时,循环终止。此代码允许超过 10 名学生,但要求每个学生 12 年级。如果一行学生成绩不包含 12 个条目,则程序会打印一条错误消息并退出。这是通过检查来自sscanf() 的返回值来完成的,这是成功转换的次数。读取每个学生的成绩后,计算平均值并将其存储在studentGPAs 的数组中。

按照问题帖子中的建议,我假设了整数成绩条目,但 GPA 是使用浮点值计算的。另请注意,我假设成绩由空格分隔。如果成绩由逗号(或其他分隔符)分隔,则需要相应地修改格式字符串。

一种更优雅的方法是使用strtok() 根据一串分隔符将成绩线分成单独的标记。这些定界符delims = " ,\r\n" 表示strtok() 应该将行分成出现空格、逗号或换行符的标记。如果需要,可以简单地将其他分隔符添加到字符串中。

获取每行成绩,并首次调用strtok()。如果有令牌,则由atoi() 转换为int,并添加到sum。 numGrades 被更新,strtok() 被再次调用以获得新令牌。从一行读取所有成绩后,计算平均值并将其存储在studentGPA 数组中。请注意,平均计算中的分母乘以1.0 以强制计算为double。另请注意,更健壮的代码将使用strtol() 而不是atoi(),检查转换是否有错误。

使用此版本,可以评估不超过 MAX_STUDENTS 的任意数量的学生,并具有不同的成绩。

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

#define MAX_STUDENTS  100

int main(void)
{
    /* Open a file */
    FILE *fPointer = fopen("hello.xls", "r");

    /* Make sure file opened successfully */
    if (fPointer == NULL) {
        fprintf(stderr, "Unable to open file\n");
        exit(EXIT_FAILURE);
    }

    char singleLine[500];
    double studentGPA[MAX_STUDENTS];
    size_t studentIndex = 0;
    char *token;
    char *delims = " ,\r\n";

    /* Loop to fetch data and calculate GPAs */
    while ((fgets(singleLine, sizeof singleLine, fPointer)) != NULL &&
           studentIndex < MAX_STUDENTS) {
        int sum = 0;
        int numGrades = 0;

        /* Get grade tokens */
        token = strtok(singleLine, delims);

        while (token) {
            sum += atoi(token);
            ++numGrades;
            token = strtok(NULL, delims);
        }

        /* Calculate student GPA */
        studentGPA[studentIndex] = sum / (1.0 * numGrades);
        ++studentIndex;
    }

    /* Display GPAs */
    for (size_t i = 0; i < studentIndex; i++) {
        printf("Student %zu GPA: %.2f\n", i, studentGPA[i]);
    }

    return 0;
}

这是一个示例hello.xls 文件,带有混合的逗号和空格分隔符。除非删除逗号,否则第一个程序将无法使用此文件。

3 4 2 3 3 4 3 2 3 3 4 4
2 2 3 4 2 3 3 2 3 2 2 3
1 2 3, 3 2 3 3, 3, 4 2 3 3
3 2 2 3 3 2 1 2 2 2 1 1
4 4 3 4 4 4 4 3 4 4 4 4
4, 3, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4
2 3 2 2 2 1 2 2 2 3 2 2
3 4 4 4 4 4 4 4 4 4 4 4
3 2 3 3 2 3 3 3 2 3 3 3
4 1 4 3 4 4 4 2 4 4 4 4

这是带有这个成绩文件的第二个程序的输出:

Student 0 GPA: 3.17
Student 1 GPA: 2.58
Student 2 GPA: 2.67
Student 3 GPA: 2.00
Student 4 GPA: 3.83
Student 5 GPA: 3.83
Student 6 GPA: 2.08
Student 7 GPA: 3.92
Student 8 GPA: 2.75
Student 9 GPA: 3.50

【讨论】:

    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 2017-06-25
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多