题目描述
用链式存储结构实现对一个班级学生信息管理。设计程序求出每个人的平均成绩并按平均成绩由高到底排序后输出学生记录。
输入描述:
人数 n
人员记录1(格式为:学号姓名成绩1成绩2成绩3)人员记录2
输出描述:
人员记录×1人员记录y2
人员记录 z
#include <iostream>
using namespace std;
#include <string>
struct LinkNode {
int s_num;
string s_name;
double s_garde1;
double s_garde2;
double s_garde3;
double jun_zhi;
LinkNode* link;
LinkNode() {
link = NULL;
}
LinkNode(int num, string name, double n1, double n2, double n3) {
s_num = num;
s_name = name;
s_garde1 = n1;
s_garde2 = n2;
s_garde3 = n3;
jun_zhi = (n1 + n2 + n3) / 3;
link = NULL;
}
};
template <class T>
class LineList {
public:
int length;
LinkNode* first;
public:
LineList() {}
LineList(int n) {
length = n;
first = new LinkNode();
LinkNode* current = first;
for (int i = 0; i < length; i++) {
int num;
string name;
double n1, n2, n3;
cin >> num;
cin >> name;
cin >> n1 >> n2 >> n3;
LinkNode* newnode = new LinkNode(num,name,n1,n2,n3);
current->link = newnode;
current = newnode;
}
}
void Sort() {
LinkNode* current = first;
LinkNode* p;
LinkNode* q;
for (p = current->link; p != NULL; p = p->link)
{
for (q = p->link; q != NULL; q = q->link) {
if (p->jun_zhi < q->jun_zhi)
{
int tmp = q->s_num;
q->s_num = p->s_num;
p->s_num = tmp;
string tmp2 = q->s_name;
q->s_name = p->s_name;
p->s_name = tmp2;
double tmp3 = q->s_garde1;
q->s_garde1 = p->s_garde1;
p->s_garde1 = tmp3;
double tmp4 = q->s_garde2;
q->s_garde2 = p->s_garde2;
p->s_garde2 = tmp4;
double tmp5 = q->s_garde3;
q->s_garde3 = p->s_garde3;
p->s_garde3 = tmp5;
double tmp6 = q->jun_zhi;
q->jun_zhi = p->jun_zhi;
p->jun_zhi = tmp6;
}
}
}
}
void Print() {
LinkNode* current = first;
for (int i = 1; i <= length; i++) {
current = current->link;
cout.precision(2);
cout << current->s_num << " " << fixed
<< current->s_name << " " << fixed
<< current->s_garde1 << " " << fixed
<< current->s_garde2 << " " << fixed
<< current->s_garde3 << " " << fixed
<< current->jun_zhi << " " << i << endl;
}
cout << endl;
}
};
int main() {
int n;
cin >> n;
LineList<double> L(n);
L.Sort();
L.Print();
}