Advertisement
Guest User

Untitled

a guest
Dec 5th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. class Person {
  7. protected:
  8.     std::string m_Name, m_Surname;
  9.  
  10. public:
  11.     Person() {
  12.     }
  13.     Person(std::string &s, std::string &f) :
  14.             m_Name(f), m_Surname(s) {
  15.     }
  16.  
  17.     void set_name(std::string &f) {
  18.         m_Name = f;
  19.     }
  20.  
  21.     void set_surname(std::string &s) {
  22.         m_Surname = s;
  23.     }
  24.  
  25.     const std::string& get_name() const {
  26.         return m_Name;
  27.     }
  28.  
  29.     const std::string& get_surname() const {
  30.         return m_Surname;
  31.     }
  32.  
  33.     virtual void print(std::ostream &o) const {
  34.         o << "<person" << " S " << m_Surname << " N " << m_Name << " >";
  35.     }
  36.  
  37.     virtual ~Person() {}
  38. };
  39.  
  40. class Person_with_telephone: public virtual Person {
  41. protected:
  42.     std::string m_Telephone;
  43.  
  44. protected:
  45.     Person_with_telephone(const std::string &telephone) :
  46.             m_Telephone(telephone) {
  47.     }
  48.  
  49. public:
  50.     Person_with_telephone() {
  51.     }
  52.     Person_with_telephone(std::string &s, std::string &f, std::string &telephone) :
  53.             Person(s, f), m_Telephone(telephone) {
  54.     }
  55.  
  56.     void set_telephone(std::string &telephone) {
  57.         m_Telephone = telephone;
  58.     }
  59.  
  60.     const std::string& get_telephone() const {
  61.         return m_Telephone;
  62.     }
  63.  
  64.     virtual void print(std::ostream &o) const override {
  65.         o << "<person" << " S " << m_Surname << " N " << m_Name
  66.               << " T " << m_Telephone << " >";
  67.     }
  68. };
  69.  
  70. class Person_with_email: public virtual Person {
  71. protected:
  72.     std::string m_Email;
  73.  
  74. protected:
  75.     Person_with_email(const std::string &email) :
  76.             m_Email(email) {
  77.     }
  78.  
  79. public:
  80.     Person_with_email() {
  81.     }
  82.     Person_with_email(std::string &s, std::string &f, std::string &email) :
  83.             Person(s, f), m_Email(email) {
  84.     }
  85.  
  86.     void set_email(std::string &email) {
  87.         m_Email = email;
  88.     }
  89.  
  90.     const std::string& get_email() const {
  91.         return m_Email;
  92.     }
  93.  
  94.     virtual void print(std::ostream &o) const override {
  95.         o << "<person" << " S " << m_Surname << " N " << m_Name << " E " << m_Email << " >";
  96.     }
  97. };
  98.  
  99. class Person_with_telephone_and_email: public virtual Person_with_telephone,
  100.         public virtual Person_with_email {
  101. public:
  102.     Person_with_telephone_and_email() {
  103.     }
  104.     Person_with_telephone_and_email(std::string &s, std::string &f, std::string &telephone,
  105.             std::string &email) :
  106.             Person(s, f), Person_with_telephone(telephone), Person_with_email(email) {
  107.     }
  108.  
  109.     virtual void print(std::ostream &o) const override {
  110.         o << "<person" << " S " << m_Surname << " N " << m_Name
  111.           << " T " << m_Telephone << " E " << m_Email << " >";
  112.     }
  113. };
  114.  
  115. Person* read_person(std::istream &in) {
  116.     std::string tmp, fname, surname, email, telephone;
  117.  
  118.     in >> tmp;
  119.  
  120.     if (tmp == "<person") {
  121.         in >> tmp;
  122.         while (tmp != ">") {
  123.             if (tmp == "S") {
  124.                 in >> surname;
  125.             } else if (tmp == "N") {
  126.                 in >> fname;
  127.             } else if (tmp == "T") {
  128.                 in >> telephone;
  129.             } else if (tmp == "E") {
  130.                 in >> email;
  131.             } else {
  132.                 std::cerr << "Unknown token: " << tmp << "\n";
  133.                 return nullptr;
  134.             }
  135.             in >> tmp;
  136.         }
  137.     } else {
  138.         std::cerr << "Bad starting sequence: " << tmp << std::endl;
  139.         return nullptr;
  140.     }
  141.  
  142.     if (email != "" && telephone != "") {
  143.         return new Person_with_telephone_and_email(surname, fname, telephone, email);
  144.     } else if (email != "") {
  145.         return new Person_with_email(surname, fname, email);
  146.     } else if (telephone != "") {
  147.         return new Person_with_telephone(surname, fname, telephone);
  148.     }
  149.     return new Person(surname, fname);
  150. }
  151.  
  152. int main() {
  153.     std::stringstream ss("<person S Smith N Tom >\n<person S Smith N Dick T +49.921.1434 >\n<person S Smith N Harry E hsmith@gmail.com >\n<person S Smith N Mary T +39.921.1434 E msmith@gmail.com >\n<person S John N John T +33.921.1434 E jjohn@gmail.com >");
  154.  
  155.     std::vector<Person*> people;
  156.     std::string tmp;
  157.     std::stringstream stream;
  158.  
  159.     while(std::getline(ss, tmp)) {
  160.         stream.clear();
  161.         stream.str(tmp);
  162.         people.push_back(read_person(stream));
  163.     }
  164.  
  165.     for (auto &p : people) {
  166.         p->print(std::cout);
  167.         std::cout << std::endl;
  168.     }
  169.  
  170.     for (auto &p : people) {
  171.         delete p;
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement