多数组字母/降序排序及学生信息管理程序开发技术问询
Alright, let's walk through building this student record management program with all the requirements you've outlined. I'll break this down into clear, actionable parts with code examples (using C since it's common for array-based student record tasks):
Core Requirements Recap
First, let's formalize what we need to build:
- Collect student details: Last Name (LN), First Name (FN), Middle Initial (MI), Address (ADD), and 4 quarterly grades (FIR, SEC, THR, FTH)
- Calculate each student's average grade
- Support two sorting modes for displaying records:
- Alphabetical order by last name
- Descending order by average grade
- After showing full records, clear the screen and display only student names
- Support up to 90 students total; use 3 students for initial testing
- Implement synchronized multi-array sorting (critical to keep each student's data linked during sorting)
Implementation Approach
1. Data Structure Setup
You can use either separate arrays (per the multi-array requirement) or a struct (cleaner, easier to maintain). Below are both options:
Option 1: Separate Multi-Arrays
#define MAX_STUDENTS 90 #define TEST_STUDENTS 3 // Arrays to store each student's data char lastName[MAX_STUDENTS][50]; char firstName[MAX_STUDENTS][50]; char middleInitial[MAX_STUDENTS][3]; char address[MAX_STUDENTS][100]; float grades[MAX_STUDENTS][4]; float averages[MAX_STUDENTS];
Option 2: Struct (Recommended for Maintainability)
This avoids repetitive swap code during sorting:
typedef struct { char lastName[50]; char firstName[50]; char middleInitial[3]; char address[100]; float grades[4]; float average; } Student; Student students[MAX_STUDENTS];
2. Collect Student Data
Write a function to gather input and calculate averages:
void collectStudentData(int count) { for (int i = 0; i < count; i++) { printf("--- Enter Details for Student %d ---\n", i+1); printf("Last Name: "); scanf("%s", lastName[i]); printf("First Name: "); scanf("%s", firstName[i]); printf("Middle Initial: "); scanf("%s", middleInitial[i]); // Handle address with spaces printf("Address: "); getchar(); // Clear leftover newline from scanf fgets(address[i], sizeof(address[i]), stdin); address[i][strcspn(address[i], "\n")] = '\0'; // Remove trailing newline // Collect grades and calculate average float gradeSum = 0; printf("Enter 4 quarterly grades (separated by spaces): "); for (int j = 0; j < 4; j++) { scanf("%f", &grades[i][j]); gradeSum += grades[i][j]; } averages[i] = gradeSum / 4.0; } }
3. Synchronized Sorting Functions
Sort by Last Name (A-Z)
For multi-arrays, we need to swap every corresponding field to keep data linked:
void sortByLastName(int count) { for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { // Compare last names if (strcmp(lastName[i], lastName[j]) > 0) { // Swap last names char tempLN[50]; strcpy(tempLN, lastName[i]); strcpy(lastName[i], lastName[j]); strcpy(lastName[j], tempLN); // Repeat swap for first name, middle initial, address, grades, and average // (Copy-paste similar code for each array field) char tempFN[50]; strcpy(tempFN, firstName[i]); strcpy(firstName[i], firstName[j]); strcpy(firstName[j], tempFN); char tempMI[3]; strcpy(tempMI, middleInitial[i]); strcpy(middleInitial[i], middleInitial[j]); strcpy(middleInitial[j], tempMI); char tempADD[100]; strcpy(tempADD, address[i]); strcpy(address[i], address[j]); strcpy(address[j], tempADD); float tempGrades[4]; memcpy(tempGrades, grades[i], sizeof(tempGrades)); memcpy(grades[i], grades[j], sizeof(grades[i])); memcpy(grades[j], tempGrades, sizeof(tempGrades)); float tempAvg = averages[i]; averages[i] = averages[j]; averages[j] = tempAvg; } } } }
Sort by Average Grade (Descending)
Same structure as above, just change the comparison condition:
void sortByAverageDesc(int count) { for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { if (averages[i] < averages[j]) { // Swap all corresponding fields (same code as sortByLastName) // ... } } } }
Note: If using a struct, you only need to swap entire Student instances instead of each field individually—way less code!
4. Display Functions
Show Full Student Records
void displayFullRecords(int count) { printf("\n=== All Student Records ===\n"); for (int i = 0; i < count; i++) { printf("\nStudent %d:\n", i+1); printf("Name: %s %s %s\n", firstName[i], middleInitial[i], lastName[i]); printf("Address: %s\n", address[i]); printf("Quarter Grades: %.2f | %.2f | %.2f | %.2f\n", grades[i][0], grades[i][1], grades[i][2], grades[i][3]); printf("Average Grade: %.2f\n", averages[i]); } }
Clear Screen & Show Only Names
void displayOnlyNames(int count) { // Cross-platform clear screen: use "clear" for Linux/macOS, "cls" for Windows #ifdef _WIN32 system("cls"); #else system("clear"); #endif printf("\n=== Student Names ===\n"); for (int i = 0; i < count; i++) { printf("%s %s %s\n", firstName[i], middleInitial[i], lastName[i]); } }
5. Main Program Flow
int main() { int studentCount = TEST_STUDENTS; // Use 3 for testing; switch to MAX_STUDENTS later // Collect data collectStudentData(studentCount); // Let user choose sorting mode int sortChoice; printf("\nChoose Sorting Mode:\n1. Alphabetical by Last Name\n2. Descending by Average Grade\nEnter choice: "); scanf("%d", &sortChoice); switch(sortChoice) { case 1: sortByLastName(studentCount); break; case 2: sortByAverageDesc(studentCount); break; default: printf("Invalid choice—sorting by last name by default.\n"); sortByLastName(studentCount); } // Show full records displayFullRecords(studentCount); // Wait for user input before clearing screen printf("\nPress Enter to clear screen and show only names..."); getchar(); getchar(); // Clear leftover newline, then wait for enter // Show only names displayOnlyNames(studentCount); return 0; }
Key Tips
- Synchronized Sorting: Always swap all related data fields when sorting multi-arrays—this ensures no student's grades or address get mixed up with another student's name.
- Input Handling: Use
fgetsfor addresses to capture spaces, and clear the input buffer withgetchar()to avoid unexpected behavior from leftover newlines. - Scalability: The
MAX_STUDENTSmacro makes it easy to switch from 3 test students to 90 later.
内容的提问来源于stack exchange,提问作者M. Salipuran




