c++上机练习4 (要求用面向对象的方法实现) 1).从键盘上输入一组同学的基本信息,然后按照学号升序的形式在屏幕上显示出来。(学号、姓名、性别、生日、学院、专业班级) 2),从键盘上输入一组课程信息(课程号、课程名,学分),然后按照课程号升序的在屏幕上显示出课程信息。 3).从键盘上输入一组同学的选课信息(学号、课程号、成绩),然后在屏幕上显示出每位同学的各科成绩和平均成绩。 4).将平均成绩最高同学的基本信息显示在屏幕上。要求:1.私有数据成员:相应结构数组、记录条数 2.各类中包含公有成员函数如下:构造函数、初始化函数、插入函数、查找记录函数、删除记录函数、排序函数、显示函数等。 3.编程测试以上功能。
时间: 2023-02-07 14:24:45 浏览: 92
这道题要求你使用面向对象的方法来实现一组学生的选课信息的输入,排序和输出。你需要定义至少四个类:Student、Course、Enrollment、School。
Student 类应该包含以下私有数据成员:
- 学号
- 姓名
- 性别
- 生日
- 学院
- 专业班级
Course 类应该包含以下私有数据成员:
- 课程号
- 课程名
- 学分
Enrollment 类应该包含以下私有数据成员:
- 学号
- 课程号
- 成绩
School 类应该包含以下私有数据成员:
- 学生数组
- 课程数组
- 选课信息数组
各类应该包含以下公有成员函数:
- Student 类:
- 构造函数: 用于创建新的学生对象并初始化数据成员
- 初始化函数: 用于从键盘输入学生信息并初始化对象的数据成员
- 插入函数: 用于向学生数组中添加新的学生对象
- 查找记录函数: 用于根据学号查找学生信息
- 删除记录函数: 用于根据学号删除学生信息
- 排序函数: 用于将学生数组按照学号升序排序
- 显示函数: 用于在屏幕上显示学生信息
-
相关问题
要求用C++面向对象的方法实现 1).从键盘上输入一组同学的基本信息,然后按照学号升序的形式在屏幕上显示出来。(学号、姓名、性别、生日、学院、专业班级) 2),从键盘上输入一组课程信息(课程号、课程名,学分),然后按照课程号升序的在屏幕上显示出课程信息。 3).从键盘上输入一组同学的选课信息(学号、课程号、成绩),然后在屏幕上显示出每位同学的各科成绩和平均成绩。 4).将平均成绩最高同学的基本信息显示在屏幕上。要求:1.私有数据成员:相应结构数组、记录条数 2.各类中包含公有成员函数如下:构造函数、初始化函数、插入函数、查找记录函数、删除记录函数、排序函数、显示函数等。 请用C++编程以上功能。
这是一个使用 C 语言实现面向对象的例子:
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 10
#define MAX_COURSES 5
typedef struct {
int id;
char name[50];
char gender[10];
char birthday[20];
char college[50];
char major[50];
} Student;
typedef struct {
int id;
char name[50];
int credits;
} Course;
typedef struct {
int student_id;
int course_id;
int score;
} Enrollment;
typedef struct {
Student students[MAX_STUDENTS];
int num_students;
Course courses[MAX_COURSES];
int num_courses;
Enrollment enrollments[MAX_STUDENTS * MAX_COURSES];
int num_enrollments;
} School;
School school;
void init(School *school) {
school->num_students = 0;
school->num_courses = 0;
school->num_enrollments = 0;
}
void insert_student(School *school, Student student) {
school->students[school->num_students++] = student;
}
void insert_course(School *school, Course course) {
school->courses[school->num_courses++] = course;
}
void insert_enrollment(School *school, Enrollment enrollment) {
school->enrollments[school->num_enrollments++] = enrollment;
}
Student *find_student(School *school, int id) {
for (int i = 0; i < school->num_students; i++) {
if (school->students[i].id == id) {
return &school->students[i];
}
}
return NULL;
}
Course *find_course(School *school, int id) {
for (int i = 0; i < school->num_courses; i++) {
if (school->courses[i].id == id) {
return &school->courses[i];
}
}
return NULL;
}
Enrollment *find_enrollment(School *school, int student_id, int course_id) {
for (int i = 0; i < school->num_enrollments; i++) {
if (school->enrollments[i].student_id == student_id &&
school->enrollments[i].course_id == course_id) {
return &school->enrollments[i];
}
}
return NULL;
}
void delete_student(School *school, int id) {
Student *student = find_student(school, id);
if (student) {
for (int i = 0; i < school->num_enrollments; i++) {
if (school->enrollments[i].student_id == id)
1.从键盘上任意输入一个4*4的矩阵,并将其输出:c++
下面是一个简单的示例代码,用于从键盘上输入一个4*4的矩阵并将其输出:
```c++
#include <iostream>
using namespace std;
int main()
{
int matrix[4][4];
// 从键盘上输入矩阵
cout << "请输入4*4的矩阵:" << endl;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cin >> matrix[i][j];
}
}
// 输出矩阵
cout << "您输入的矩阵为:" << endl;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl; // 换行
}
return 0;
}
```
示例输入:
```
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
```
示例输出:
```
请输入4*4的矩阵:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
您输入的矩阵为:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
```