从文本中读取数字并输出平均值

Reading numbers from text and outputting average

本文关键字:输出 平均值 数字 读取 文本      更新时间:2023-10-16

因此,我正在从文件中读取文本(逐行)并输出IDID之后的平均4个等级以及字母等级。因此,任何50级或以上的平均成绩的字母等级都是S,任何低于50级的成绩都是U,2个免修班的成绩是I。(如果超过或等于2,则无S或U)。

假设文件有以下数字:

42 50 51 57 52
48 90 -1 60 -1
40 46 -1 59 45
47 50 -1 49 50

输出应该是这样的:

ID=42 Avg=52.5 Grade=S
ID=48 Excused=2 Grade=I
ID=40 Avg=50.0 Grade=S
ID=47 Avg=49.7 Grade=U

类型的等级数

S    U    I
2    1    1

这是我从我的代码收到的输出

它正在读取所有的数字,但我需要它将第一个数字读取为ID,并将后面的4个数字作为等级。

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    ifstream in;
    ofstream results;
    string filename;
    char grade;
    int S = 0, U = 0, I = 0, i = 0, ID, excused = 0, avg;
    double allscores = 0;
    cout << "Enter the name of the file that has the scores of students: " << endl;
    cin >> filename;
    cout << "Enter the number of scores for each student: " << endl;
    cin >> ID;
    in.open(filename);
    results.open("results.txt");
    if (in)
    {
        while (in >> ID)
        {
            int first = 0
            for (i = 0; i<=4; i++)
            {
                if (first == -1)
                    excused++;
                else
                    allscores += first;
            }
            if (first > 4)
            {
                avg = allscores / (4 - excused);
                if (avg >= 50.0)
                {
                    grade = 'S';
                    S++;
                    cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
                }
                else
                {
                    grade = 'U';
                    U++;
                    cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
                }
            }
            else
            {
                grade = 'I';
                I++;
                cout << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl;
            }
        }
    }
    else
    {
        cout << "Couldn't open filen";
    }
    cout << "Number of Grades of Type" << endl;
    cout << "S   " << "U   " << "I" << endl;
    cout << S << "   " << U << "   " << I << endl;
    in.close();
    results.close();
    system("pause");
    return 0;
}

您的变量int first=0在代码中没有被分配除0以外的任何值,因此语句if(first>4)将始终为false,并导致您获得的输出。

你可以这样做:

    while (in >> ID)//this is supposed to get the ID of each student, in order for that to happen you need to read all the 4 scores inside this loop before coming here and reading the next ID,otherwise everything will read as an ID
    {
       int first = 0
       excused=0;//you need to reset excused on every iteration
       allscores=0;//you need to reset allscore on every iteration
       for (i = 0; i<4; i++)
       {//here you need to get all the 4 scores
           in>>first;//get each score in first
           if (first == -1)
             excused++;
           else
             allscores += first;
       }
       if(excused<2)//instead of if(first>4)
            ...//do your calculation for average score
       else
            ...//set the grade to 'I'...
    }

这是我的最终解决方案:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    ifstream in;
    ofstream results;
    string filename;
    char grade;
    int S = 0, U = 0, I = 0, i = 0, ID, excused = 0, avg;
    double allscores = 0;
    cout << "Enter the name of the file that has the scores of students: " << endl;
    cin >> filename;
    cout << "Enter the number of scores for each student: " << endl;
    cin >> ID;
    in.open(filename);
    results.open("results.txt");
    if (in)
    {
        while (in >> ID)
        {
            int first = 0;
            excused = 0;
            allscores = 0;
            for (i = 0; i < 4; i++)
            {
                in >> first;
                if (first == -1)
                    excused++;
                else
                    allscores += first;
            }
            if (excused < 2)
            {
                avg = allscores / (4 - excused);
                if (avg >= 50.0)
                {
                    grade = 'S';
                    S++;
                    results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
                }
                else
                {
                    grade = 'U';
                    U++;
                    results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
                }
            }
            else
            {
                grade = 'I';
                I++;
                results << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl;
            }
        }
    }
    else
    {
        cout << "Couldn't open filen";
    }
    results << "Number of Grades of Type" << endl;
    results << "S   " << "U   " << "I" << endl;
    results << S << "   " << U << "   " << I << endl;
    in.close();
    results.close();
    system("pause");
    return 0;
}

代码完成后,我将其输出到一个名为"results"的文件中
谢谢你的帮助。我想我的while循环是最大的错误
特别是在CCD_ 9部分中没有添加。