我必须按字母顺序对姓氏进行排序,我一直在寻找如何做到这一点的时间,但我的垃圾书除了整数之外没有提供任何例子,所以任何帮助都会很棒。这是我的代码:
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int getAverage(int,int,int);
string letterGrade(int);
void getInfo();
int main(int argc, char *argv[])
{
getInfo();
system("PAUSE");
return EXIT_SUCCESS;
}
void getInfo()
{
const int info = 5;
string last[info];
string first[info];
int ID[info];
int score1[info];
int score2[info];
int score3[info];
for (int count = 0; count < info; count++)
{
cout << "last name\n";
cin >> last[count];
cout << "first name\n";
cin >> first[count];
cout << "enter ID\n";
cin >> ID[count];
cout << "enter test 1\n";
cin >> score1[count];
cout << "enter test 2\n";
cin >> score2[count];
cout << "enter test 3\n";
cin >> score3[count];
}
cout << endl;
for (int count = 0; count < info; count++)
{
cout << last[count]
<< setw(10) << first[count]
<< setw(10) << ID[count]
<< setw(10) <<score1[count]
<< setw(10) << score2[count]
<< setw(10) << score3[count]
<< setw(10) << getAverage(score1[count],score2[count],score3[count])
<< setw(10) << letterGrade(getAverage(score1[count],score2[count],score3[count]));
cout << endl;
}
}
string letterGrade(int average)
{
string lGrade;
if (average > 89)
lGrade = "A";
else if (average > 79 && average < 90)
lGrade = "B";
else if (average > 69 && average < 80)
lGrade = "C";
else if (average > 59 && average < 70)
lGrade = "D";
else if (average >= 0 && average < 60)
lGrade = "F";
return lGrade;
}
int getAverage(int score1,int score2,int score3)
{
int average;
average = ((score1 + score2 + score3)/ 3);
return average;
}
答案 0 :(得分:2)
我建议使用struct i / o一堆数组和std :: sort(假设你可以这样做)。
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int getAverage(int,int,int);
string letterGrade(int);
void getInfo();
struct gradeInfo {
string last;
string first;
int ID;
int score1;
int score2;
int score3;
};
bool mySort (gradeInfo g1,gradeInfo g2) {
return (g1.last <g2.last);
} //lexicographical string comparison
int main(int argc, char *argv[])
{
getInfo();
system("PAUSE");
return EXIT_SUCCESS;
}
void getInfo()
{
const int info = 5;
vector<gradeInfo> grades;
for (int count = 0; count < info; count++)
{
gradeInfo gradesEntry;
cout << "last name\n";
cin >> gradesEntry.last;
cout << "first name\n";
cin >> gradesEntry.first;
cout << "enter ID\n";
cin >> gradesEntry.ID;
cout << "enter test 1\n";
cin >> gradesEntry.score1;
cout << "enter test 2\n";
cin >> gradesEntry.score2;
cout << "enter test 3\n";
cin >> gradesEntry.score3;
grades.push_back(gradesEntry);
}
cout << endl;
sort(grades.begin(), grades.end(), mySort);
for (int count = 0; count < info; count++)
{
int average = getAverage(grades[count].score1,grades[count].score2,grades[count].score3);
cout << grades[count].last
<< setw(10) << grades[count].first
<< setw(10) << grades[count].ID
<< setw(10) << grades[count].score1
<< setw(10) << grades[count].score2
<< setw(10) << grades[count].score3
<< setw(10) << average
<< setw(10) << letterGrade(average);
cout << endl;
}
}
string letterGrade(int average)
{
string lGrade;
if (average > 89)
lGrade = "A";
else if (average > 79 && average < 90)
lGrade = "B";
else if (average > 69 && average < 80)
lGrade = "C";
else if (average > 59 && average < 70)
lGrade = "D";
else if (average >= 0 && average < 60)
lGrade = "F";
return lGrade;
}
int getAverage(int score1,int score2,int score3)
{
int average;
average = ((score1 + score2 + score3)/ 3);
return average;
}