7-8 Score Processing (10分)
Write a program to process students score data.
The input of your program has lines of text, in one of the two formats:
- Student’s name and student id, as
<student id>, <name>
, and - Score for one student of one course, as
<student id>, <course name>, <marks>.
Example of the two formats are:
3190101234, Zhang San
3190101111, Linear Algebra, 89.5
Comma is used as the seperator of each field, and will never be in any of the fields. Notice that there are more than one word for name of the person and name of the course. To make your code easier, the score can be treated as double.
The number of the students and the number of the courses are not known at the beginning. The number of lines are not known at the beginning either. The lines of different format appear in no order. One student may not get enrolled in every course.
Your program should read every line in and print out a table of summary in .csv format.
The first line of the output is the table head, consists fields like this:
student id, name, <course name 1>, <course name 2>, ..., average
where the course names are all the courses read, in alphabet order. There should be one space after each comma.
Then each line of the output is data for one student, in the ascended order of their student id, with score of each course, like: 3190101234, Zhang San, 85.0, , 89.5, , , 87.3
For the course that hasn’t been enrolled, leave a blank before the comma, and should not get included in the average. The average has one decimal place. There should be one space after each comma.
And the last line of the output is a summary line for average score of every course, like:, , 76.2, 87.4, , , 76.8
All the number output, including the averages have one decimal place.
Input Format
As described in the text above.
Output Format
As described in the text above. The standard output is generated by a program compiled by gcc, that the round of the first decimal place is in the “gcc way”.
Sample Input
3180111435, Operating System, 34.5
3180111430, Linear Algebra, 80
3180111435, Jessie Zhao
3180111430, Zhiwen Yang
3180111430, Computer Architecture, 46.5
3180111434, Linear Algebra, 61.5
3180111434, Anna Teng
Sample Output
student id, name, Computer Architecture, Linear Algebra, Operating System, average
3180111430, Zhiwen Yang, 46.5, 80.0, , 63.2
3180111434, Anna Teng, , 61.5, , 61.5
3180111435, Jessie Zhao, , , 34.5, 34.5
, , 46.5, 70.8, 34.5, 50.6
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
using namespace std;
class student{
public:
string id, name;
map<string, double> course;
student() : id(""), name("") {
course.clear();
}
void set1(string id_, string name_) {
id = id_;
name = name_;
}
void set2(string a, double b) {
course[a] = b;
}
bool operator < (const student& tmp) const {
return id < tmp.id;
}
};
int main() {
string in;
set<string> s;
vector<student> res;
map<string, int> mp;
while (getline(cin, in)) {
int n = 0;
for (int i = 0; i < in.size(); i++) {
if (in[i] == ',') {
n++;
}
}
switch (n) {
case 1: {
int ad = -1;
for (int i = 0; i < in.size(); i++) {
if (in[i] == ',') {
ad = i;
break;
}
}
string id, name;
id = in.substr(0, ad);
name = in.substr(ad+2);
if (!mp.count(id)) {
mp[id] = res.size();
student tmp;
tmp.set1(id, name);
res.push_back(tmp);
} else {
ad = mp[id];
res[ad].set1(id, name);
}
break;
}
case 2: {
int ad1 = -1, ad2 = -1;
for (int i = 0; i < in.size(); i++) {
if (in[i] == ',') {
if (ad1 == -1) {
ad1 = i;
} else {
ad2 = i;
break;
}
}
}
string id, course, grade;
id = in.substr(0, ad1);
course = in.substr(ad1+2, ad2-ad1-2);
grade = in.substr(ad2+2);
s.insert(course);
if (!mp.count(id)) {
mp[id] = res.size();
student tmp;
tmp.set2(course, atof(grade.c_str()));
res.push_back(tmp);
} else {
int ad = mp[id];
res[ad].set2(course, atof(grade.c_str()));
}
break;
}
}
}
cout << "student id, name";
for (auto i: s) {
cout << ", " << i;
}
cout << ", average" << endl;
vector<double> sum(s.size(), 0), cnt(s.size(), 0);
sort(res.begin(), res.end());
int tot = 0;
double ave = 0;
for (int i = 0; i < res.size(); i++) {
cout << res[i].id << ", "<< res[i].name;
int ad = 0;
tot = 0;
ave = 0;
for (auto j: s) {
cout << ", ";
if (res[i].course.count(j)) {
cout << fixed << setprecision(1) << res[i].course[j];
sum[ad] += res[i].course[j];
cnt[ad]++;
ave += res[i].course[j];
tot++;
}
ad++;
}
cout << ", " << fixed << setprecision(1) << ave/tot << endl;
}
cout << ", ";
tot = 0;
ave = 0;
for (int i = 0; i < sum.size(); i++) {
tot++;
ave += sum[i]/cnt[i];
cout << ", " << fixed << setprecision(1) << sum[i]/cnt[i];
}
cout << ", " << fixed << setprecision(1) << ave/tot << endl;
return 0;
}