谷样例似乎过了,wa求调。
#include <bits/stdc++.h>
using namespace std;
const double EPS=1e-5;
namespace A_Typical_Homework{
using namespace std;
struct score{
int a[10];
int& operator[](int i){
return a[i];
}
friend istream& operator>>(istream& in,score& b){
in>>b[1]>>b[2]>>b[3]>>b[4];
return in;
}
friend ostream& operator<<(ostream& out,score& b){
out<<b[1]<<' '<<b[2]<<' '<<b[3]<<' '<<b[4];
return out;
}
score& operator=(score& b){
for(int i=1; i<=6; i++)this->a[i]=b[i];
return *this;
}
};
struct student{
string name,sid;
int cid;
score sco;
bool remov;
student(){
remov=0;
name="";
sid="";
for(int i=1; i<=6; i++)sco[i]=0;
cid=0;
}
student& operator=(student& a){
this->cid=a.cid;
this->sid=a.sid;
this->name=a.name;
this->sco=a.sco;
return *this;
}
friend istream& operator>>(istream& in,student& tmp){
in>>tmp.cid>>tmp.name>>tmp.sco;
return in;
}
friend ostream& operator<<(ostream& out,student& tmp){
out<<tmp.sid<<' '<<tmp.cid<<' '<<tmp.name<<' '<<tmp.sco<<' ';
return out;
}
} stu[1000];
int cnt=0;
bool is_same_sid(student stu_tmp){
for(int i=1; i<=cnt; i++) {
if(stu_tmp.sid==stu[i].sid)return true;
}
return false;
}
void join(student stu_tmp){
stu[++cnt]=stu_tmp;
}
bool is_sid(string s){
if(s[0]>='0'&&s[0]<='9')return 1;
else return 0;
}
int find_sid_or_name_cnt_and_del_it(string s){
int ans;
if(is_sid(s)){
for(int i=1;i<=cnt;i++){
if(stu[i].sid==s&&!stu[i].remov){
ans++;
stu[i].remov=1;
}
}
}
else{
for(int i=1;i<=cnt;i++){
if(stu[i].name==s&&!stu[i].remov){
ans++;
stu[i].remov=1;
}
}
}
return ans;
}
int total(student stud){
int tot=0;
for(int i=1;i<=6;i++)tot+=stud.sco[i];
return tot;
}
int rank(student stud){
int ans=1;
for(int i=1;i<=cnt;i++){
if(!stu[i].remov&&total(stu[i])>total(stud))ans++;
}
return ans;
}
int type(string s){
if(s=="Chinese")return 1;
if(s=="Mathematics")return 2;
if(s=="English")return 3;
if(s=="Programming")return 4;
}
void part_subject(string subjects,int class_need){
cout<<subjects<<'\n';
int sub_type=type(subjects);
int pass=0,fail=0,sum=0,num=0;
double avg;
if(class_need){
for(int i=1;i<=cnt;i++){
if(!stu[i].remov&&stu[i].cid==class_need){
num++;
sum+=stu[i].sco[sub_type];
if(stu[i].sco[sub_type]>=60)pass++;
else fail++;
}
}
}
else{
for(int i=1;i<=cnt;i++){
if(!stu[i].remov){
num++;
sum+=stu[i].sco[sub_type];
if(stu[i].sco[sub_type]>=60)pass++;
else fail++;
}
}
}
if(num){
avg=sum/(num+0.0)+EPS;
cout<<"Average Score: "<<fixed<<setprecision(2)<<avg<<'\n';
cout<<"Number of passed students: "<<pass<<'\n';
cout<<"Number of failed students: "<<fail<<'\n'<<'\n';
}
else{
cout<<"Average Score:-nan"<<'\n';
cout<<"Number of passed students: "<<pass<<'\n';
cout<<"Number of failed students: "<<fail<<'\n'<<'\n';
}
}
void over_alls(int class_need){
int answer[]={0,0,0,0,0};
if(class_need){
for(int i=1;i<=cnt;i++){
int array_=0;
if(!stu[i].remov&&stu[i].cid==class_need){
for(int j=1;j<=4;j++){
if(stu[i].sco[j]>=60)array_++;
}
if(array_){
for(int j=array_;j>0;j--){
answer[j]++;
}
}
else answer[array_]++;
}
}
}
else{
for(int i=1;i<=cnt;i++){
int array_=0;
if(!stu[i].remov){
for(int j=1;j<=4;j++){
if(stu[i].sco[j]>=60)array_++;
}
if(array_){
for(int j=array_;j>0;j--){
answer[j]++;
}
}
else answer[array_]++;
}
}
}
cout<<"Overall:"<<'\n';
cout<<"Number of students who passed all subjects: "<<answer[4]<<'\n';
cout<<"Number of students who passed 3 or more subjects: "<<answer[3]<<'\n';
cout<<"Number of students who passed 2 or more subjects: "<<answer[2]<<'\n';
cout<<"Number of students who passed 1 or more subjects: "<<answer[1]<<'\n';
cout<<"Number of students who failed all subjects: "<<answer[0]<<'\n'<<'\n';
}
void greet(){
cout<<"Welcome to Student Performance Management System (SPMS)."<<endl;
cout<<endl;
cout<<"1 - Add"<<endl;
cout<<"2 - Remove"<<endl;
cout<<"3 - Query"<<endl;
cout<<"4 - Show ranking"<<endl;
cout<<"5 - Show Statistics"<<endl;
cout<<"0 - Exit"<<endl;
cout<<endl;
}
void add(){
for(;;){
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish."<<'\n';
student tmp;
cin>>tmp.sid;
if(tmp.sid=="0")return;
cin>>tmp;
if(!is_same_sid(tmp)){
join(tmp);
}
else cout<<"Duplicated SID."<<'\n';
}
}
void remove(){
int ans;
for(;;){
cout<<"Please enter SID or name. Enter 0 to finish."<<'\n';
string s;
cin>>s;
if(s=="0")return;
ans+=find_sid_or_name_cnt_and_del_it(s);
cout<<ans<<" student(s) removed."<<'\n';
}
}
void query(){
for(;;){
cout<<"Please enter SID or name. Enter 0 to finish."<<'\n';
string s;
cin>>s;
if(s=="0")return;
if(is_sid(s)){
for(int i=1;i<=cnt;i++){
int ans=total(stu[i]);
double avg=ans/4.0+EPS;
if(s==stu[i].sid)cout<<rank(stu[i])<<' '<<stu[i]<<ans<<' '<<fixed<<setprecision(2)<<avg<<'\n';
}
}
else{
for(int i=1;i<=cnt;i++){
int ans=total(stu[i]);
double avg=ans/4.0+EPS;
if(s==stu[i].name)cout<<rank(stu[i])<<' '<<stu[i]<<ans<<' '<<fixed<<setprecision(2)<<avg<<'\n';
}
}
}
}
void show_ranking(){
cout<<"Showing the ranklist hurts students' self-esteem. Don't do that."<<'\n';
}
void show_statistics(){
cout<<"Please enter class ID, 0 for the whole statistics."<<'\n';
int class_cin;
cin>>class_cin;
part_subject("Chinese",class_cin);
part_subject("Mathematics",class_cin);
part_subject("English",class_cin);
part_subject("Programming",class_cin);
over_alls(class_cin);
}
}
int main(){
for(;;){
A_Typical_Homework::greet();
int opt;
cin>>opt;
if(opt==0)break;
else if(opt==1)A_Typical_Homework::add();
else if(opt==2)A_Typical_Homework::remove();
else if(opt==3)A_Typical_Homework::query();
else if(opt==4)A_Typical_Homework::show_ranking();
else if(opt==5)A_Typical_Homework::show_statistics();
}
return 0;
}