该代码用于输出学生名册,并允许按名字、姓氏、年级等对表格进行排序。
我的程序进行了编译,实际上我在最初的几个问题上得到了赞扬,但我的SortByFirstName和SortByLastName函数,或者这些函数与SortByGrade交互的方式似乎有问题。(这些功能在代码的底部,其他一切都应该正常工作(
当试图按姓氏或名字排序时,输出结果几乎是正确的,也就是说,列表似乎排序正确,但中间的两个名字没有排序。坡度值也被不正确地交换(或者根本没有交换?(。
当只是按等级排序时,似乎没有问题,所以我认为错误在于SortByFirstName和SortByLastName函数。
#include <vector>
#include <string>
#include <locale>
#include <iomanip>
using namespace std;
void PrintWelcome();
void GetNumberOfStudents(int &numOfStudents);
void PrintMenu();
char GetUserSelection();
void PrintSummary(const vector<string> &students, const vector<double> &grades);
void AddStudent(vector<string> &students, vector<double> &grades);
void RemoveStudent(vector<string> &students, vector<double> &grades);
void SortByFirstName(vector<string> &students, vector<double> &grades);
void SortByLastName(vector<string> &students, vector<double> &grades);
void SortByGrade(vector<string> &students, vector<double> &grades);
int main() {
const char QUIT = 'q';
const char ADD = 'a';
const char REMOVE = 'r';
const char PRINT = 'p';
const char MENU = 'm';
const char SORTFULL = 'f';
const char SORTLAST = 'l';
const char SORTGRADE = 'g';
string thankYou = "Thank you for entering your students information!n";
string notValid = "Not a valid selection.n";
char selection;
int numOfStudents;
vector<string> students;
vector<double> grades;
//Print the Welcome Message
PrintWelcome();
//Get Number of Students
GetNumberOfStudents(numOfStudents);
//Add the total number of students to the student and grades vectors
for(int i = 0; i <numOfStudents; i++){
AddStudent(students, grades);
}
//Print thank you message
cout << thankYou;
//Print the Roster Menu
PrintMenu();
//Get the users selection
selection = GetUserSelection();
while(selection != QUIT){
if(selection == ADD){
AddStudent(students, grades);
}
else if(selection == REMOVE){
RemoveStudent(students, grades);
}
else if(selection == PRINT){
PrintSummary(students, grades);
}
else if(selection == MENU){
PrintMenu();
}
/*Provide Implementation for other menu options*/
else if (selection == SORTFULL){
SortByFirstName(students, grades);
}
else if (selection == SORTLAST){
SortByLastName(students, grades);
}
else if (selection == SORTGRADE){
SortByGrade(students, grades);
}
else{
cout << notValid;
}
selection = GetUserSelection();
}
}
void PrintWelcome(){
string welcome = "Welcome to the student roster!n";
cout << welcome;
}
void GetNumberOfStudents(int &numOfStudents){
string numOfStudentsQuestion = "How many students are in your class?:n";
cout << numOfStudentsQuestion;
cin >> numOfStudents;
}
void PrintMenu(){
string menu = "Please choose one of the following options:n"
"a: add a studentn"
"r: remove a studentn"
"p: print the class summaryn"
"m: print menun"
"f: sort - first namen"
"l: sort - last namen"
"g: sort - graden"
"q: quit programn";
cout << menu;
}
char GetUserSelection(){
string selectionString = "selection:n";
char selection;
cout << selectionString;
cin >> selection;
return selection;
}
void PrintSummary(const vector<string> &students, const vector<double> &grades){
string summaryHeading = "Class Summaryn"
"------------------------n";
string nameGradeHeading = "Name Graden"
"--------- --------n";
string numOfStudentsString = "Number of Students:n"
"-------------------n";
string averageGradeString = "Average Grade:n"
"--------------n";
double sum = 0.0;
double average = 0.0;
int numOfStudents = students.size();
cout << endl;
cout << summaryHeading << nameGradeHeading;
for(unsigned int i = 0; i < students.size(); i++){
sum += grades.at(i);
cout << left << setw(20) << students.at(i) << setprecision(2) << fixed << grades.at(i) << endl;
}
cout << numOfStudentsString << numOfStudents << endl;
cout << averageGradeString << setprecision(2) << fixed << sum/numOfStudents << endl;
cout << endl;
}
void AddStudent(vector<string> &students, vector<double> &grades){
string studentInfo = "Please enter student (First Last Grade) info:n";
string firstName, lastName;
double grade;
cout << studentInfo;
cin >> firstName >> lastName >> grade;
students.push_back(firstName + " " + lastName);
grades.push_back(grade);
}
void RemoveStudent(vector<string> &students, vector<double> &grades){
string removeStudent = "Please enter students first and last name";
string firstName, lastName;
cout << removeStudent;
cin >> firstName >> lastName;
string fullName = firstName + " " + lastName;
for(unsigned int i = 0; i < students.size(); i++){
if(students.at(i) == fullName) {
students.erase(students.begin() + i);
grades.erase(grades.begin() + i);
cout << "Removing: " << fullName;
}
}
}
void SortByFirstName(vector<string> &students, vector<double> &grades){
string firstName1, firstName2;
string temp;
int pos;
double tempGrade;
for(unsigned int i=0; i<students.size();i++){
pos=students[i].find(' ');
firstName1=students[i].substr(0,pos);
for(unsigned int j=i+1;j<students.size();j++){
pos=students[j].find(' ');
firstName2=students[j].substr(0,pos);
if(firstName1>firstName2){
temp=students[i];
students[i]=students[j];
students[j]=temp;
tempGrade=grades[i];
grades[i]=grades[j];
grades[j]=tempGrade;
}
}
}
}
void SortByLastName(vector<string> &students, vector<double> &grades){
string lastName1, lastName2;
string temp;
int pos;
double tempGrade;
for(unsigned int i=0; i<students.size();i++){
pos=students[i].find(' ');
lastName1=students[i].substr(pos+1);
for(unsigned int j=i+1;j<students.size();j++){
pos=students[j].find(' ');
lastName2=students[j].substr(pos+1);
if(lastName1>lastName2){
temp=students[i];
students[i]=students[j];
students[j]=temp;
tempGrade=grades[i];
grades[i]=grades[j];
grades[j]=tempGrade;
}
}
}
}
void SortByGrade(vector<string> &students, vector<double> &grades){
string temp;
double tempGrade;
for(unsigned int i=0;i<students.size();i++){
for(unsigned int j=i+1;j<students.size();j++){
if(grades[i]>grades[j]){
temp=students[i];
students[i]=students[j];
students[j]=temp;
tempGrade=grades[i];
grades[i]=grades[j];
grades[j]=tempGrade;
}
}
}
}
以下是我当前代码(针对SortByFirstName(的错误输出示例:
预期:||发现:
Alyx Masinas下面是另一个(用于SortByFirstName+SortByGrade(:
预期:||发现:
Alyx Masinas预期:||发现:
- 83.5||83.5
- 98.7 ||100
- 100 ||98.7
- 89.5||89.5
- 100 ||100
为什么我的函数排序不正确?在最终将其全部编译后,我不知道该去哪里。在我的笔记中,我一直在寻找类似的练习问题,其中向量在函数调用过程中没有完全选择。任何帮助都将不胜感激,谢谢。
在排序函数的内部循环中,您没有考虑到当您交换两个学生时(例如,当firstName2
移动到以前由firstName1
占用的插槽中时(,您现在需要将后续元素与新的最小已知条目(本例中为firstName2
(进行比较。
增加一行:
if (firstName1 > firstName2) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
tempGrade = grades[i];
grades[i] = grades[j];
grades[j] = tempGrade;
// now only look for items that should come before our new "leader"
firstName1 = firstName2;
}