ADD file via upload

main
pw92p68vm 5 months ago
parent 015e1f11ad
commit c6fbf1b804

@ -0,0 +1,151 @@
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
// 学生类
class Student {
public:
// 构造函数
Student(const std::string& name, const std::string& id, int totalScore,
int chineseScore, int mathScore, int englishScore,
int elective1Score, int elective2Score1, int elective2Score2, int rank)
: name(name), id(id), totalScore(totalScore),
chineseScore(chineseScore), mathScore(mathScore), englishScore(englishScore),
elective1Score(elective1Score), elective2Score1(elective2Score1), elective2Score2(elective2Score2),
rank(rank) {}
// 打印学生信息
void printInfo() const {
std::cout << "Name: " << name << std::endl;
std::cout << "ID: " << id << std::endl;
std::cout << "Total Score: " << totalScore << std::endl;
std::cout << "Chinese Score: " << chineseScore << std::endl;
std::cout << "Math Score: " << mathScore << std::endl;
std::cout << "English Score: " << englishScore << std::endl;
std::cout << " Score: " << totalScore << std::endl;
std::cout << "Elective1 Score: " << elective1Score << std::endl;
std::cout << "Elective2 Score1: " << elective2Score1 << std::endl;
std::cout << "Elective2 Score2: " << elective2Score2 << std::endl;
std::cout << "Rank: " << rank << std::endl;
}
private:
std::string name;
std::string id;
int totalScore;
int chineseScore;
int mathScore;
int englishScore;
int elective1Score;
int elective2Score1;
int elective2Score2;
int rank;
};
// 学生管理类
class StudentManager {
public:
// 添加学生
void addStudent(const Student& student) {
students.push_back(student);
}
// 打印所有学生信息
void printAllStudents() const {
for (const auto& student : students) {
student.printInfo();
std::cout << std::endl; // 添加换行符以便分隔不同学生的信息
}
}
void saveToFile(const std::string& filename) const {
std::ofstream file(filename);
if (file.is_open()) {
for (const auto& student : students) {
// 假设有一个函数可以将Student对象转化为可保存的字符串
std::string studentStr = studentToString(student);
file << studentStr << std::endl;
}
file.close();
std::cout << "学生信息已保存到文件: " << filename << std::endl;
} else {
std::cerr << "无法打开文件: " << filename << std::endl;
}
}
static std::string studentToString(const Student& student) {
std::stringstream ss;
ss << s.name << "\t" << s.id << "\t" << s.totalScore << "\t" << s.chineseScore << "\t" << s.mathScore << "\t" << s.englishScore << "\t" << s.elective1Score << "\t" << s.elective2Score1 << "\t" << s.elective2Score2;
return ss.str();
private:
std::vector<Student> students; // 使用STL中的vector来管理学生信息
};
}
int main() {
StudentManager manager;
// 添加学生信息到管理器 ,例如学生张三学号为123456789高考总成绩为650语文成绩为120数学成绩为130英语成绩为110物理成绩为100化学成绩为90政治成绩为80省排名为2000
manager.addStudent(Student("张三", "123456789", 650, 120, 130, 110, 100, 90, 80, 2000));
// 打印所有学生信息
manager.printAllStudents();
StudentManager manager;
bool quit = false;
while (!quit) {
// 显示菜单
std::cout << "1. 添加学生" << std::endl;
std::cout << "2. 退出" << std::endl;
// 接收用户输入
int choice;
std::cin >> choice;
switch (choice) {
case 1: {
std::string name, id;
int totalScore, chineseScore, mathScore, englishScore, elective1Score, elective2Score1, elective2Score2, rank;
std::cout << "请输入学生姓名: ";
std::cin >> name;
std::cout << "请输入准考证号: ";
std::cin >> id;
std::cout << "请输入高考总成绩: ";
std::cin >> totalScore;
std::cout << "请输入语文成绩: ";
std::cin >> chineseScore;
std::cout << "请输入数学成绩: ";
std::cin >> mathScore;
std::cout << "请输入英语成绩: ";
std::cin >> englishScore;
std::cout << "请输入二选一科目成绩: ";
std::cin >> elective1Score;
std::cout << "请输入四选二科目一成绩: ";
std::cin >> elective2Score1;
std::cout << "请输入四选二科目二成绩: ";
std::cin >> elective2Score2;
std::cout << "请输入省排名: ";
std::cin >> rank;
// 创建Student对象并添加到管理器
manager.addStudent(Student(name, id, totalScore, chineseScore, mathScore, englishScore, elective1Score, elective2Score1, elective2Score2, rank));
break;
}
case 2:
quit = true;
break;
default:
std::cout << "无效的选择,请重新输入." << std::endl;
}
}
// 保存学生信息到文件
manager.saveToFile("students.txt");
return 0;
}
Loading…
Cancel
Save