gpt4 book ai didi

C++尝试按平均值对类数组进行排序,然后按递增顺序对它们进行排序

转载 作者:行者123 更新时间:2023-11-28 06:16:58 33 4
gpt4 key购买 nike

这是类里面的一个项目,我被困了几个小时。作业是打开一个文本文件,其中包含学生姓名列表、他们的 ID 号以及他们在不同测验、期中考试和期末考试中的分数,然后计算平均值。我们必须为每个学生创建一个类(class),然后计算每个学生的平均分,并根据谁获得最好的成绩给学生排名。到目前为止,我已经为每个学生创建了一个类(class),找到了平均水平,并将学生的平均水平从高到低进行了排名,但我不知道如何分配排名。如果您想了解更多信息,这里是作业的图片:http://imgur.com/Jetvj7O

这是我的代码:

            #include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <algorithm>
#include <functional>
using namespace std;

int num_a = 0, num_b = 0, num_c = 0, num_d = 0, num_f = 0;

class Student {
private:
int rank;
string name;
int id;
double quiz_scores[5];
double midterm_scores[2];
double final_score;
double average;
double quiz_average;
double midterm_average;
char grade;


public:
Student();
void setRank(int num);
int getRank();
void setGrade(double average);
char getGrade();
void setName(string first);
string getName();
void setId(int num);
int getId();
void setMidterms(double nums[]);
double getMidterms();
void setQuizes(double nums[]);
double getQuizes();
void setFinal(double num);
double getFinal();
void setAverage();
double getAverage();

};
Student::Student()
{
name = "John Doe";
id = 0;
final_score = 0;
average = 0;
grade = 'F';
midterm_scores[2] = {};
quiz_scores[5] = {};
rank = 0;
}
void Student::setRank(int num)
{
rank = num;
}
int Student::getRank()
{
return rank;
}
void Student::setGrade(double average)
{
if (average <= 100 && average >= 90)
{
grade = 'A';
num_a++;
}
else if (average <= 89.99 && average >= 80)
{
grade = 'B';
num_b++;
}
else if (average <= 79.99 && average >= 70)
{
grade = 'C';
num_c++;
}
else if (average <= 69.99 && average >= 60)
{
grade = 'D';
num_d++;
}
else if (average < 60)
grade = 'F';
num_f++;
}
void Student::setName(string first)
{
name = first;
}
string Student::getName()
{
return name;
}
void Student::setId(int num)
{
id = num;
}
int Student::getId()
{
return id;
}
void Student::setMidterms(double nums[])
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);

double average, sum = 0;
for (int i = 0; i < 2; i++)
{
midterm_scores[i] = nums[i];
sum += midterm_scores[i];
}

average = sum / 2;
midterm_average = average * .40;

}
double Student::getMidterms()
{
return midterm_average;
}
void Student::setQuizes(double nums[])
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);

double score, sum = 0, average, min = nums[0];

for (int i = 0; i < 5; i++)
{
if (nums[i] < min)
{
min = nums[i];
}

score = nums[i];
quiz_scores[i] = score;
sum += quiz_scores[i];
}
sum = sum - min;
average = (sum / 40) * 100;
quiz_average = average * .20 ;

}
double Student::getQuizes()
{
return quiz_average;
}
void Student::setAverage()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);

average = (quiz_average + midterm_average + final_score);
}
void Student::setFinal(double num)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);

final_score = num * .40;
}
double Student::getFinal()
{
return final_score;
}
double Student::getAverage()
{
return average;
}

char Student::getGrade()
{
return grade;
}
//ranks the student by decreasing average grade
void rankStudents(double array[], int arraySize, Student scores[])
{
int m; // keep the index of current smallest value
double newArray[30], hold, finalArray[30];
bool isFound = false;
//stores the averages in an array then sorts array
for (int i = 0; i < arraySize; i++)
{
newArray[i] = array[i];
}

for (int k = 0; k <= arraySize - 2; k++)
{
m = k;
for (int j = k + 1; j <= arraySize - 1; j++)
{
if (newArray[j] > newArray[m])
m = j;
}

hold = newArray[m];
newArray[m] = newArray[k];
newArray[k] = hold;
}

//assigns rank to each student by comparing the array of averages to the students average by calling student.getAverage()
for (int i = 0; i < arraySize; i++)
{
double test = newArray[i];
int counter = i;
for (int k = 0; k < arraySize; k++)
{
int counter2 = i;
if (scores[k].getAverage() == test)
{
scores[k].setRank(counter+1);
}
else if (scores[k].getAverage() == scores[k - 1].getAverage() && scores[k].getAverage() == test)
{
scores[k].setRank(i + 1);
scores[k - 1].setRank(scores[k].getRank());
}
//cout << scores[k].getName() << ' ' << scores[k].getId() << ' ' << scores[k].getRank() << endl;
}
}
for (int i = 0; i < arraySize; i++)
{
//cout << scores[i].getName() << ' ' << scores[i].getId() << ' ' << scores[i].getAverage() << ' ' << scores[i].getGrade() << ' ' << scores[i].getRank() << endl;
}
//creates finalArray which stores the students ranks and sorts them
for (int i = 0; i < arraySize; i++)
{
finalArray[i] = scores[i].getRank();
}
for (int k = 0; k <= arraySize - 2; k++)
{
m = k;
for (int j = k + 1; j <= arraySize - 1; j++)
{
if (finalArray[j] < finalArray[m])
m = j;
}

hold = finalArray[m];
finalArray[m] = finalArray[k];
finalArray[k] = hold;
}
for (int i = 0; i < arraySize; i++)
{
//cout << scores[i].getRank() << endl;
}
//prints out the array
for (int i = 0; i < arraySize; i++)
{
double test = newArray[i];
int counter3 = i;
for (int k = 0; k < arraySize; k++)
{
if (scores[k].getAverage() == test)
{
cout << scores[k].getName() << ' ' << scores[k].getId() << ' ' << scores[k].getAverage() << ' ' <<scores[k].getGrade() << ' ' << scores[k].getRank() << endl;
}
if (scores[k].getAverage() == scores[k - 1].getAverage() && scores[k].getAverage() == test)
{
scores[k].setRank(i + 1);
scores[k - 1].setRank(scores[k].getRank());
cout << scores[k].getName() << ' ' << scores[k].getId() << ' ' << scores[k].getAverage() << ' ' << scores[k].getGrade() << ' ' << scores[k].getRank() << endl;
}
}

}


}

void readFile(Student people[])
{
//declares input file
ifstream in_file;
string user_file;
int number_of_students, id;

double quiz1, quiz2, quiz3, quiz4, quiz5, midterm1, midterm2, final_score, quizes[5], midterm[2], averages[30];
string name;
cout << "Enter an input file: ";
cin >> user_file;

in_file.open(user_file);
if (in_file.fail())
{
cout << "Error: file open failed \n" << endl;
exit(1);
}

//finds number of players in the array
in_file >> number_of_students;


//creates an array for scores then stores the id and scores of each player
for (int i = 0; i < number_of_students; i++)
{
in_file >> name >> id >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5 >> midterm1 >> midterm2 >> final_score;
people[i].setName(name);
people[i].setId(id);
quizes[0] = quiz1;
quizes[1] = quiz2;
quizes[2] = quiz3;
quizes[3] = quiz4;
quizes[4] = quiz5;
midterm[0] = midterm1;
midterm[1] = midterm2;
people[i].setQuizes(quizes);
people[i].setMidterms(midterm);
people[i].setFinal(final_score);
//cout << array[i].getName() << " quiz average = " << array[i].getQuizes() << "midterm average = " << array[i].getMidterms() << "final = " << array[i].getFinal() << endl;
people[i].setAverage();
people[i].setGrade(people[i].getAverage());
//cout << array[i].getAverage() << endl;
averages[i] = people[i].getAverage();
//cout << averages[i] << endl;


//cout << "grade = " << array[i].getGrade() << endl;

//calls findAverage function to find average of each array then stores the result in player class



}
rankStudents(averages, number_of_students, people);

for (int i = 0; i < number_of_students; i++)
{
if (people[i].getAverage() == averages[i])
people[i].setRank(i);

}
for (int i = 0; i < number_of_students; i++)
{
cout << people[i].getRank();
}

}
int main()
{

Student students[30];
readFile(students);




return 0;
}

这是我需要帮助的部分:

            void rankStudents(double array[], int arraySize, Student scores[])
{
int m; // keep the index of current smallest value
double newArray[30], hold, finalArray[30];
bool isFound = false;
//stores the averages in an array then sorts array
for (int i = 0; i < arraySize; i++)
{
newArray[i] = array[i];
}

for (int k = 0; k <= arraySize - 2; k++)
{
m = k;
for (int j = k + 1; j <= arraySize - 1; j++)
{
if (newArray[j] > newArray[m])
m = j;
}

hold = newArray[m];
newArray[m] = newArray[k];
newArray[k] = hold;
}

//assigns rank to each student by comparing the array of averages to the students average by calling student.getAverage()
for (int i = 0; i < arraySize; i++)
{
double test = newArray[i];
int counter = i;
for (int k = 0; k < arraySize; k++)
{
int counter2 = i;
if (scores[k].getAverage() == test)
{
scores[k].setRank(counter+1);
}
else if (scores[k].getAverage() == scores[k - 1].getAverage() && scores[k].getAverage() == test)
{
scores[k].setRank(i + 1);
scores[k - 1].setRank(scores[k].getRank());
}
//cout << scores[k].getName() << ' ' << scores[k].getId() << ' ' << scores[k].getRank() << endl;
}
}
for (int i = 0; i < arraySize; i++)
{
//cout << scores[i].getName() << ' ' << scores[i].getId() << ' ' << scores[i].getAverage() << ' ' << scores[i].getGrade() << ' ' << scores[i].getRank() << endl;
}
//creates finalArray which stores the students ranks and sorts them
for (int i = 0; i < arraySize; i++)
{
finalArray[i] = scores[i].getRank();
}
for (int k = 0; k <= arraySize - 2; k++)
{
m = k;
for (int j = k + 1; j <= arraySize - 1; j++)
{
if (finalArray[j] < finalArray[m])
m = j;
}

hold = finalArray[m];
finalArray[m] = finalArray[k];
finalArray[k] = hold;
}
for (int i = 0; i < arraySize; i++)
{
//cout << scores[i].getRank() << endl;
}
//prints out the array
for (int i = 0; i < arraySize; i++)
{
double test = newArray[i];
int counter3 = i;
for (int k = 0; k < arraySize; k++)
{
if (scores[k].getAverage() == test)
{
cout << scores[k].getName() << ' ' << scores[k].getId() << ' ' << scores[k].getAverage() << ' ' <<scores[k].getGrade() << ' ' << scores[k].getRank() << endl;
}
if (scores[k].getAverage() == scores[k - 1].getAverage() && scores[k].getAverage() == test)
{
scores[k].setRank(i + 1);
scores[k - 1].setRank(scores[k].getRank());
cout << scores[k].getName() << ' ' << scores[k].getId() << ' ' << scores[k].getAverage() << ' ' << scores[k].getGrade() << ' ' << scores[k].getRank() << endl;
}
}

}


}

最佳答案

您可以使用 std sort() 函数对分数进行排序,方法是给它一个指向数组开头的迭代器作为第一个参数,并给它一个指向数组开头的迭代器数组的末尾作为第二个参数。更多关于 sort .

然后,您可以遍历排序后的平均分数,并根据递减的基础给出分数。例如,您的最高分将获得排名 1。转到下一个分数。检查分数是否与之前相同。如果是,给出相同的排名,否则,增加你的排名然后分配它。

您的排名函数有点复杂。我建议你把它分成三个部分。获取平均值,排序,然后排名。我会使用 vector 来进一步简化事情。我希望这会有所帮助:)

关于C++尝试按平均值对类数组进行排序,然后按递增顺序对它们进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30067474/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com