Помощ със задача по програмиране на C++

0 гласа
103 прегледа
попитан 21 юни в PHP от FewGod (120 точки)

Здравейте!
Дадоха ми задача по програмиране и не мога да я направя. Бих желал някой да ми помогне.
Ето я и нея:
Да се въведат данните за оценките на учениците от даден клас по 10 предмета. Данните се съхраняват в двумерен масив.
- да се намери средния успех на класа по предмети
- да се изведат предметите с най-нисък и най-висок среден успех
- да се изведе броя на учениците, които имат поправителни изпити за всеки предмет
- да се изведат предметите, по които има поправителни изпити
- да се дефинират необходимите процедури и финкции за въвеждане, обработка и извеждане на информацията.

1 отговор

+1 глас
отговорени 23 юни от Павката (3,400 точки)
редактиран 24 юни от Павката
Поради ограничението на сайта за изпращане до 8000 символа изпращам решението на 3 съобщения. Първото тук съдържа само хедъра, а следващите 2 са част от сорса. За тестване на решението е необходимо единствено да бъде извикан метода run() на описания тук клас:

#ifndef SCHOOLSTATS_H_
#define SCHOOLSTATS_H_

#include <string>

#define MAX_STUDENT_COUNT 25

using namespace std;

namespace school_stats {

class SchoolStats {
private:
    typedef enum Subject_e {
        ESubject_Undefined = 0,
        ESubject_S1,
        ESubject_S2,
        ESubject_S3,
        ESubject_S4,
        ESubject_S5,
        ESubject_S6,
        ESubject_S7,
        ESubject_S8,
        ESubject_S9,
        ESubject_S10,
        ESubject_Max,
    } Subject_t;

    unsigned int student_amount;
    string studentNames[MAX_STUDENT_COUNT];
    string subjectNames[ESubject_Max];
    unsigned int tableClass[MAX_STUDENT_COUNT][ESubject_Max];

    typedef enum program_state_e {
        EStateUndefined = 0,
        EReadStudentsCountData,
        EReadStudentsNamesData,
        EReadSubjectNamesData,
        EReadStudentsGradesData,
        EFindAverageClassGrade,
        EFindMinAndMaxAvgGradeSubjects,
        EFindStudentsRepeatingExams,
        EFindSubjectsWithRepeaters,
        EStateMax
    } program_state_t;

    program_state_t state;

    void printMenu();
    program_state_t getCurrentState();
    string stateToString(program_state_t _state);

    void readUserStateRequest();
    void processCurrentState();

    void dumpCurrentData();

public:
    SchoolStats();
    virtual ~SchoolStats();

    void run();

};

} /* namespace school_stats */

#endif /* SCHOOLSTATS_H_ */
коментиран 23 юни от Павката (3,400 точки)
/*
 * SchoolStats.cpp
 */

#include <iostream>
#include <sstream>
#include <iomanip>

#include "SchoolStats.h"

namespace school_stats {

#define _ALLOW_HARDCODED_VALUES_ 1

SchoolStats::SchoolStats() : student_amount(MAX_STUDENT_COUNT) {
    // TODO Auto-generated constructor stub
    state = EStateUndefined;
#if _ALLOW_HARDCODED_VALUES_==1
//unsigned int SchoolStats::student_amount = 3;
//    string studentNames[MAX_STUDENT_COUNT];
//    string subjectNames[ESubject_Max];
    student_amount = 5;
    for (unsigned int index_stud = 0; index_stud < MAX_STUDENT_COUNT; index_stud++)
    {
        switch (index_stud) {
        case 0:
            studentNames[index_stud] = "Ann";
            break;
        case 1:
            studentNames[index_stud] = "Bonnie";
            break;
        case 2:
            studentNames[index_stud] = "Cat";
            break;
        case 3:
            studentNames[index_stud] = "Denis";
            break;
        case 4:
            studentNames[index_stud] = "Elena";
            break;
        default:
            studentNames[index_stud] = "";
        }
    }

    for (unsigned int index_subj = 0; index_subj < ESubject_Max; index_subj++)
    {
        switch (index_subj) {
        case 0:
        {
            subjectNames[index_subj] = "Maths";
            break;
        }
        case 1:
        {
            subjectNames[index_subj] = "Literature";
            break;
        }
        case 2:
        {
            subjectNames[index_subj] = "Chemistry";
            break;
        }
        case 3:
        {
            subjectNames[index_subj] = "Physics";
            break;
        }
        case 4:
        {
            subjectNames[index_subj] = "Biology";
            break;
        }
        case 5:
        {
            subjectNames[index_subj] = "Bulgarian";
            break;
        }
        case 6:
        {
            subjectNames[index_subj] = "English";
            break;
        }
        case 7:
        {
            subjectNames[index_subj] = "German";
            break;
        }
        case 8:
        {
            subjectNames[index_subj] = "Arts";
            break;
        }
        case 9:
        {
            subjectNames[index_subj] = "Sports";
            break;
        }
        default:
            subjectNames[index_subj] = "Music";
        }
    }

    for (unsigned int indexStud = 0; indexStud < MAX_STUDENT_COUNT; indexStud++)
    {
        unsigned int defaultGrade = 2;
        if (indexStud < student_amount)
        {
            if (indexStud / 3 == 0)
            {
                defaultGrade = 2;
            }
            else if (indexStud / 3 == 1)
            {
                defaultGrade = 3;
            }
            else {
                defaultGrade = 4;
            }
        }

        for (unsigned int indexSubj = 0; indexSubj < ESubject_Max; indexSubj++)
        {

            if (indexStud < student_amount)
            {
                if (indexSubj / 3 == 0)
                {
                    tableClass[indexStud][indexSubj] = defaultGrade;
                }
                else if (indexSubj / 3 == 1)
                {
                    tableClass[indexStud][indexSubj] = defaultGrade + 1;
                }
                else
                {
                    tableClass[indexStud][indexSubj] = defaultGrade + 2;
                }
            }
            else
            {
                tableClass[indexStud][indexSubj] = 0;
            }
        }
    }
//    tableClass = {
//               {3, 4, 3, 4, 3, 4, 3, 4, 3, 4} ,   /*  initializers for row indexed by 0 */
//               {4, 5, 4, 5, 4, 5, 4, 5, 4, 5} ,   /*  initializers for row indexed by 1 */
//               {5, 6, 5, 6, 5, 6, 5, 6, 5, 6}     /*  initializers for row indexed by 2 */
//    };
#endif
}

SchoolStats::~SchoolStats() {
    // TODO Auto-generated destructor stub
}

void SchoolStats::printMenu()
{
    cout <<endl<<"------------------------------------"<<endl;
    cout <<"Enter 0 for exit"<<endl;
    cout <<"Enter 1 for entering the students count"<<endl;
    cout <<"Enter 2 for entering students names"<<endl;
    cout <<"Enter 3 for entering subjects names"<<endl;
    cout <<"Enter 4 for entering students grades"<<endl;
    cout <<"Enter 5 to find the average grade for the class"<<endl;
    cout <<"Enter 6 to find Subjects with minimum and maximum avarage grade"<<endl;
    cout <<"Enter 7 to find list of students to repeat exams"<<endl;
    cout <<"Enter 8 to find list of subjects that will be repeated by students"<<endl;
    cout <<endl<<"------------------------------------"<<endl;
}

SchoolStats::program_state_t SchoolStats::getCurrentState()
{
    return state;
}

string SchoolStats::stateToString(const program_state_t _state)
{
    string result;
    switch (_state)
    {
    case EStateUndefined: result = "State not Undefined"; break;
    case EReadStudentsCountData: result = "Enter the students count"; break;
    case EReadStudentsNamesData: result = "Enter students names"; break;
    case EReadSubjectNamesData: result = "Enter students names"; break;
    case EReadStudentsGradesData: result = "Enter students grades"; break;
    case EFindAverageClassGrade: result = "Find the average grade for the class"; break;
    case EFindMinAndMaxAvgGradeSubjects: result = "Find Subjects with minimum and maximum avarage grade"; break;
    case EFindStudentsRepeatingExams: result = "Find list of students to repeat exams"; break;
    case EFindSubjectsWithRepeaters: result = "Find list of subjects that will be repeated by students"; break;
//    case ESubject_Max:
    default: result = "Error"; break;
    }

    return result;
}

void SchoolStats::readUserStateRequest()
{
    string student_amount_str;

    cin >>student_amount_str;
    istringstream buffer(student_amount_str);
    unsigned int student_amount;

    buffer >> student_amount;
    state = static_cast<program_state_t>(student_amount);
}


} /* namespace school_stats */
коментиран 24 юни от Павката (3,400 точки)
void SchoolStats::processCurrentState()
{
    program_state_t next_state = EStateUndefined;
    string student_amount_str ="";
    unsigned int index = 0;

    cout <<"Current state : "<<stateToString(getCurrentState());
    switch (state)
    {
    case EReadStudentsCountData:
    {
        cout <<"Entry : "<<endl;
        cin >>student_amount_str;
        istringstream buffer(student_amount_str);
        buffer >> student_amount;
        break;
    }
    case EReadStudentsNamesData:
    {
        for (index = 0; index < student_amount; index++)
        {
            cout <<"Type the name of student number "<<index<<" : "<<endl;
            if (index < MAX_STUDENT_COUNT) cin >>studentNames[index];
        }

        cout <<"Total "<<index<<" names stored in diary"<<endl;
        break;
    }
    case EReadSubjectNamesData:
    {
        for (index = 0; index < ESubject_Max; index++)
        {
            cout <<"Type the name of subject number "<<index<<" : "<<endl;
            if (index < ESubject_Max) cin >>subjectNames[ESubject_Max];
        }

        cout <<"Total "<<index<<" subjects stored in diary"<<endl;
        break;
    }
    case EReadStudentsGradesData:
    {
        for (unsigned int indexStud = 0; indexStud < ESubject_Max; indexStud++)
        {
            for (unsigned int indexSubj = 0; indexSubj < student_amount; indexSubj++)
            {
                cout <<"Type the grade of "<<studentNames[indexStud]
                        <<" in "<<subjectNames[indexSubj]<<endl;
            }
        }

        break;
    }
    case EFindAverageClassGrade:
    {
        cout <<"Calculate average grade by subject "<<endl;
        cout <<"========================================================================"<<endl;
        for (unsigned int indexSubj = 0; indexSubj < ESubject_Max; indexSubj++)
        {
            unsigned int sum = 0;
            cout <<"Subject :"<< subjectNames[indexSubj]<<endl;
            cout <<"============================================"<<endl;
            for (unsigned int indexStud = 0; indexStud < student_amount; indexStud++)
            {
                sum += tableClass[indexStud][indexSubj];

                cout <<indexStud<<" : "<<studentNames[indexStud]<<" . "
                        <<indexSubj<<" : "<<subjectNames[indexSubj]<< " = "
                        <<tableClass[indexStud][indexSubj]<<" ; "
                        <<" sum = "<<sum<<endl;
            }

            float fresult = 0.0;
            fresult = static_cast<float>(sum) / static_cast<float>(student_amount);
            cout <<" Average grade for the class : "<<fresult<<endl;
        }
        break;
    }
    case EFindMinAndMaxAvgGradeSubjects:
    {
        unsigned int sums[ESubject_Max] = {0};
        unsigned int min_subject_index = 0;
        unsigned int max_subject_index = 0;
        unsigned int min_sum = 6*MAX_STUDENT_COUNT;
        unsigned int max_sum = 0;

        cout <<"Calculate min and max subjects by average grade"<<endl;
        cout <<"========================================================================"<<endl;
        for (unsigned int indexSubj = 0; indexSubj < ESubject_Max; indexSubj++)
        {
            for (unsigned int indexStud = 0; indexStud < student_amount; indexStud++)
            {
                sums[indexSubj] += tableClass[indexStud][indexSubj];
                cout <<indexStud<<" : "<<studentNames[indexStud]<<" . "
                        <<indexSubj<<" : "<<subjectNames[indexSubj]<< " = "
                        <<tableClass[indexStud][indexSubj]<<" ; "
                        <<" sum = "<<sums[indexSubj]<<endl;
            }

            cout <<indexSubj<<" : "<<subjectNames[indexSubj]
                    <<" sum = "<<sums[indexSubj]<<endl;

            if (sums[indexSubj] > max_sum)
            {
                max_sum = sums[indexSubj];
                max_subject_index = indexSubj;
                cout <<"New max sum = "
                        <<sums[indexSubj]<<" for the subject :"
                        <<indexSubj<<" : "<<subjectNames[indexSubj];
            }

            if (sums[indexSubj] < min_sum)
            {
                min_sum = sums[indexSubj];
                min_subject_index = indexSubj;
                cout <<"New max sum = "
                        <<sums[indexSubj]<<" for the subject :"
                        <<indexSubj<<" : "<<subjectNames[indexSubj];
            }
        }

        float fresult = 0.0;
        fresult = static_cast<float>(min_sum) / static_cast<float>(student_amount);
        cout <<" Subject with minimum grade for the class : "
                <<subjectNames[min_subject_index]<<" : Avg grade : "<<fresult<<endl;
        fresult = static_cast<float>(max_sum) / static_cast<float>(student_amount);
        cout <<" Subject with maximum grade for the class : "
                <<subjectNames[max_subject_index]<<" : Avg grade : "<<fresult<<endl;
        break;
    }
    case EFindStudentsRepeatingExams:
    {
        cout <<"Print list of the students repeating exams"<<endl;
        cout <<"=========================================="<<endl;

        for (unsigned int indexStud = 0; indexStud < student_amount; indexStud++)
        {
            for (unsigned int indexSubj = 0; indexSubj < ESubject_Max; indexSubj++)
            {
                if (2 == tableClass[indexStud][indexSubj])
                {
                    cout <<studentNames[indexStud]<<" ; ";
                    break;
                }
            }
        }
        break;
    }
    case EFindSubjectsWithRepeaters:
    {
        cout <<"Print list of the subjects that will be repeated by some of the students"<<endl;
        cout <<"========================================================================"<<endl;
        for (unsigned int indexSubj = 0; indexSubj < ESubject_Max; indexSubj++)
        {
            for (unsigned int indexStud = 0; indexStud < student_amount; indexStud++)
            {
                if (2 == tableClass[indexStud][indexSubj])
                {
                    cout <<subjectNames[indexSubj]<<" ; ";
                    break;
                }
            }
        }
        break;
    }

    default:
        {
                next_state = EStateUndefined;
        }
    }

    state = next_state;
}

void SchoolStats::dumpCurrentData()
{
    cout <<"Names:"<<endl;
    cout <<"======"<<endl;

    for (unsigned int indexStud = 0; indexStud < student_amount; indexStud++)
    {
        cout <<studentNames[indexStud]<<endl;
    }

    cout <<"Subjects:"<<endl;
    cout <<"========="<<endl;
    for (unsigned int indexSubj = 0; indexSubj < ESubject_Max; indexSubj++)
    {
        cout <<subjectNames[indexSubj]<<endl;
    }

    cout <<"Grades:"<<endl;
    cout <<"========="<<endl;

    for (unsigned int indexStud = 0; indexStud < student_amount; indexStud++)
    {
        cout <<studentNames[indexStud]<<" : ";
        for (unsigned int indexSubj = 0; indexSubj < ESubject_Max; indexSubj++)
        {
            cout <<subjectNames[indexSubj]<<" : "
                    <<tableClass[indexStud][indexSubj]<<";";
        }

        cout <<endl;
    }
}

void SchoolStats::run()
{
//    cout <<"Trace"<<__PRETTY_FUNCTION__<<endl;
    string user_input;
    do
    {
        printMenu();
        cout <<"Current state is"<<stateToString(getCurrentState())<<endl;
        readUserStateRequest();
        processCurrentState();
//        dumpCurrentData();
    } while (user_input.length()<1);
}
...