用c++设计一个简单的学生成绩管理系统。设有学生成绩文件student.txt,每位学生成绩信息包含学号(num)、姓名(name)、性别(sex)、出生日期(birthday,包括年、月、日,分别表示为year,month,day)、3门功课的成绩(高数、C语言、大学物理)、总分和平均分。设计程序,要求从文件student.txt中读出学生成绩表,之后参对成绩表进行如下操作: (1)建立学生成绩信息结构数组(从student.txt读入数据) (2)显示。即输出出所有成绩。 (3)添加。可添加一条或多条记录。 (4)排序。分为两个方面,按学号递增排序和按总分递减排序。 (5)查找。分为三方面:按学号查找、按姓名查找和按分数查找。 (6)计算。计算各门课的平均分数。 (7)保存。退出程序前将结果保存到studeng.txt。 2)要求 (1)设计一个简单的界面 (2)每一个功能设为一个模块
时间: 2023-12-10 09:42:51 浏览: 75
这是一个比较大的项目,需要涉及到读写文件、结构体、数组、排序、查找等多个知识点,下面是一个简单的实现:
```
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct Student {
string num;
string name;
string sex;
int year;
int month;
int day;
int math;
int cprog;
int phy;
int total;
float avg;
};
int count = 0;
Student students[100];
void readData() {
ifstream fin("student.txt");
while (!fin.eof()) {
fin >> students[count].num;
fin >> students[count].name;
fin >> students[count].sex;
fin >> students[count].year;
fin >> students[count].month;
fin >> students[count].day;
fin >> students[count].math;
fin >> students[count].cprog;
fin >> students[count].phy;
students[count].total = students[count].math + students[count].cprog + students[count].phy;
students[count].avg = students[count].total / 3.0;
count++;
}
fin.close();
}
void display() {
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
for (int i = 0; i < count; i++) {
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
}
}
void add() {
cout << "请输入要添加的学生个数:";
int num;
cin >> num;
for (int i = 0; i < num; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "学号:";
cin >> students[count].num;
cout << "姓名:";
cin >> students[count].name;
cout << "性别:";
cin >> students[count].sex;
cout << "出生日期(格式为YYYY MM DD):";
cin >> students[count].year >> students[count].month >> students[count].day;
cout << "高数成绩:";
cin >> students[count].math;
cout << "C语言成绩:";
cin >> students[count].cprog;
cout << "大学物理成绩:";
cin >> students[count].phy;
students[count].total = students[count].math + students[count].cprog + students[count].phy;
students[count].avg = students[count].total / 3.0;
count++;
}
}
bool cmpNum(Student a, Student b) {
return a.num < b.num;
}
bool cmpTotal(Student a, Student b) {
return a.total > b.total;
}
void sort() {
int choice;
cout << "请选择排序方式(1.按学号递增排序 2.按总分递减排序):";
cin >> choice;
if (choice == 1) {
std::sort(students, students + count, cmpNum);
} else if (choice == 2) {
std::sort(students, students + count, cmpTotal);
} else {
cout << "输入有误,请重新选择!" << endl;
sort();
}
}
void searchNum() {
string num;
cout << "请输入要查找的学生学号:";
cin >> num;
for (int i = 0; i < count; i++) {
if (students[i].num == num) {
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
void searchName() {
string name;
cout << "请输入要查找的学生姓名:";
cin >> name;
for (int i = 0; i < count; i++) {
if (students[i].name == name) {
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
return;
}
}
cout << "未找到该学生!" << endl;
}
void searchScore() {
int score;
cout << "请输入要查找的成绩:";
cin >> score;
bool found = false;
cout << "学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分" << endl;
for (int i = 0; i < count; i++) {
if (students[i].math == score || students[i].cprog == score || students[i].phy == score) {
cout << students[i].num << "\t" << students[i].name << "\t" << students[i].sex << "\t" << students[i].year << "-" << students[i].month << "-" << students[i].day << "\t";
cout << students[i].math << "\t" << students[i].cprog << "\t" << students[i].phy << "\t" << students[i].total << "\t" << students[i].avg << endl;
found = true;
}
}
if (!found) {
cout << "未找到该成绩!" << endl;
}
}
void calcAvg() {
int mathSum = 0;
int cprogSum = 0;
int phySum = 0;
for (int i = 0; i < count; i++) {
mathSum += students[i].math;
cprogSum += students[i].cprog;
phySum += students[i].phy;
}
float mathAvg = mathSum / (float)count;
float cprogAvg = cprogSum / (float)count;
float phyAvg = phySum / (float)count;
cout << "高数平均分:" << mathAvg << endl;
cout << "C语言平均分:" << cprogAvg << endl;
cout << "大学物理平均分:" << phyAvg << endl;
}
void saveData() {
ofstream fout("student.txt");
for (int i = 0; i < count; i++) {
fout << students[i].num << " ";
fout << students[i].name << " ";
fout << students[i].sex << " ";
fout << students[i].year << " ";
fout << students[i].month << " ";
fout << students[i].day << " ";
fout << students[i].math << " ";
fout << students[i].cprog << " ";
fout << students[i].phy << endl;
}
fout.close();
}
void showMenu() {
cout << "1. 显示成绩" << endl;
cout << "2. 添加学生成绩" << endl;
cout << "3. 排序" << endl;
cout << "4. 查找" << endl;
cout << "5. 计算平均分" << endl;
cout << "6. 保存数据" << endl;
cout << "0. 退出程序" << endl;
}
int main() {
readData();
while (true) {
showMenu();
int choice;
cout << "请输入要执行的操作:";
cin >> choice;
if (choice == 0) {
break;
} else if (choice == 1) {
display();
} else if (choice == 2) {
add();
} else if (choice == 3) {
sort();
} else if (choice == 4) {
int searchChoice;
cout << "请选择查找方式(1.按学号查找 2.按姓名查找 3.按分数查找):";
cin >> searchChoice;
if (searchChoice == 1) {
searchNum();
} else if (searchChoice == 2) {
searchName();
} else if (searchChoice == 3) {
searchScore();
} else {
cout << "输入有误,请重新选择!" << endl;
}
} else if (choice == 5) {
calcAvg();
} else if (choice == 6) {
saveData();
} else {
cout << "输入有误,请重新选择!" << endl;
}
}
return 0;
}
```
这个程序中,我们首先定义了一个结构体 `Student`,表示学生的基本信息和成绩,然后用一个数组 `students` 存储所有学生的信息。`readData()` 函数从文件中读取数据,`display()` 函数显示所有成绩,`add()` 函数添加一条或多条记录,`sort()` 函数按照学号或总分进行排序,`searchNum()`、`searchName()` 和 `searchScore()` 函数分别按照学号、姓名和分数进行查找,`calcAvg()` 函数计算各门课程的平均分数,`saveData()` 函数将结果保存到文件中。
在主函数中,我们通过一个循环来不断读取用户的输入,根据用户输入的操作来调用相应的函数。
阅读全文