一道c++编程题,求指导错在哪了

2025年03月01日 09:40
有2个网友回答
网友(1):

#include 
#include  
using namespace std;
struct Student {
static int _number;
int _math;
int _chinese;
int _english;
int _physics;
int _chemistry;
int _total;
double _average;
Student *p_next;

Student () { _number ++; }
}; /* --- end --- */
int Student::_number = 0; // count student number
Student *add_Student ( Student *p_list ) {
Student *p_student = new Student;

p_student -> _math = 0;
p_student -> _chinese = 0;
p_student -> _english = 0;
p_student -> _physics = 0;
p_student -> _chemistry = 0;
p_student -> _total = 0;
p_student -> _average = 0.0;
p_student -> p_next = p_list;
return p_student;
} /* --- end --- */
void input_Scores ( Student *p_student ) {
cout << "Please input the scores for student[" << p_student->_number << "]\n";

// input scores of each subject 
cout << "Math:\t";
cin >> p_student -> _math;

cout << "Chinese:\t";
cin >> p_student -> _chinese;

cout << "English:\t";
cin >> p_student -> _english;

cout << "Physics:\t";
cin >> p_student -> _physics;

cout << "Chemistry:\t";
cin >> p_student -> _chemistry;

// calculate
p_student->_total = p_student->_math + p_student->_chinese + p_student->_english
                                         + p_student->_physics + p_student->_chemistry;
p_student->_average = (double)p_student->_total / 5.0;
} /* --- end --- */
void output_Scores ( Student *p_student ) {
Student *p_list = p_student;

cout << setw(4+4) << "MATH";
cout << setw(7+4) << "CHINESE";
cout << setw(7+4) << "ENGLISH";
cout << setw(7+4) << "PHYSICS"; 
cout << setw(9+4) << "CHEMISTRY";
cout << setw(5+4) << "TOTAL";
cout << setw(7+4) << "AVERAGE\n";
while ( p_list != nullptr ) {
cout << setw(4+4) << p_list->_math;
cout << setw(7+4) << p_list->_chinese;
cout << setw(7+4) << p_list->_english;
cout << setw(7+4) << p_list->_physics;
cout << setw(9+4) << p_list->_chemistry;
cout << setw(5+4) << p_list->_total;
cout << setw(7+4) << p_list->_average;
cout << endl;
p_list = p_list->p_next;
}
} /* --- end --- */
void sort_Scores ( Student *p_student, const int size ) {
int i = 0;
Student *p_list = p_student;
double classroom[size][7];

// transfer scores to external matrix
while ( p_list != nullptr ) {
classroom[i][0] = p_list->_math;
classroom[i][1] = p_list->_chinese;
classroom[i][2] = p_list->_english;
classroom[i][3] = p_list->_physics;
classroom[i][4] = p_list->_chemistry;
classroom[i][5] = p_list->_total;
classroom[i][6] = p_list->_average;
p_list = p_list->p_next;
++i; 
}

for ( int i=0; i int index_of_biggest = i;
// find the smallest index of sub-matrix[][5]
for ( int j=i+1; j if ( classroom[j][5] > classroom[index_of_biggest][5] ) {
index_of_biggest = j;
}
}

if ( i != index_of_biggest ) {
// create a temporary array
int temp[7];

// swap matrix[i][] and matrix[index_of_smallest][]
for ( int k=0; k<7; k++ ) {
temp[k] = classroom[i][k];
classroom[i][k] = classroom[index_of_biggest][k];
classroom[index_of_biggest][k] = temp[k];
}
}
} /* -- end -- */

// display the sorted scores
cout << '\n' << "Sorted Scores\n";
cout << setw(4+4) << "MATH";
cout << setw(7+4) << "CHINESE";
cout << setw(7+4) << "ENGLISH";
cout << setw(7+4) << "PHYSICS"; 
cout << setw(9+4) << "CHEMISTRY";
cout << setw(5+4) << "TOTAL";
cout << setw(7+4) << "AVERAGE\n";

for ( int i=0; i cout << setw(4+4) << classroom[i][0];
cout << setw(7+4) << classroom[i][1];
cout << setw(7+4) << classroom[i][2];
cout << setw(7+4) << classroom[i][3];
cout << setw(9+4) << classroom[i][4];
cout << setw(5+4) << classroom[i][5];
cout << setw(7+4) << classroom[i][6];
cout << endl;
}
} /* --- end --- */
int main(int argc, char *argv[]) {
char is_continue = 'Y';
cout << "Press 'Y' to start or 'N' to end\n";
cin >> is_continue;

// initialize Student data structure
Student *p_student = nullptr;
while ( is_continue == 'Y') {
p_student = add_Student( p_student );
input_Scores( p_student );
cout << "Press 'Y' to continue or 'N' to end\n";
cin >> is_continue;
}

output_Scores( p_student );

sort_Scores( p_student, Student::_number );
return 0;
} /* --- end --- */

网友(2):

报什么错。断点调试。这么一大行代码,谁也不想挨个看一遍