新人求助,以下需求该怎么实现呢?
c++吧
全部回复
仅看楼主
level 6
想知道以下的需求怎么实现,有代码最好辣😋😋😋
2023年04月21日 11点04分 1
level 6
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
// 定义Person类
class Person {
protected: // 使用protected保护数据成员
string name;
string sex;
int age;
public:
Person(string _name = "", string _sex = "", int _age = 0) :name(_name), sex(_sex), age(_age) {};
virtual void show() {
cout << "姓名:" << name << " 性别:" << sex << " 年龄:" << age << endl;
}
};
// 定义Student类继承于Person类
class Student :public Person {
private:
string id; // 学号
string enrollment_date; // 入学日期
int enrollment_score; // 入学成绩
public:
Student(string _name = "", string _sex = "", int _age = 0, string _id = "", string _date = "", int _score = 0) :Person(_name, _sex, _age), id(_id), enrollment_date(_date), enrollment_score(_score) {};
void show() { // 重载Person类的show函数
cout << "姓名:" << name << " 性别:" << sex << " 年龄:" << age << " 学号:" << id << " 入学日期:" << enrollment_date << " 入学成绩:" << enrollment_score << endl;
}
int getScore() { // 获取学生入学成绩
return enrollment_score;
}
void setScore(int score) { // 设置学生入学成绩
enrollment_score = score;
}
};
2023年04月21日 11点04分 2
level 6
// 从文件中读取学生信息并显示
void readFromFile() {
ifstream infile; // 文件输入流对象
infile.open("student.txt"); // 打开文件
if (!infile) {
cout << "无法打开文件" << endl;
return;
}
string name, sex, id, date;
int age, score;
infile >> name >> sex >> age >> id >> date >> score; // 读取文件中的学生信息
while (!infile.eof()) { // 循环读取信息并显示
Student s(name, sex, age, id, date, score);
s.show();
infile >> name >> sex >> age >> id >> date >> score;
}
infile.close(); // 关闭文件
system("pause");
system("cls");
}
2023年04月21日 11点04分 3
level 6
// 修改学生入学成绩
void modifyScore() {
string id;
int score;
cout << "请输入需要修改入学成绩的学生学号:";
cin >> id;
cout << "请输入修改后的入学成绩:";
cin >> score;
ifstream infile;
infile.open("student.txt");
if (!infile) {
cout << "无法打开文件" << endl;
return;
}
string name, sex, date;
int age, orig_score;
ofstream outfile;
outfile.open("temp.txt"); // 临时文件
if (!outfile) {
cout << "无法打开文件" << endl;
return;
}
infile >> name >> sex >> age >> id >> date >> orig_score;
while (!infile.eof()) { // 逐个读取学生信息
if (id == id) { // 找到目标学生并修改其信息
Student s(name, sex, age, id, date, score);
s.show(); // 显示修改后的学生信息
outfile << name << " " << sex << " " << age << " " << id << " " << date << " " << score << endl; // 将修改后的学生信息写入文件
}
else { // 其他学生信息不变
outfile << name << " " << sex << " " << age << " " << id << " " << date << " " << orig_score << endl;
}
infile >> name >> sex >> age >> id >> date >> orig_score;
}
infile.close();
outfile.close();
remove("student.txt"); // 删除原文件
rename("temp.txt", "student.txt"); // 重命名临时文件
system("pause");
system("cls");
}
2023年04月21日 11点04分 4
level 6
// 添加学生信息
void addStudent() {
string name, sex, id, date;
int age, score;
cout << "请输入新学生的姓名、性别、年龄、学号、入学日期、入学成绩:" << endl;
cin >> name >> sex >> age >> id >> date >> score;
ofstream outfile;
outfile.open("student.txt", ios::app); // 在文件末尾添加
if (!outfile) {
cout << "无法打开文件" << endl;
return;
}
outfile << name << " " << sex << " " << age << " " << id << " " << date << " " << score << endl; // 添加新学生信息
outfile.close();
system("pause");
system("cls");
}
int main() {
while (true) { // 无限循环,等待用户选择操作
cout << "请选择操作:" << endl;
cout << "1.查看学生信息" << endl;
cout << "2.修改学生入学成绩 " << endl;
cout << "3.添加学生信息 " << endl;
cout << "4.退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
readFromFile();
break;
case 2:
modifyScore();
break;
case 3:
addStudent();
break;
case 4:
return 0; // 退出程序
default:
cout << "输入错误,请重新输入" << endl;
break;
}
}
return 0;
}
2023年04月21日 12点04分 5
level 6
想问问代码哪里有问题呢?[疑问][疑问][疑问]
2023年04月21日 12点04分 6
level 1
emmmmm,可以参考一下哈[柯基暗中观察][柯基暗中观察][柯基暗中观察]:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义Person类
class Person {
public:
// 构造函数
Person(string n, string s, int a) {
name = n;
sex = s;
age = a;
}
// 成员函数,输出基础信息成员变量
void show() {
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
cout << "Age: " << age << endl;
}
protected:
string name; // 名字
string sex; // 性别
int age; // 年龄
};
int main() {
Person per1("Tom", "Male", 20);
per1.show();
return 0;
}
```
[钱币][钱币][钱币]可以参考一下,这个是第一个,我根据要求定义了一个名为Person的类,然后呢里面包含了name、sex和age三个成员变量以及一个show()成员函数。其实~主要就是在main函数中,创建一个名为per1的Person对象,并输出其基础信息(即调用了show()函数)。
2023年04月21日 12点04分 8
1