一个很简单的学生成绩管理系统,我是用c++写的,用了点数据结构,
编译环境是VS2013+Windows10,
直接将代码导入编译器就能运行。
#include
#include
#include //升序降序的头文件
#include
using namespace std;
struct Student
{
int ID;
char name[10];
float mark1, mark2, mark3, mark4, mark5;
};
bool sort1(const Student &x, const Student &y);
bool sort2(const Student &x, const Student &y);
void menu();
int main()
{
system("color F2"); //("color zz") 显示功能
system("title 学生成绩管理系统"); //更改标题
int i, j, n = 0, t = 0, ID2;
int min, max;
char g;
string address;
float average1=0,average2=0,average3=0,average4=0;
Student stu[100];
ofstream output;
ifstream input;
menu();
input.open("学生成绩.txt");
while (!input.eof()) {
input >> stu[n].ID;
if (!input) {
cout << "打开文件有错误!" << endl;
continue;
}
input >> stu[n].name >> stu[n].mark1 >> stu[n].mark2 >> stu[n].mark3 >> stu[n].mark4;
stu[n].mark5 = stu[n].mark1 + stu[n].mark2 + stu[n].mark3 + stu[n].mark4;
n++;
}
cout << "————————————————————读取文件成功!————————————————————" << endl;
input.close();
cout << n;
while (1) {
cout << endl << "请选择:";
cin >> g;
switch (g) {
case '1': //将文件中的数据读出至结构体数组中,按学号从小到大排序,并显示之;
{
system("cls");
menu();
sort(stu, stu + n, sort2);
cout << "学号\t姓名\t成绩1\t成绩2\t成绩3\t成绩4\t总分" << endl;
for (i = 0; i < n; i++) {
cout << stu[i].ID << "\t" << stu[i].name << "\t" << stu[i].mark1 << "\t" << stu[i].mark2
<< "\t" << stu[i].mark3 << "\t" << stu[i].mark4 << "\t" << stu[i].mark5 << endl;
}
break;
} // cout << endl;
case '2': //输出总分最高分和最低分的名字;
{
system("cls");
menu();
min = max = stu[0].mark5;
for (i = 0; i < n; i++) {
if (min > stu[i].mark5)
min = stu[i].mark5;
if (max < stu[i].mark5)
max = stu[i].mark5;
}
for (j = 0; j < n; j++) {
if (stu[j].mark5 == min) {
cout << "总分最低的同学是:";
cout << stu[j].name << endl;
}
if (stu[j].mark5 == max) {
cout << "总分最高的同学是:";
cout << stu[j].name << endl;
}
}
break;
} //cout << endl;
case '3': //输出总分在340分,单科成绩不低于80分的名单;
{
system("cls");
menu();
cout << "以下是输出总分在340分,单科成绩不低于80分的名单:" << endl;
cout << "学号\t姓名\t成绩1\t成绩2\t成绩3\t成绩4\t总分" << endl;
for (i = 0; i < n; i++) {
if (stu[i].mark5 >= 340) {
if (stu[i].mark1 >= 80 && stu[i].mark2 >= 80 && stu[i].mark3 >= 80 && stu[i].mark4 >= 80) {
t = i;
cout << stu[i].ID << "\t" << stu[i].name << "\t" << stu[i].mark1 << "\t" << stu[i].mark2
<< "\t" << stu[i].mark3 << "\t" << stu[i].mark4 << "\t" << stu[i].mark5 << endl;
}
}
}
break;
}
case '4': //求出各科平均分数;
{
system("cls");
menu();
for (i = 0; i < n; i++) {
average1 += stu[i].mark1;
}
average1 = average1 / n;
for (i = 0; i < n; i++) {
average2 += stu[i].mark2;
}
average2 = average2 / n;
for (i = 0; i < n; i++) {
average3 += stu[i].mark3;
}
average3 = average3 / n;
for (i = 0; i < n; i++) {
average4 += stu[i].mark4;
}
average4 = average4 / n;
cout << "成绩1的平均分为:" << average1 << endl;
average1 = 0;
cout << "成绩2的平均分为:" << average2 << endl;
average2 = 0;
cout << "成绩3的平均分为:" << average3 << endl;
average3 = 0;
cout << "成绩4的平均分为:" << average4 << endl;
average4 = 0;
break;
}
case '5': //按总分排名;
{
system("cls");
menu();
sort(stu, stu + n, sort1);
cout << "排名\t学号\t姓名\t成绩1\t成绩2\t成绩3\t成绩4\t总分" << endl;
for (i = 0; i < n; i++) {
cout << i + 1 << "\t" << stu[i].ID << "\t" << stu[i].name << "\t" << stu[i].mark1 << "\t" << stu[i].mark2
<< "\t" << stu[i].mark3 << "\t" << stu[i].mark4 << "\t" << stu[i].mark5 << endl;
}
break;
}
case '6': //输出补考名单并存入文件。
{
system("cls");
menu();
output.open("补考同学名单.txt"); //ios::app 为追加输入
cout << "成绩1需补课的同学是:"; output << "成绩1需补课的同学是:";
for (i = 0; i < n; i++) {
if (stu[i].mark1 < 60) {
cout << stu[i].name << " ";
output << stu[i].name << " ";
}
}
cout << endl; output << endl;
cout << "成绩2需补课的同学是:"; output << "成绩2需补课的同学是:";
for (i = 0; i < n; i++) {
if (stu[i].mark2 < 60) {
cout << stu[i].name << " ";
output << stu[i].name << " ";
}
}
cout << endl; output << endl;
cout << "成绩3需补课的同学是:"; output << "成绩3需补课的同学是:";
for (i = 0; i < n; i++) {
if (stu[i].mark3 < 60) {
cout << stu[i].name << " ";
output << stu[i].name << " ";
}
}
cout << endl; output << endl;
cout << "成绩4需补课的同学是:"; output << "成绩4需补课的同学是:";
for (i = 0; i < n; i++) {
if (stu[i].mark4 < 60) {
cout << stu[i].name << " ";
output << stu[i].name << " ";
}
}output << endl;
output.close();
cout << endl << endl << "补考同学名单已存入 补考同学名单.txt ";
cout << endl;
break;
}
case '7': //增加学生的信息
{
system("cls");
menu();
//n++;
output.open("学生成绩.txt", ios::app);
cout << "请输入对应学生信息" << endl;
cout << "学号 姓名 成绩1 成绩2 成绩3 成绩4"<< endl;
cin >> stu[i].ID >> stu[i].name >> stu[i].mark1 >> stu[i].mark2 >> stu[i].mark3 >> stu[i].mark4;
stu[i].mark5 = stu[i].mark1 + stu[i].mark2 + stu[i].mark3 + stu[i].mark4;
output << endl << stu[i].ID << "\t" << stu[i].name << "\t" << stu[i].mark1 << "\t" << stu[i].mark2
<< "\t" << stu[i].mark3 << "\t" << stu[i].mark4;
cout << endl << "增加学生信息成功。" << endl;
output.close();
break;
}
case '8': //查询学生的信息
{
system("cls");
menu();
cout << "请输入学生学号:";
cin >> ID2;
for (i = 0; i < n; i++) {
if (stu[i].ID == ID2) {
cout << "学号\t姓名\t成绩1\t成绩2\t成绩3\t成绩4\t总分" << endl;
cout<< stu[i].ID << "\t" << stu[i].name << "\t" << stu[i].mark1 << "\t" << stu[i].mark2
<< "\t" << stu[i].mark3 << "\t" << stu[i].mark4 << "\t" << stu[i].mark5 << endl;
}
}
break;
}
case '9': //删除学生的信息
{
system("cls");
menu();
cout << "请输入学生学号:";
cin >> ID2;
output.open("学生成绩.txt");
for (i = 0; i < n; i++) {
if (stu[i].ID == ID2)
;
else {
output << stu[i].ID << "\t" << stu[i].name << "\t" << stu[i].mark1 << "\t" << stu[i].mark2
<< "\t" << stu[i].mark3 << "\t" << stu[i].mark4 << endl;
}
}
//n--;
cout << endl << "删除学生信息成功。" << endl << "请重新启动系统以确保数据正确。" << endl;
output.close();
break;
}
case '0':
{
cout << "—————————————————系统即将退出,谢谢使用。—————————————————" << endl << endl;
break;
}
default: {
system("cls");
menu();
cout << "输入有误,请重新输入" << endl;
break;
}
}
if (g == '0')
break;
if (g == '7')
n++;
if (g == '9')
n--;
cout << n;
}
return 0;
}
void menu()
{
cout<< "\n———————————————————学生成绩管理系统————————————————————\n\n";
cout << "\t\t\t ———————————————————————\n";
cout << "\t\t\t| 1.将文件中的数据读出至结构体数组中,并显示之;|\n";
cout << "\t\t\t| 2.输出总分最高分和最低分的名字; \t\t|\n";
cout << "\t\t\t| 3.输出总分在340分,单科成绩不低于80分的名单; |\n";
cout << "\t\t\t| 4.求出各科平均分数;\t\t\t\t|\n";
cout << "\t\t\t| 5.按总分排名; \t\t\t|\n";
cout << "\t\t\t| 6.输出补考名单并存入文件; \t\t\t|\n";
cout << "\t\t\t| 7.增加学生的信息。\t\t\t\t|\n";
cout << "\t\t\t| 8.查询学生的信息。\t\t\t\t|\n";
cout << "\t\t\t| 9.删除学生的信息。\t\t\t\t|\n";
cout << "\t\t\t| 0.结束。\t\t\t\t\t|\n";
cout << "\t\t\t ———————————————————————";
cout << endl;
}
bool sort1(const Student &x, const Student &y)
{
return x.mark5>y.mark5; //从小到大排<,若要从大到小排则>
}
bool sort2(const Student &x, const Student &y)
{
return x.ID
}
截图: