Last active
August 29, 2015 13:56
-
-
Save jasonlvhit/9307490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*写多了Java和Python,写C++全是坑。。。。。:| */ | |
#include <cstdio> | |
#include <string> | |
#include <iostream> | |
#include <vector> | |
#include <sstream> | |
#include <cstdlib> | |
#include <cassert> | |
class Student | |
{ | |
/* data */ | |
std::string name; | |
std::string id; //学号 | |
//size_t age; | |
unsigned int age; | |
unsigned int gradeFirstYear; | |
unsigned int gradeSecondYear; | |
unsigned int gradeThirdYear; | |
unsigned int gradeFourthYear; | |
public: | |
Student(std::string p_name, unsigned int p_age, std::string p_id, | |
unsigned int p_gradeFirstYear = 0, | |
unsigned int p_gradeSecondYear = 0, | |
unsigned int p_gradeThirdYear = 0, | |
unsigned int p_gradeFourthYear = 0){ | |
name = p_name; | |
id = p_id; | |
age = p_age; | |
gradeFirstYear = p_gradeFirstYear; | |
gradeSecondYear = p_gradeSecondYear; | |
gradeThirdYear = p_gradeThirdYear; | |
gradeFourthYear = p_gradeFourthYear; | |
} | |
std::string getName(){ | |
return name; | |
} | |
std::string getId(){ | |
return id; | |
} | |
size_t getAge(){ | |
return age; | |
} | |
int averageGrade(){ | |
return (int)((gradeFourthYear + gradeThirdYear + gradeSecondYear + gradeFirstYear)/4.0); | |
} | |
}; | |
//split by delim | |
//http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c | |
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { | |
std::stringstream ss(s); | |
std::string item; | |
while (std::getline(ss, item, delim)) { | |
elems.push_back(item); | |
} | |
return elems; | |
} | |
std::vector<std::string> split(const std::string &s, char delim) { | |
std::vector<std::string> elems; | |
split(s, delim, elems); | |
return elems; | |
} | |
//string -> int | |
size_t trans(const std::string s){ | |
const char *p; | |
p = s.c_str(); | |
return atoi(p); | |
} | |
int main(){ | |
unsigned int age; | |
unsigned int gradeFirstYear; | |
unsigned int gradeSecondYear; | |
unsigned int gradeThirdYear; | |
unsigned int gradeFourthYear; | |
std::string input; | |
std::cin >> input; | |
std::vector<std::string> x = split(input,','); | |
assert(x.size() > 6); | |
std::string name = x[0]; | |
assert((age = trans(x[1])) > 0); | |
std::string id = x[2]; | |
assert((gradeFirstYear = trans(x[3])) >= 0); | |
assert((gradeSecondYear = trans(x[4])) >= 0); | |
assert((gradeThirdYear = trans(x[5])) >= 0); | |
assert((gradeFourthYear = trans(x[6])) >= 0); | |
Student anonymous(name, age, id, gradeFirstYear, gradeSecondYear,gradeThirdYear, gradeFourthYear); | |
std::cout << anonymous.getName() << ',' << anonymous.getAge() << ',' << anonymous.getId() << ',' << anonymous.averageGrade(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment