c++3.定义学生类Student,包含成员:学号(num):整型,姓名(name):char [15], 四门课成绩(sc):一维整型数组,然后定义相关函数分别完成以下功能: 1)输入学生信息; 2)显示学生信息; 3)保存学生信息; 4)从文件读取学生信息; 在主函数中定义对象,并对上述函数进行测试。
时间: 2023-12-24 16:05:32 浏览: 186
以下是实现上述要求的C++代码:
```
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int COURSE_NUM = 4; // 成绩科目数
class Student {
private:
int num; // 学号
char name[15]; // 姓名
int sc[COURSE_NUM]; // 四门课成绩
public:
void input(); // 输入学生信息
void display(); // 显示学生信息
void save(); // 保存学生信息到文件
void load(); // 从文件读取学生信息
};
void Student::input() {
cout << "请输入学号:";
cin >> num;
cout << "请输入姓名:";
cin >> name;
cout << "请输入四门课成绩:" << endl;
for (int i = 0; i < COURSE_NUM; i++) {
cout << "第" << i + 1 << "门课成绩:";
cin >> sc[i];
}
}
void Student::display() {
cout << "学号:" << num << endl;
cout << "姓名:" << name << endl;
cout << "四门课成绩:" << endl;
for (int i = 0; i < COURSE_NUM; i++) {
cout << "第" << i + 1 << "门课成绩:" << sc[i] << endl;
}
}
void Student::save() {
ofstream fout("student.txt", ios::app);
fout << num << " " << name << " ";
for (int i = 0; i < COURSE_NUM; i++) {
fout << sc[i] << " ";
}
fout << endl;
fout.close();
}
void Student::load() {
ifstream fin("student.txt");
while (!fin.eof()) {
fin >> num >> name;
for (int i = 0; i < COURSE_NUM; i++) {
fin >> sc[i];
}
display();
}
fin.close();
}
int main() {
Student s;
int choice;
do {
cout << "请选择要进行的操作:" << endl;
cout << "1. 输入学生信息" << endl;
cout << "2. 显示学生信息" << endl;
cout << "3. 保存学生信息到文件" << endl;
cout << "4. 从文件读取学生信息" << endl;
cout << "5. 退出程序" << endl;
cin >> choice;
switch (choice) {
case 1:
s.input();
break;
case 2:
s.display();
break;
case 3:
s.save();
break;
case 4:
s.load();
break;
case 5:
cout << "程序已退出。" << endl;
break;
default:
cout << "输入无效,请重新输入。" << endl;
break;
}
} while (choice != 5);
return 0;
}
```
在主函数中创建了一个Student对象s,通过switch语句实现了菜单选择功能,用户可以选择输入学生信息、显示学生信息、保存学生信息到文件或从文件读取学生信息。其中,保存学生信息到文件使用了ofstream类,从文件读取学生信息使用了ifstream类。注意,在保存学生信息到文件时使用了ios::app参数,表示在文件末尾添加内容而不是覆盖原有内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)