Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Tuesday, May 28, 2013

uva 450 - Little Black Book

  1  /*
  2  Problem link
  3  Type: Adhoc - Sorting
  4  Algorithm:
  5      Just use any kind of sort to sort according to last name
  6      I use C++ STL in this code:
  7          To use C++ STL sort on a user defined object (i.e Person)
  8          we must implement the overloaded operator functions
  9          or define our compare function.
 10  
 11      Overloaded operator functions:
 12          These are functions that defines a particular operator for
 13          your own objects (+, -, *, /, >, <, =). The syntax for
 14          overloaded operator is:
 15      <return type> operator<operator> (const ObjectType& anObject) const;
 16  
 17          The function should be const since it won't change the
 18          object it is called.
 19          If you don't declare it as a const, you cannot use the
 20          operator on const object.
 21  
 22      The input reading of this problem is a little bit tricky
 23      if you're not familiar with C++ string object methods.
 24  */
 25  #include <iostream>
 26  #include <cstdio>
 27  #include <algorithm>
 28  #include <cstring>
 29  #include <string>
 30  #include <vector>
 31  
 32  using namespace std;
 33  
 34  class Person
 35  {
 36  public:
 37      string Title,
 38             FirstName,
 39             LastName,
 40             Department,
 41             StAddress,
 42             HPhone,
 43             WPhone,
 44             Mail;
 45  
 46      Person(string Ti, string FName, string LName, string Dep, string Add, string HP, string WP, string M):
 47          Title(Ti),
 48          FirstName(FName),
 49          LastName(LName),
 50          Department(Dep),
 51          StAddress(Add),
 52          HPhone(HP),
 53          WPhone(WP),
 54          Mail(M) {}
 55  
 56      bool operator<(const Person& aPerson) const         // <-- overloaded operator <
 57      {
 58          return this->LastName < aPerson.LastName;
 59      }
 60  
 61      bool operator>(const Person& aPerson) const         // <-- overloaded operator >
 62      {
 63          return this->LastName > aPerson.LastName;
 64      }
 65  };
 66  
 67  int n;
 68  vector<Person> a;
 69  
 70  void ReadFile();
 71  void Print(Person aPerson);
 72  
 73  int main()
 74  {
 75      ReadFile();
 76  
 77      sort(a.begin(), a.end());        // This will automatically sort in ascending order
 78      for (unsigned int i = 0; i < a.size(); i++) Print(a[i]);
 79      return 0;
 80  }
 81  
 82  void ReadFile()
 83  {
 84      cin >> n;
 85      string line;
 86      getline(cin,line);
 87  
 88      for (int i = 1; i <= n; i++)
 89      {
 90          getline(cin,line);
 91          string dep = line;
 92          while (true)
 93          {
 94              getline(cin,line);
 95              if (line == "") break;
 96  
 97              unsigned int cur = 0, pre = 0;
 98              cur = line.find(",", pre);
 99              string title = "";
100              for (unsigned int i = pre; i < cur; i++) title += line[i];
101              pre = cur+1;
102  
103              cur = line.find(',', pre);
104              string FName = "";
105              for (unsigned int i = pre; i < cur; i++) FName += line[i];
106              pre = cur+1;
107  
108              cur = line.find(',', pre);
109              string LName = "";
110              for (unsigned int i = pre; i < cur; i++) LName += line[i];
111              pre = cur+1;
112  
113              cur = line.find(',', pre);
114              string Add = "";
115              for (unsigned int i = pre; i < cur; i++) Add += line[i];
116              pre = cur+1;
117  
118              cur = line.find(',', pre);
119              string HP = "";
120              for (unsigned int i = pre; i < cur; i++) HP += line[i];
121              pre = cur+1;
122  
123              cur = line.find(',', pre);
124              string WP = "";
125              for (unsigned int i = pre; i < cur; i++) WP += line[i];
126              pre = cur+1;
127  
128              string Mail = "";
129              for (unsigned int i = pre; i < line.length(); i++) Mail += line[i];
130              a.push_back(Person(title,FName,LName,dep,Add,HP,WP,Mail));
131          }
132      }
133  }
134  
135  void Print(Person aPerson)
136  {
137      cout << "----------------------------------------" << endl;
138      cout << aPerson.Title << " " << aPerson.FirstName << " " << aPerson.LastName << endl;
139      cout << aPerson.StAddress << endl;
140      cout << "Department: " << aPerson.Department << endl;
141      cout << "Home Phone: " << aPerson.HPhone << endl;
142      cout << "Work Phone: " << aPerson.WPhone << endl;
143      cout << "Campus Box: " << aPerson.Mail << endl;
144  }

10 comments:

  1. Excuse me.
    Do you know how to read file by this code?
    I want to try to make a file named "data.txt" which contains input data

    2
    English Department
    Dr.,Tom,Davis,Anystreet USA,555-2832,555-2423,823
    Mrs.,Jessica,Lembeck,Center Street,555-2543,555-8584,928
    Computer Science
    Mr.,John,Euler,East Pleasure,555-1432,555-2343,126
    Thank you :)

    ReplyDelete
    Replies
    1. Hi, before ReadFile() in main(). add freopen("data.txt","r",stdin);

      Delete
    2. http://imgur.com/2lvoZ5Z
      http://imgur.com/x942c4x
      I did what you taught me to do,and......
      Can you tell me what's wrong is it?

      Delete
    3. Can you show me the error? I cannot tell by looking at these...

      Have you include ?

      Do you have your input file in the correct directory?

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. well,this is my latest code.
    http://codepad.org/Gaf5ACuw

    I have included ,and freopen("hw1.txt","r",stdin); fclose (stdin);
    I have exchanged all of cin to stdin.
    And I got some error messages:
    63 11 C:\Users\Vicky Chen\Desktop\C++ SUMMER\lbb3.cpp [Error] invalid operands of types 'FILE* {aka _iobuf*}' and 'int' to binary 'operator>>'
    65 23 C:\Users\Vicky Chen\Desktop\C++ SUMMER\lbb3.cpp [Error] no matching function for call to 'getline(FILE*, std::string&)'
    69 24 C:\Users\Vicky Chen\Desktop\C++ SUMMER\lbb3.cpp [Error] no matching function for call to 'getline(FILE*, std::string&)'
    73 31 C:\Users\Vicky Chen\Desktop\C++ SUMMER\lbb3.cpp [Error] no matching function for call to 'getline(FILE*, std::string&)'

    I really need your help,thank you.
    I am a new coding learner,and after a semester of c++ codeing class,I think I am still know little about how to codeing.
    Thanks again QAQ

    ReplyDelete
    Replies
    1. you don't need to change "cin" to "stdin". Just add freopen("hw1.txt","r",stdin); at the beginning of main() function, that's all you need

      Delete
  6. I try your method,there are no error!!
    However,it stop working again......
    http://imgur.com/nCiN8zp

    ReplyDelete