Skip to content

Instantly share code, notes, and snippets.

@techlarry
Created November 11, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techlarry/9edd42ac5d54defbba6330db5d047063 to your computer and use it in GitHub Desktop.
Save techlarry/9edd42ac5d54defbba6330db5d047063 to your computer and use it in GitHub Desktop.
描述
在一个学生信息处理程序中,要求实现一个代表学生的类,并且所有成员变量都应该是私有的。
输入
姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。
其中姓名、学号为字符串,不含空格和逗号;年龄为正整数;成绩为非负整数。
各部分内容之间均用单个英文逗号","隔开,无多余空格。
输出
一行,按顺序输出:姓名,年龄,学号,四年平均成绩(向下取整)。
各部分内容之间均用单个英文逗号","隔开,无多余空格。
样例输入
```
Tom,18,7817,80,80,90,70
```
样例输出
```
Tom,18,7817,80
```
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class student{
public:
student(string _name, int _age, string _id, int a1, int a2, int a3, int a4):
name(_name), age(_age), id(_id), gpa1(a1), gpa2(a2), gpa3(a3), gpa4(a4)
{average = (gpa1+gpa2+gpa3+gpa4)/4;}
void print();
private:
string name; //姓名
int age; //年龄
string id; //学号
int gpa1; //第1学年平均成绩
int gpa2; //第2学年平均成绩
int gpa3; //第3学年平均成绩
int gpa4; //第4学年平均成绩
int average; // average gpa
};
void student::print()
{
cout << name << "," << age << "," << id << "," << average << endl;
}
int main()
{
vector<student> s;
string line;
while (getline(cin, line))//ctrl-D结束
{
string name; //姓名
int age; //年龄
string id; //学号
int gpa1; //第1学年平均成绩
int gpa2; //第2学年平均成绩
int gpa3; //第3学年平均成绩
int gpa4; //第4学年平均成绩
int average; // average gpa
//preprocessin input
string item, token;
string delim=",";
size_t pos = 0;
while( (pos = line.find(delim))!=string::npos)
{
token = line.substr(0,pos);
item += " ";
item += token;
line.erase(0, pos+delim.length());
}
istringstream record(item);
record >> name >> age >> id >> gpa1 >> gpa2 >> gpa3 >> gpa4;
student a(name, age, id, gpa1, gpa2, gpa3, gpa4);
s.push_back(a);
a.print();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment