#include <iostream>
#include <fstream>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
struct student
{
int lead;
char num[13];
string name;
float score[3];
};
void menu();
void output(student stud);
void best(student stud[], float a[], int n);
void numberfind(student stud[], int n);
void namenotclear(student stud[], int n);
void all(student stud[], int n);
void add(student stud[], int &n, float *pa);
void subject(student stud[], int n);
void average(student stud[], int n, float a[]);
void eight(student stud[],int n);
void nine(student stud[], int n);
void nine1(student stud[], int n);
void ten(student stud[], int n);
void eleven(student stud[], int n);
int main()
{
int choose, n=0;
float a[1000];
student stud[1000];
menu();
fstream infile;
infile.open("score.txt");
for(int i=0; ; i++)
{
stud[i].num[12]='\0';
infile>>stud[i].lead>>stud[i].num>>stud[i].name>>stud[i].score[0]>>stud[i].score[1]>>stud[i].score[2];
a[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
if(stud[i].lead==0) break;
n++;
}
cin>>choose;
while (choose!=0)
{
switch (choose)
{
case 1: add(stud, n, a); break;
case 2: all(stud, n); break;
case 3: subject(stud, n); break;
case 4: namenotclear(stud, n); break;
case 5: numberfind(stud, n); break;
case 6: best(stud, a, n); break;
case 7: average(stud, n, a); break;
case 8: eight(stud, n); break;
case 9: nine(stud, n); break;
case 10: ten(stud, n); break;
case 11: eleven(stud, n); break;
//default:cout << "暂不存在该选项功能,请重新选择!!!"; break;
}
system("pause");
system("CLS");
menu();
cin>>choose;
}
cout<<"你已退出该系统"<<endl;
return 0;
}
void menu()
{
cout<<"2301班C++大作业--程序设计"<<endl;
cout<<"小组成员:侯勇鹏(20231003253) 吴欣泽(20231003258) 魏镇(20231003268)"<<endl;
cout<<"完成时间:2023年12月16日" <<endl;
cout<<"********************************"<<endl;
cout<<"*欢迎使用学生成绩管理系统 *"<<endl;
cout<<"*0.退出该系统 *"<<endl;
cout<<"*1.录入学生成绩 *"<<endl;
cout<<"*2.显示所有学生的信息 *"<<endl;
cout<<"*3.显示某门课程的所有学生成绩 *"<<endl;
cout<<"*4.按姓名查找某学生成绩 *"<<endl;
cout<<"*5.按学号查找学生成绩 *"<<endl;
cout<<"*6.查找最高平均分 *"<<endl;
cout<<"*7.计算学生的平均成绩 *"<<endl;
cout<<"*8.按平均分分数排序 *"<<endl;
cout<<"*9.修改学生信息 *"<<endl;
cout<<"*10.学生成绩水平分析 *"<<endl;
cout<<"*11.将学生成绩信息导出到文件中 *"<<endl;
cout<<"********************************"<<endl;
cout<<"请输入你要选择的功能:";
}
void output(student stud)
{
cout<<stud.lead<<"\t"<<stud.num<<" "<<setiosflags(ios_base::left)<<setw(18)<<stud.name<<stud.score[0]<<"\t"<<stud.score[1]<<"\t"<<stud.score[2]<<endl;
}
void best(student stud[], float a[], int n)//功能六:查找最高平均分
{
int p=a[0], k=0;
for(int i=1; i<n; i++)
if(p<a[i])
{
p=a[i];
k=i;
}
cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<<"语文 "<<"数学 "<<"英语"<<endl;
output(stud[k]);
cout<<"最高平均分为:"<< fixed << setprecision(3) << a[k]/3 <<endl;
}
void numberfind(student stud[], int n)//功能五:按学号查找学生成绩
{
int k, p;
char numfind[13];
numfind[12]='\0';
cout<<"请输入需要查询的学号:";
cin>>numfind;
for(int i=0; i<n; i++)
{
k=strcmp(numfind, stud[i].num);
if(k==0) {p=i; break;}
}
if(k==0)
{
cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<<"语文 "<<"数学 "<<"英语"<<endl;
output(stud[p]);
}
else cout<<"Not Found"<<endl;
}
void namenotclear(student stud[], int n)//功能四:按姓名查找某学生成绩
{
int k=0;
string namefind;
cout<<"请输入需要查询的名字:";
cin>>namefind;
cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<<"语文 "<<"数学 "<<"英语"<<endl;
for(int i=0; i<n; i++)
{
if (stud[i].name.find(namefind) != string::npos)
{
output(stud[i]);
k=1;
}
}
if(k==0) cout<<"Not Found"<<endl;
}
void all(student stud[], int n)//功能二:显示所有学生的信息
{
cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<<"语文 "<<"数学 "<<"英语"<<endl;
for(int i=0; i<n; i++)
output(stud[i]);
}
void add(student stud[], int& n, float a[])//功能一:录入学生成绩
{
int m, k, l;
student hhh;
cout << "需要录入的学生人数:";
cin >> m;
cout << "请输入需要录入的学生的信息(请依次输入学号、姓名和三门课的成绩)" << endl;
for (int i = n; i < n + m; i++)
{
cout << "学生" << i - n + 1 << ":";
cin >> hhh.num >> hhh.name >> hhh.score[0] >> hhh.score[1] >> hhh.score[2];
if (strlen(hhh.num) != 12)
{
cout << "输入的学号位数应是12位(~._.~)!请重新输入" << endl;
i = i - 1;
continue;
}
for (int j = 0; j < i; j++)
{
k = strcmp(stud[j].num, hhh.num);
if (k == 0) break;
}
if (k == 0)
{
cout << "学号重复!请重新输入" << endl;
i = i - 1;
continue;
}
stud[i] = hhh;
stud[i].lead = i + 1;
a[i] = stud[i].score[0] + stud[i].score[1] + stud[i].score[2];
}
n = n + m;
}
void subject(student stud[], int n)//功能三:显示某门课程的所有学生成绩
{
int choose;
cout << "请输入所需查询成绩的课程(0.语文;1.数学;2.英语):";
cin >> choose;
switch (choose)
{
case 0:cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<< "语文" << endl; break;
case 1:cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<< "数学" << endl; break;
case 2:cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<< "英语" << endl; break;
default:cout << "不存在该课程!!!" << endl;
}
if(choose>=0&&choose<=2)
for(int i=0; i<n; i++)
cout<<stud[i].lead<<"\t"<<stud[i].num<<" "<<setiosflags(ios_base::left)<<setw(18)<<stud[i].name<<stud[i].score[choose]<<endl;
}
void average(student stud[], int n, float a[])//功能七:计算学生的平均成绩
{
float m[1000], d, b, c, e;
b=c=d=0;
for(int i=0; i<n; i++)
{
b+=stud[i].score[0];
c+=stud[i].score[1];
d+=stud[i].score[2];
}
e=b+c+d;
b=b/n; c=c/n; d=d/n; e=e/n;
cout<<"所有学生的平均成绩:"<<endl;
cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<< "平均成绩"<<endl;
for(int i=0; i<n; i++)
{
cout<<stud[i].lead<<"\t"<<stud[i].num<<" "<<setiosflags(ios_base::left)<<setw(18)<<stud[i].name<< fixed << setprecision(1) << a[i]/3 <<endl;
}
cout<<endl;
cout<<"所有学生语文的平均成绩:"<< fixed << setprecision(3) << b<<endl;
cout<<"所有学生数学的平均成绩:"<< fixed << setprecision(3) << c<<endl;
cout<<"所有学生英语的平均成绩:"<< fixed << setprecision(3) << d<<endl;
cout<<"所有学生的总平均成绩: "<< fixed << setprecision(3) << e<<endl;
}
void eight(student stud[],int n)//功能八:按平均分分数排序
{
student stud1[1000], stud2;
for(int i=0; i<n; i++)
{
stud1[i]=stud[i];
}
int i,j,match;
for(int i=0; i<n-1; i++)
{
int item=0;
for(j=0; j<n-i-1; j++)
{
if(a[j]<a[j+1])
{
match=a[j];
a[j]=a[j+1];
a[j+1]=match;
stud2=stud1[j];
stud1[j]=stud1[j+1];
stud1[j+1]=stud2;
item=1;
}
}
if(item==0) break;
}
cout<<"按平均分从大到小排名结果为:"<<endl;
cout<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<< "平均成绩"<<endl;
for(i=0; i<n; i++)
cout<<stud1[i].lead<<"\t"<<stud1[i].num<<" "<<setiosflags(ios_base::left)<<setw(18)<<stud1[i].name<< fixed << setprecision(1) << a[i]/3 <<endl;
}
void nine1(student stud[], int n, int l)
{
int o, t, k = 1;
cout << "请选择需要修改的信息(1.学号/2.姓名/3.成绩):";
cin >> o;
char hh;
student haha;
if (o == 1)
{
while (1)
{
cout << "请输入新的学号(12位):";
cin >> haha.num;
for (int i = 0; i < n; i++)
{
k = 1;
k = strcmp(haha.num, stud[i].num);
if (k == 0)
{
cout << "学号重复,请重新输入:";
cin >> haha.num;
i = 0;
continue;
}
}
if (strlen(haha.num) != 12) { cout << "输入的学号位数应是12位!(~._.~) " << endl; continue; }
if (k != 0) break;
}
cout << "确定要修改吗?(Y/N):";
cin >> hh;
if (hh == 'Y')
{
strcpy(stud[l].num, haha.num);
cout << "修改成功!" << endl;
}
else if (hh == 'N') cout << "已取消修改" << endl;
else cout << "输入错误,已取消修改" << endl;
}
else if (o == 2)
{
cout << "请输入新的姓名:";
cin >> haha.name;
cout << "确定要修改吗?(Y/N):";
cin >> hh;
if (hh == 'Y')
{
stud[l].name = haha.name;
cout << "修改成功!" << endl;
}
else if (hh == 'N') cout << "已取消修改" << endl;
else cout << "输入错误,已取消修改" << endl;
}
else if (o == 3)
{
student s1;
while (1)
{
cout << "请选择需要修改成绩的课程(1.语文/2.数学/3.英语):";
cin >> t;
if (t == 1)
{
cout << "请输入新的语文成绩:";
cin >> s1.score[0];
cout << "确定要修改吗?(Y/N):";
cin >> hh;
if (hh == 'Y')
{
stud[l].score[0] = s1.score[0];
cout << "修改成功!" << endl;
}
else if (hh == 'N') cout << "已取消修改" << endl;
else cout << "输入错误,已取消修改" << endl;
}
else if (t == 2)
{
cout << "请输入新的数学成绩:";
cin >> s1.score[1];
cout << "确定要修改吗?(Y/N):";
cin >> hh;
if (hh == 'Y')
{
stud[l].score[1] = s1.score[1];
cout << "修改成功!" << endl;
}
else if (hh == 'N') cout << "已取消修改" << endl;
else cout << "输入错误,已取消修改" << endl;
}
else if (t == 3)
{
cout << "请输入新的英语成绩:";
cin >> s1.score[2];
cout << "确定要修改吗?(Y/N):";
cin >> hh;
if (hh == 'Y')
{
stud[l].score[2] = s1.score[2];
cout << "修改成功!" << endl;
}
else if (hh == 'N') cout << "已取消修改" << endl;
else cout << "输入错误,已取消修改" << endl;
}
else { cout << "输入错误,请重新输入" << endl; continue; }
cout << "是否需要修改其他课程的成绩(Y/N):";
cin >> hh;
if (hh == 'Y') hh = 'Y';
else if (hh == 'N') { cout << "已退出成绩修改功能" << endl; break; }
else { cout << "输入错误,已退出成绩修改功能" << endl; break; }
}
}
else cout << "输入错误!" << endl;
}
void nine(student stud[], int n)
{
char hh;
int o, k, l;
all(stud, n);
cout << "请写出需要修改的学生的学号或序号(1.学号/2.序号):";
cin >> o;
if (o == 1)
{
char numfind[13];
numfind[12] = '\0';
cout << "请输入学生的学号:";
cin >> numfind;
for (int i = 0; i < n; i++)
{
k = strcmp(numfind, stud[i].num);
if (k == 0) { l = i + 1; break; }
}
if (k == 0)
{
cout << "序号 " << "学号 " << setiosflags(ios_base::left) << setw(18) << "姓名" << "语文 " << "数学 " << "英语" << endl;
output(stud[l - 1]);
}
}
if (o == 2)
{
cout << "请输入学生的序号:";
cin >> l;
if (l <= n && l > 0)
{
cout << "序号 " << "学号 " << setiosflags(ios_base::left) << setw(18) << "姓名" << "语文 " << "数学 " << "英语" << endl;
output(stud[l - 1]);
}
}
if ((o == 1 && k == 0) || (o == 2 && (l <= n && l > 0)))
{
while (1)
{
nine1(stud, n, l - 1);
cout << "是否要继续修改该学生的其他信息?(Y/N):";
cin >> hh;
if (hh == 'Y') continue;
else if (hh == 'N') { cout << "已退出修改信息功能" << endl; break; }
else { cout << "输入错误,已退出修改信息功能" << endl; break; }
}
}
else cout << "输入错误/没有该序号/没有该学号,已退出修改信息功能" << endl;
}
void ten(student stud[], int n) //功能十:学生成绩水平分析
{
int a=0, b=0, c=0, d=0;
cout<<"学生成绩水平分析如下所示(总人数:"<<n<<"):"<<endl;
cout<<"\t"<<"<60"<<"\t"<<"60~70"<<"\t"<<"70~90"<<"\t"<<"90~100\t"<<endl;
cout<<"\t"<<"不及格"<<"\t"<<"及格"<<"\t"<<"良好"<<"\t"<<"优秀\t"<<endl;
for(int i=0; i<n; i++)
{
if(stud[i].score[0]<60) a++;
if(stud[i].score[0]>=60&&stud[i].score[0]<70) b++;
if(stud[i].score[0]>=70&&stud[i].score[0]<90) c++;
if(stud[i].score[0]>=90&&stud[i].score[0]<=100) d++;
}
cout<<"语文\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl;
a=b=c=d=0;
for(int i=0; i<n; i++)
{
if(stud[i].score[1]<60) a++;
if(stud[i].score[1]>=60&&stud[i].score[1]<70) b++;
if(stud[i].score[1]>=70&&stud[i].score[1]<90) c++;
if(stud[i].score[1]>=90&&stud[i].score[1]<=100) d++;
}
cout<<"数学\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl;
a=b=c=d=0;
for(int i=0; i<n; i++)
{
if(stud[i].score[2]<60) a++;
if(stud[i].score[2]>=60&&stud[i].score[2]<70) b++;
if(stud[i].score[2]>=70&&stud[i].score[2]<90) c++;
if(stud[i].score[2]>=90&&stud[i].score[2]<=100) d++;
}
cout<<"英语\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl;
}
void eleven(student stud[], int n)//功能十一:将学生成绩信息导出到文件中
{
char hh;
cout<<"确定要导出到文件“score_out.txt”中吗?(Y/N):" ;
cin>>hh;
if(hh=='Y')
{
ofstream outfile;
outfile.open("score_out.txt");
outfile<<"序号 "<<"学号 "<<setiosflags(ios_base::left)<<setw(18)<<"姓名"<<"语文 "<<"数学 "<<"英语"<<endl;
for(int i=0; i<n; i++)
{
outfile<<stud[i].lead<<"\t"<<stud[i].num<<" "<<setiosflags(ios_base::left)<<setw(18)<<stud[i].name<<stud[i].score[0]<<"\t"<<stud[i].score[1]<<"\t"<<stud[i].score[2]<<endl;
}
cout<<"已成功导出"<<endl;
}
else if(hh=='N') cout<<"已取消导出"<<endl;
else cout<<"输入错误,已取消导出" <<endl;
}