用C++代码实现一个单门课程成绩管理系统。进入系统时,用户可选择身份:1.教师;2学生。 以教师身份进入后,如果上次已保存了数据,可将文件中数据读取出来,并允许修改或添加新数据。如果为第一次录入(之前无保存的数据),则允许教师录入相关数据。具体功能包括: 1.可录入、修改、删除学生(数量不限)成绩,成绩项目包含两项:平时成绩、期末成绩,并按照各50%比例自动算出总成绩;学生基本信息包括学号、姓名;示例如下: 请录入“C++编程”课程成绩 学号:201901 姓名:张三 平时成绩:60 期末成绩:80 2.信息显示功能,可显示课程编号、课程名称、所有学生的成绩单。成绩单格式如下: 学号 姓名 平时成绩 期末成绩 总成绩 3.能够将上述数据保存在文件中,二进制、文本文件均可。 4.可读取上述文件。 以学生身份进入后,如果已保存了成绩数据,可将文件中数据读取出来,并允许查询。如果尚无保存数据,提示尚无成绩信息。具体功能包括: 1.信息显示功能,可显示课程编号、课程名称、所有学生的成绩单。
时间: 2024-02-25 08:58:10 浏览: 98
很好,这是一个很棒的问题,下面是一个简单的 C++ 代码实现。请注意,代码实现可能不是最优的,但可以满足您的需求。
```c++
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
struct Student{
string id;
string name;
double dailyScore;
double finalScore;
double totalScore;
};
void addStudent(Student* students, int& count){
Student student;
cout<<"请输入学生学号:";
cin>>student.id;
cout<<"请输入学生姓名:";
cin>>student.name;
cout<<"请输入学生平时成绩:";
cin>>student.dailyScore;
cout<<"请输入学生期末成绩:";
cin>>student.finalScore;
student.totalScore = (student.dailyScore + student.finalScore) / 2;
students[count++] = student;
}
void modifyStudent(Student* students, int count){
string id;
cout<<"请输入要修改的学生学号:";
cin>>id;
for(int i=0; i<count; i++){
if(students[i].id == id){
cout<<"学号\t姓名\t平时成绩\t期末成绩\t总成绩"<<endl;
cout<<students[i].id<<"\t"<<students[i].name<<"\t"<<students[i].dailyScore<<"\t\t"<<students[i].finalScore<<"\t\t"<<students[i].totalScore<<endl;
cout<<"请输入新的信息:"<<endl;
cout<<"学生姓名:";
cin>>students[i].name;
cout<<"学生平时成绩:";
cin>>students[i].dailyScore;
cout<<"学生期末成绩:";
cin>>students[i].finalScore;
students[i].totalScore = (students[i].dailyScore + students[i].finalScore) / 2;
cout<<"修改成功!"<<endl;
return;
}
}
cout<<"找不到该学生信息!"<<endl;
}
void deleteStudent(Student* students, int& count){
string id;
cout<<"请输入要删除的学生学号:";
cin>>id;
for(int i=0; i<count; i++){
if(students[i].id == id){
for(int j=i; j<count-1; j++){
students[j] = students[j+1];
}
count--;
cout<<"删除成功!"<<endl;
return;
}
}
cout<<"找不到该学生信息!"<<endl;
}
void displayScore(Student* students, int count){
cout<<"学号\t姓名\t平时成绩\t期末成绩\t总成绩"<<endl;
for(int i=0; i<count; i++){
cout<<students[i].id<<"\t"<<students[i].name<<"\t"<<students[i].dailyScore<<"\t\t"<<students[i].finalScore<<"\t\t"<<students[i].totalScore<<endl;
}
}
void saveToFile(Student* students, int count){
ofstream out("score.txt", ios::out);
if(out.is_open()){
out<<count<<endl;
for(int i=0; i<count; i++){
out<<students[i].id<<" "<<students[i].name<<" "<<students[i].dailyScore<<" "<<students[i].finalScore<<endl;
}
out.close();
cout<<"保存成功!"<<endl;
}else{
cout<<"打开文件失败!"<<endl;
}
}
void readFromFile(Student* students, int& count){
ifstream in("score.txt", ios::in);
if(in.is_open()){
in>>count;
for(int i=0; i<count; i++){
in>>students[i].id>>students[i].name>>students[i].dailyScore>>students[i].finalScore;
students[i].totalScore = (students[i].dailyScore + students[i].finalScore) / 2;
}
in.close();
cout<<"读取成功!"<<endl;
}else{
cout<<"打开文件失败!"<<endl;
}
}
void teacherMenu(Student* students, int& count){
int choice;
while(true){
cout<<"请选择操作:"<<endl;
cout<<"1. 录入学生成绩"<<endl;
cout<<"2. 修改学生成绩"<<endl;
cout<<"3. 删除学生成绩"<<endl;
cout<<"4. 显示成绩单"<<endl;
cout<<"5. 保存成绩到文件"<<endl;
cout<<"6. 从文件读取成绩"<<endl;
cout<<"7. 退出"<<endl;
cin>>choice;
switch(choice){
case 1:
addStudent(students, count);
break;
case 2:
modifyStudent(students, count);
break;
case 3:
deleteStudent(students, count);
break;
case 4:
displayScore(students, count);
break;
case 5:
saveToFile(students, count);
break;
case 6:
readFromFile(students, count);
break;
case 7:
return;
default:
cout<<"输入错误,请重新输入!"<<endl;
}
}
}
void studentMenu(Student* students, int count){
if(count == 0){
cout<<"尚无成绩信息!"<<endl;
return;
}
displayScore(students, count);
}
int main(){
int role;
cout<<"请选择身份:"<<endl;
cout<<"1. 教师"<<endl;
cout<<"2. 学生"<<endl;
cin>>role;
if(role == 1){
Student students[100];
int count = 0;
teacherMenu(students, count);
}else if(role == 2){
Student students[100];
int count = 0;
readFromFile(students, count);
studentMenu(students, count);
}else{
cout<<"输入错误!"<<endl;
}
return 0;
}
```
这个代码实现了您的要求,包括添加、修改、删除学生信息,显示成绩单,保存和读取文件等功能。当然,实际使用中可能还需要更多的功能和优化。
阅读全文