这是针对课堂上的一个项目,我已经被困了几个小时。分配是打开一个文本文件,其中包含学生姓名列表,他们的ID号码和他们在不同的quizes,midterms和决赛中的分数然后计算平均值。我们必须为每个学生上课,然后计算每个学生的平均分,并根据谁获得最好的成绩给予学生排名。到目前为止,我已经为每个学生创建了一个班级,并找到了平均值,并将学生平均值从最大到最小排名,但我无法弄清楚如何分配排名。如果您需要更多信息,请参阅下面的图片: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;
}
}
}
}
答案 0 :(得分:1)
您可以使用std
sort()
函数对分数进行排序,方法是将数组开头的迭代器作为第一个参数,并将数组末尾的迭代器作为第二个参数。更多关于sort。
然后,您可以迭代排序的平均分数,并根据递减的基础给出分数。例如,您的最高分将被赋予等级1.转到下一个分数。检查分数是否与之前的分数相同。如果是,则给出相同的等级,否则,增加你的等级然后分配它。
您的排名功能有点令人费解。我建议你把它分成三部分。获得平均值,排序,然后排名。我使用向量来进一步促进事情。我希望这会有所帮助:)
答案 1 :(得分:0)
你说你已经完成了最后一部分所做的一切,分配成绩(&#34;任何平均90分或更高分数是&#39; A&#39;,......&#34;)。 / p>
我建议你隔离这部分问题。编写一个得分并返回一个字母的函数。一旦完美运行,您可以将该功能添加到您的代码中并填写每个学生的grade
字段。
函数的签名应该是这样的(除非你把它作为成员函数或其他东西):
char letterGrade(double score)
这足以与之合作吗?