C ++使用文件IO和数组的平均分数

时间:2016-04-21 03:03:17

标签: c++ arrays file io

我目前正在开展一个项目,我必须从文件中读取信息:
123456789 77.2 88.3 22 28 35 45 33 35 40 234567890 97.5 90 25 30 38 48 34 35 50 345678901 82.4 77.5 22.5 27 35.5 44 35 33 48 并放入数组并显示。以上信息是学生ID,考试成绩(2)和家庭作业成绩(7)。我认为我遇到的问题是我的代码集试图获得这些分数的平均值:

students[i].hwAve = students[i].hwAve / 7; students[i].testAve = students[i].testAve / 2; students[i].finalScore = (students[i].hwAve * 0.40) + (students[i].testAve * 0.60);

但我并不积极。以下是我的完整代码。任何帮助表示赞赏。谢谢。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <conio.h>

using namespace std;

struct SStudent
{
    int studentID;
    double test[2];
    double hw[7];
    double hwAve = 0;
    double testAve = 0;
    double finalScore;
    char grade;
};

int getInput(SStudent[], int);
void process(SStudent[], int);
void display(const SStudent[], int size);

int main()
{
    int totStudents;
    int i;
    SStudent amount[30];

    totStudents = (amount, 30);
    process(amount, totStudents);
    display(amount, totStudents);

    _getch();
};

int getInput(SStudent students[], int size)
{
    int i = 0;
    ifstream scores;
    scores.open("scores.txt");

    if (!scores)
    {
        cout << "Can't open input file.\n";
        _getch();
    }

    for (i = 0; i < size; i++)
    {
        scores >> students[i].studentID;

        for (int j = 0; j < 2; j++)
            scores >> students[i].test[j];

        for (int k = 0; k < 7; k++)
            scores >> students[i].hw[k];

        if (!scores) break;
    }
    return i;
}

void process(SStudent students[], int size)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < 2; j++)
            students[i].testAve += students[i].test[j];

        for (int k = 0; k < 7; k++)
            students[i].hwAve += students[i].hw[k];

        students[i].hwAve = students[i].hwAve / 265;
        students[i].testAve = students[i].testAve / 200;
        students[i].finalScore = (students[i].hwAve * 0.40) + (students[i].testAve * 0.60);

        if (students[i].finalScore < 60)
            students[i].grade = 'E';

        else if (students[i].finalScore > 60 && students[i].grade <= 69)
            students[i].grade = 'D';

        else if (students[i].finalScore > 69 && students[i].grade <= 79)
            students[i].grade = 'C';

        else if (students[i].finalScore > 79 && students[i].grade <= 89)
            students[i].grade = 'B';

        else
            students[i].grade = 'A';
    }
}

void display(const SStudent students[], int size)
{
    ofstream grades;
    grades.open("grades.txt");
    grades << fixed << showpoint << setprecision(1);
    grades << "STUDENT ID\t\tHW AVE\t\tTEST AVE\t\tFINAL SCORE\t\tGRADE" << endl;
    for (int i = 0; i < size; i++)
        cout << students[i].studentID << "\t" << students[i].hwAve << "\t\t << students[i].testAve << "\t\t" << students[i].finalScore << "\t\t" <<     students[i].grade << endl;


    if (grades)
        cout << "Output file created.";
    else
        cout << "Output file not created.";
}

0 个答案:

没有答案