代码拉取完成,页面将自动刷新
/**
* @file Project1.cpp
* @author Shuang Hu <hsmath@ubuntu>
* @date Thu Mar 25 05:41:30 2021
*
* @brief A brief program to get a database of students.
*
*
*/
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
class Students{
private:
string id,name;
map<string,double> course;
double Average;
public:
Students(){
id="";
name="";
course.clear();
Average=0;
}
void info1(string _id,string _name){
id=_id;
name=_name;
}
void info2(string cname,double score){
course[cname]=score;
}
void setAvg(double sc){
Average=sc;
}
double getAvg(){
return Average;
}
string getid(){
return id;
}
map<string,double> getcourse(){
return course;
}
void Printinfo(set<string> courses);
};
void Students::Printinfo(set<string> courses){
cout<<id<<", "<<name<<", ";
for(auto j:courses){
if(course.count(j)){//course j is in the table
cout<<fixed<<setprecision(1)<<course[j];
}
cout<<", ";
}
cout<<fixed<<setprecision(1)<<Average<<endl;
}
bool cmp(Students i,Students j){
if(i.getid()<j.getid()){
return true;
}else{
return false;
}
}
int main(){
string in;
set<string> allcourse;
vector<Students> Database;
map<string,int> getindex;
while(1){
getline(cin,in);
if(in==""){
break;
}
int type=0;
for(int i=0;i<in.size();i++){//Student Name and ID?Or ID,Score and Subject?
if(in[i]==','){
type++;
}
}
if(type==1){//case1:Student name and ID
int flag=0;
for(int i=0;i<in.size();i++){
if(in[i]==','){
flag=i;
break;
}
}
string s1=in.substr(0,flag);//substring #1:Student ID;
string s2=in.substr(flag+2);//substring #2:Student Name;
if(getindex.count(s1)==0){//No such student ID!
getindex[s1]=Database.size();
Students stu;
stu.info1(s1,s2);
Database.push_back(stu);//register the student.
}else{//There exists such an ID,set his/her name
int index=getindex[s1];
Database[index].info1(s1,s2);//modify the student information
}
}
else if(type==2){//case2:ID,course and grades
int flag1,flag2;//mark the position of ','
flag1=flag2=-1;
for(int i=0;i<in.size();i++){
if(in[i]==','&&flag1==-1){
flag1=i;
}
else if(in[i]==','&&flag1!=-1){
flag2=i;
}
}
string s1=in.substr(0,flag1);//s1 marks ID
string s2=in.substr(flag1+2,flag2-flag1-2);//s2 marks the course
string s3=in.substr(flag2+2);//s3 marks the grade
allcourse.insert(s2);
if(getindex.count(s1)==0){//no such ID!
getindex[s1]=Database.size();
Students stu;
stu.info2(s2,atof(s3.c_str()));
Database.push_back(stu);
}else{//there exists such an ID!set his/her grades
int index=getindex[s1];
Database[index].info2(s2,atof(s3.c_str()));
}
}
}
cout<<"student id, name, ";
for(auto i:allcourse){
cout<<i<<", ";
}
cout<<"average"<<endl;
sort(Database.begin(),Database.end(),cmp);
for(int i=0;i<Database.size();i++){//calculate the average data
double sc=0;
int cnt=0;
for(auto j:allcourse){
if(Database[i].getcourse().count(j)){//have such a course
sc=sc+Database[i].getcourse()[j];
cnt++;
}
}
sc=sc*1.0/cnt;
Database[i].setAvg(sc);
}
for(int i=0;i<Database.size();i++){
Database[i].Printinfo(allcourse);//print the information of students.
}
cout<<", , ";
double sum=0;
int count=0;
for(auto i:allcourse){//find the average score of all the courses
double total=0;
int cnt=0;
int flag=0;//There are some students get this course
for(int j=0;j<Database.size();j++){
if(Database[j].getcourse().count(i)){//FIND the course!
flag=1;
total=total+Database[j].getcourse()[i];
cnt++;
}
}
double avg=total/cnt;
sum+=avg;
count++;
if(flag==1){
cout<<fixed<<setprecision(1)<<avg;
}
cout<<", ";
}
cout<<fixed<<setprecision(1)<<sum/count<<endl;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。