C++学生类作业:实现自定义排序的学生数据管理程序
Got it, let's work through this C++ programming problem together. Here's a complete solution that meets all your requirements:
1. Define a Student Data Structure
First, we'll create a struct to group each student's details—this makes it easy to handle and organize the related data:
#include <iostream> #include <string> using namespace std; // For simplicity in a small program; you can avoid this in larger projects by qualifying with std:: struct Student { string lastName; string firstName; double gpa; };
2. Read Input from Standard Input
We'll read student entries until we hit the end of input (EOF) or reach the maximum limit of 50 students. Each entry follows the format: lastName firstName GPA:
int readStudents(Student students[], int maxSize) { int count = 0; // Keep reading until we hit max students or no more input while (count < maxSize && cin >> students[count].lastName >> students[count].firstName >> students[count].gpa) { count++; } return count; }
3. Implement a Custom Sort Function
Since we can't use library sorting functions (like std::sort), let's go with a bubble sort—it's straightforward and efficient enough for 50 elements. The sorting priority is:
- First compare
lastNamelexicographically - If last names are identical, compare
firstNamelexicographically
void sortStudents(Student students[], int count) { for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { // Swap if current student's last name comes after the next one if (students[j].lastName > students[j+1].lastName) { Student temp = students[j]; students[j] = students[j+1]; students[j+1] = temp; } // If last names match, compare first names else if (students[j].lastName == students[j+1].lastName) { if (students[j].firstName > students[j+1].firstName) { Student temp = students[j]; students[j] = students[j+1]; students[j+1] = temp; } } } } }
4. Output the Sorted Students
Finally, we'll print each student's details in the sorted order:
void printStudents(Student students[], int count) { for (int i = 0; i < count; i++) { cout << students[i].lastName << " " << students[i].firstName << " " << students[i].gpa << endl; } }
Full Main Function
Putting all pieces together in the main function:
int main() { const int MAX_STUDENTS = 50; Student students[MAX_STUDENTS]; int studentCount = readStudents(students, MAX_STUDENTS); sortStudents(students, studentCount); printStudents(students, studentCount); return 0; }
Testing with Your Sample Input
If you input:
Ware Henry 87.2 Dantes Edmond 91.4 Earhart Amelia 92.6
The program will output:
Dantes Edmond 91.4 Earhart Amelia 92.6 Ware Henry 87.2
Key Notes
- Lexicographical Comparison: We use
std::string's built-in comparison operators, which handle dictionary-style ordering exactly as needed for names. - Input Robustness: The
readStudentsfunction automatically stops when there's no more input or we hit the 50-student limit, so it works with any valid input size under the cap. - Sorting Choice: Bubble sort is perfect here—no need for a more complex algorithm like quicksort when dealing with only 50 elements.
内容的提问来源于stack exchange,提问作者samh30

