|
|
@ -1,2 +1,151 @@
|
|
|
|
# student
|
|
|
|
# student
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int MAX_STUDENTS = 100;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Student {
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
int score;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void inputData(Student students[], int& numStudents) {
|
|
|
|
|
|
|
|
if (numStudents >= MAX_STUDENTS) {
|
|
|
|
|
|
|
|
std::cout << "学生数量已达上限!" << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "请输入学生姓名: ";
|
|
|
|
|
|
|
|
std::cin >> students[numStudents].name;
|
|
|
|
|
|
|
|
std::cout << "请输入学生成绩(0 - 100): ";
|
|
|
|
|
|
|
|
std::cin >> students[numStudents].score;
|
|
|
|
|
|
|
|
while (students[numStudents].score < 0 || students[numStudents].score > 100) {
|
|
|
|
|
|
|
|
std::cout << "成绩输入有误,请重新输入: ";
|
|
|
|
|
|
|
|
std::cin >> students[numStudents].score;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
numStudents++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void modifyData(Student students[], int numStudents) {
|
|
|
|
|
|
|
|
int index;
|
|
|
|
|
|
|
|
std::cout << "请输入要修改的学生编号: ";
|
|
|
|
|
|
|
|
std::cin >> index;
|
|
|
|
|
|
|
|
if (index < 0 || index >= numStudents) {
|
|
|
|
|
|
|
|
std::cout << "编号有误!" << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "当前成绩: " << students[index].score << std::endl;
|
|
|
|
|
|
|
|
std::cout << "请输入新成绩(0 - 100): ";
|
|
|
|
|
|
|
|
std::cin >> students[index].score;
|
|
|
|
|
|
|
|
while (students[index].score < 0 || students[index].score > 100) {
|
|
|
|
|
|
|
|
std::cout << "输入有误,重新输入: ";
|
|
|
|
|
|
|
|
std::cin >> students[index].score;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deleteData(Student students[], int& numStudents) {
|
|
|
|
|
|
|
|
int index;
|
|
|
|
|
|
|
|
std::cout << "请输入要删除的学生编号: ";
|
|
|
|
|
|
|
|
std::cin >> index;
|
|
|
|
|
|
|
|
if (index < 0 || index >= numStudents) {
|
|
|
|
|
|
|
|
std::cout << "编号有误!" << std::endl;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = index; i < numStudents - 1; i++) {
|
|
|
|
|
|
|
|
students[i] = students[i + 1];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
numStudents--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void viewStatistics(Student students[], int numStudents) {
|
|
|
|
|
|
|
|
int highest = students[0].score;
|
|
|
|
|
|
|
|
int lowest = students[0].score;
|
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < numStudents; i++) {
|
|
|
|
|
|
|
|
if (students[i].score > highest) {
|
|
|
|
|
|
|
|
highest = students[i].score;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (students[i].score < lowest) {
|
|
|
|
|
|
|
|
lowest = students[i].score;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
total += students[i].score;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
double average = static_cast<double>(total) / numStudents;
|
|
|
|
|
|
|
|
std::cout << "最高分: " << highest << std::endl;
|
|
|
|
|
|
|
|
std::cout << "最低分: " << lowest << std::endl;
|
|
|
|
|
|
|
|
std::cout << "平均分: " << average << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void saveData(Student students[], int numStudents) {
|
|
|
|
|
|
|
|
std::ofstream file("student_data.txt");
|
|
|
|
|
|
|
|
if (file.is_open()) {
|
|
|
|
|
|
|
|
for (int i = 0; i < numStudents; i++) {
|
|
|
|
|
|
|
|
file << students[i].name << " " << students[i].score << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
std::cout << "数据保存成功!" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
std::cout << "保存失败!" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void readData(Student students[], int& numStudents) {
|
|
|
|
|
|
|
|
std::ifstream file("student_data.txt");
|
|
|
|
|
|
|
|
if (file.is_open()) {
|
|
|
|
|
|
|
|
numStudents = 0;
|
|
|
|
|
|
|
|
while (!file.eof()) {
|
|
|
|
|
|
|
|
file >> students[numStudents].name >> students[numStudents].score;
|
|
|
|
|
|
|
|
numStudents++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
numStudents--;
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
std::cout << "数据读取成功!" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
std::cout << "未找到文件,重新录入!" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
Student students[MAX_STUDENTS];
|
|
|
|
|
|
|
|
int numStudents = 0;
|
|
|
|
|
|
|
|
int choice;
|
|
|
|
|
|
|
|
std::cout << "是否读取数据(1 - 是,0 - 否): ";
|
|
|
|
|
|
|
|
std::cin >> choice;
|
|
|
|
|
|
|
|
if (choice == 1) {
|
|
|
|
|
|
|
|
readData(students, numStudents);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
std::cout << "1. 录入 2. 修改/删除 3. 统计 4. 退出" << std::endl;
|
|
|
|
|
|
|
|
std::cin >> choice;
|
|
|
|
|
|
|
|
switch (choice) {
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
inputData(students, numStudents);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
std::cout << "1. 修改 2. 删除" << std::endl;
|
|
|
|
|
|
|
|
int subChoice;
|
|
|
|
|
|
|
|
std::cin >> subChoice;
|
|
|
|
|
|
|
|
if (subChoice == 1) {
|
|
|
|
|
|
|
|
modifyData(students, numStudents);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (subChoice == 2) {
|
|
|
|
|
|
|
|
deleteData(students, numStudents);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
std::cout << "无效选择!" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
|
|
viewStatistics(students, numStudents);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
|
|
saveData(students, numStudents);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
std::cout << "无效选择,重新输入!" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|