2013年9月2日

UVa 450 - Little Black Book

#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#define PB push_back
using namespace std;

struct Data {
  Data (string title,
        string firstName,
        string lastName, 
        string address,
        string department,
        string homePhone,
        string workPhone,
        string campusBox)

    :   title(title),
        firstName(firstName),
        lastName(lastName),
        address(address),
        department(department),
        homePhone(homePhone),
        workPhone(workPhone),
        campusBox(campusBox) {}

  bool operator<(const Data & other) const {
    return lastName < other.lastName;
  }
  string title, firstName, lastName;
  string address, department, homePhone, workPhone, campusBox;
};

int main() {
  vector<Data> lists;
  char temp[9999];
  gets(temp);
  int N;
  sscanf(temp, "%d", &N);
  while (N--) {
    char department[99];
    gets(department);
    while (gets(temp) && temp[0]) {
      string s(temp);
      int comma[6];
      comma[0] = s.find(',');
      for (int i = 1; i < 6; i++) {
        comma[i] = s.find(',', comma[i - 1] + 1);
      }
      lists.PB(Data(s.substr(0, comma[0]),
                    s.substr(comma[0] + 1, comma[1] - comma[0] - 1),
                    s.substr(comma[1] + 1, comma[2] - comma[1] - 1),
                    s.substr(comma[2] + 1, comma[3] - comma[2] - 1),
                    department,
                    s.substr(comma[3] + 1, comma[4] - comma[3] - 1),
                    s.substr(comma[4] + 1, comma[5] - comma[4] - 1),
                    s.substr(comma[5] + 1)));
    }
  }
  sort(lists.begin(), lists.end());
  for (int i = 0; i < lists.size(); i++) {
    Data & data = lists[i];
    puts("----------------------------------------");
    printf("%s %s %s\n", data.title.c_str(), data.firstName.c_str(), data.lastName.c_str());
    puts(data.address.c_str());
    printf("Department: %s\n", data.department.c_str());
    printf("Home Phone: %s\n", data.homePhone.c_str());
    printf("Work Phone: %s\n", data.workPhone.c_str());
    printf("Campus Box: %s\n", data.campusBox.c_str());
  }
  return 0;
}

沒有留言:

張貼留言