Advertisement
Guest User

Untitled

a guest
Dec 5th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 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.     in >> tmp;
  118.  
  119.     // Did we reach the end of the stream?
  120.     if(!in) {
  121.         return nullptr;
  122.     }
  123.  
  124.     if (tmp == "<person") {
  125.         in >> tmp;
  126.         while (tmp != ">") {
  127.             if (tmp == "S") {
  128.                 in >> surname;
  129.             } else if (tmp == "N") {
  130.                 in >> fname;
  131.             } else if (tmp == "T") {
  132.                 in >> telephone;
  133.             } else if (tmp == "E") {
  134.                 in >> email;
  135.             } else {
  136.                 std::cerr << "Unknown token: " << tmp << "\n";
  137.                 return nullptr;
  138.             }
  139.             in >> tmp;
  140.         }
  141.     } else {
  142.         std::cerr << "Bad starting sequence: " << tmp << std::endl;
  143.         return nullptr;
  144.     }
  145.  
  146.     if (email != "" && telephone != "") {
  147.         return new Person_with_telephone_and_email(surname, fname, telephone, email);
  148.     } else if (email != "") {
  149.         return new Person_with_email(surname, fname, email);
  150.     } else if (telephone != "") {
  151.         return new Person_with_telephone(surname, fname, telephone);
  152.     }
  153.     return new Person(surname, fname);
  154. }
  155.  
  156. int main() {
  157.     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 >");
  158.  
  159.     std::vector<Person*> people;
  160.     std::string tmp;
  161.  
  162.     while(Person* p = read_person(ss)) {
  163.         people.push_back(p);
  164.     }
  165.  
  166.     for (auto &p : people) {
  167.         p->print(std::cout);
  168.         std::cout << std::endl;
  169.     }
  170.  
  171.     for (auto &p : people) {
  172.         delete p;
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement