Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created October 22, 2011 07:11
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 torazuka/1305728 to your computer and use it in GitHub Desktop.
Save torazuka/1305728 to your computer and use it in GitHub Desktop.
Chapter15_drill_PPPC++
#include "../../std_lib_facilities.h"
struct Person{
Person(string first_name, string second_name, int age);
Person(string name, int age);
string first_name() const { return first_n; }
string second_name() const { return second_n; }
string name() const;
int age() const { return a; }
private:
string first_n;
string second_n;
int a;
};
string Person::name() const
{
string result = "";
if(0 < first_n.length()){
result += first_n;
}
if(0 < second_n.length()){
if(0 < first_n.length()){
result += ' ';
}
result += second_n;
}
return result;
}
bool isValidAge(int age)
{
bool result = false;
if((age < 0 || 150 < age) == false){
result = true;
}
return result;
}
vector<char> get_error_name()
{
vector<char> tb;
tb.push_back('"');
tb.push_back('\'');
tb.push_back('[');
tb.push_back(']');
tb.push_back('*');
tb.push_back('&');
tb.push_back('^');
tb.push_back('%');
tb.push_back('$');
tb.push_back('#');
tb.push_back('@');
tb.push_back('!');
return tb;
}
bool isValidName(string name)
{
bool result = false;
// 名無しを通す
if(name.length() == 0){
result = true;
return result;
}
vector<char> error_name = get_error_name();
for(int i = 0; i < name.length(); ++i){
for(int j = 0; j < error_name.size(); ++j){
if(name[i] != error_name[j]){
result = true;
} else {
result = false;
break;
}
}
if(result == false){
break;
}
}
return result;
}
Person::Person(string first_name, string second_name, int age)
{
if(isValidAge(age)){
a = age;
} else {
error("age error");
}
if(first_name.length() + second_name.length() == 0){
error("name error");
} else {
if(isValidName(first_name)){
first_n = first_name;
} else {
error("first_name error");
}
if(isValidName(second_name)){
second_n = second_name;
} else {
error("second_name error");
}
}
}
Person::Person(string name, int age)
{
if(isValidAge(age)){
a = age;
} else {
error("age error");
}
if(name.length() != 0 || isValidName(name)){
first_n = name;
} else {
error("name error");
}
}
istream& operator>>(istream& is, Person& p)
{
char ch1, ch2;
string f_n;
string s_n;
int age;
cout << "first_name: ";
is >> f_n;
if(!is){
return is;
}
cout << "second_name: ";
is >> s_n;
if(!is){
return is;
}
cout << "age: ";
is >> age;
if(!is){
return is;
}
// first_nameとsecond_nameの片方さえあればokにしたいんだけど
// そもそも「入力しないことを許す」方法がよくワカラン・・・
if(f_n.length() + s_n.length() == 0){
error("name error");
} else {
if(f_n.length() == 0){
Person temp("", s_n, age);
p = temp;
} else if (s_n.length() == 0) {
Person temp(f_n, "", age);
p = temp;
} else {
Person temp(f_n, s_n, age);
p = temp;
}
}
return is;
}
ostream& operator<<(ostream& os, const Person& p)
{
return os << p.name() << " : " << p.age();
}
int main(){
Person p("Goofy", 63);
cout << p << endl;
Person p2(" ", 0);
cin >> p2;
cout << p2 << endl;
vector<Person> persons;
Person temp(" ", 0);
while(cin >> temp){
persons.push_back(temp);
}
for(int i = 0; i < persons.size(); ++i){
cout << persons[i] << endl;
}
keep_window_open();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment