要求使用C++建立一个学生信息管理系统,具有创建、显示、查找、删除、插入功能。
信息包括学号、姓名及性别(使用顺序表),还有每位学生选修课程及成绩(使用单链表,每个结点的数据域包括课程名与成绩,每位学生课程不完全相同),并计算平均分、排序。该如何实现...
信息包括学号、姓名及性别(使用顺序表),还有每位学生选修课程及成绩(使用单链表,每个结点的数据域包括课程名与成绩,每位学生课程不完全相同),并计算平均分、排序。该如何实现?
展开
展开全部
#include <iostream>
struct subject {
double subject_score;
std::string subject_name;
};
class student {
std::string name;
subject* subject_info;
unsigned int subject_num;
public:
student() {
name = "unknown";
subject_num = 0;
subject_info = nullptr;
}
student( std::string name, unsigned int subject_number ): name(name), subject_num(subject_number) {
subject_info = new subject [subject_num];
for ( unsigned int i=0; i<subject_num; ++i ) {
std::cout << "Name of subject [" << i+1 << "] : ? ";
std::cin >> subject_info[i].subject_name;
std::cout << "Score of " << subject_info[i].subject_name << " -> ";
std::cin >> subject_info[i].subject_score;
}
}
~student() {
delete [] subject_info;
}
void set_info() {
std::cout << "student\'s name : ? "; std::cin >> name;
std::cout << "number of subjects : ? "; std::cin >> subject_num;
subject_info = new subject [subject_num];
for ( unsigned int i=0; i<subject_num; ++i ) {
std::cout << "Name of subject [" << i+1 << "] : ? ";
std::cin >> subject_info[i].subject_name;
std::cout << "Score of " << subject_info[i].subject_name << " -> ";
std::cin >> subject_info[i].subject_score;
}
}
friend std::ostream& operator<< ( std::ostream& out, const student& one ) {
out << one.name << "\'s score information\n";
for ( unsigned int i=0; i<one.subject_num; ++i ) {
out << one.subject_info[i].subject_name << "\t-- ";
out << one.subject_info[i].subject_score << '\n';
}
return out;
}
friend double average_score( const student& one );
};
double average_score( const student& one ) {
double average;
for ( unsigned int i=0; i<one.subject_num; ++i ) {
average += one.subject_info[i].subject_score ;
}
return average/(double)one.subject_num;
}
int main(int argc, char *argv[]) {
unsigned int num = 0;
std::cout << "how many students in classroom_A1 ? "; std::cin >> num;
student* classroom_A1 = new student [num];
for ( unsigned int i=0; i<num; ++i ) {
classroom_A1[i].set_info();
}
for ( unsigned int i=0; i<num; ++i ) {
std::cout << classroom_A1[i] << std::endl;
std::cout << "average_score:\t" << average_score( classroom_A1[i] ) << std::endl;
}
delete [] classroom_A1;
}
排序没有,可以在这个语言结构中自行插上。
排版有点乱,你自己调整一下吧!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询