2012-07-11 06:30:53Morris

[UVA] 450 - Little Black Book


 Little Black Book 

The Public Relations Office is looking for an easier way to compile the information for their Black Book, a listing of all faculty members. Currently, each department types up a list of their faculty and submits the list to PR. PR then takes all the lists and makes a combined list that is sorted alphabetically by last name. So far they have been doing this task by hand, which takes far more time than the average PR employee has to spare!

What PR is looking for is a program that will take these faculty listings and combine them in a certain format that PR can use for inclusion in the Black Book. All the lists are stored in text files. Each department's file is sorted by last name. The program should take these individually sorted files and combine them into a single sorted file in the format shown below.

Input

The input file will contain, on the first line, a number (between 2 and 12) that reports the number of departments that your program should sort and write to the output file. Following the first line will be a series of sets of lines. The first line of each set contains a department title, and following lines contain the data to be sorted.

The information reported is in this order: Title, First Name, Last Name, Street Address, Home Phone, Work Phone, and Campus Mailbox. The information is delimited with commas.

A blank line separates two sets of lines. There are the same number of department titles and sets of data as the number on the first line reports.

Output

The format of each record held in the output file should be as follows:

--------------------

<Title> <FirstName> <LastName>

<HomeAddress>

Department: <Department>

Home Phone: <HomePhone>

Work Phone: <WorkPhone>

Campus Box: <CampusBox>

The dashed line should be shown at the top of each record. The characters "<" and ">" show where a field should be placed. Please pay attention to spacing.

You may assume that all input files will be in the proper syntax, with no extra spaces. PR therefore expects that the data they are requesting will be in the proper syntax.

Sample Input

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

Sample Output

----------------------------------------
Dr. Tom Davis
Anystreet USA
Department: English Department 
Home Phone: 555-2832
Work Phone: 555-2423
Campus Box: 823
----------------------------------------
Mr. John Euler
East Pleasure
Department: Computer Science 
Home Phone: 555-1432
Work Phone: 555-2343
Campus Box: 126
----------------------------------------
Mrs. Jessica Lembeck
Center Street
Department: English Department
Home Phone: 555-2543
Work Phone: 555-8584
Campus Box: 928

當來練字串函數, 其實我不怎麼熟, 因此要多練習,
利用 string.find 去找尋下一次的切割地點,
如果找不到回傳 npos, 判斷用 == string::npos

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
using namespace std;
struct Info {
    string d[8];
};
int main() {
    int t;
    string depart, info;
    map<string, Info> r;
    cin >> t;
    getchar();
    while(t--) {
        getline(cin, depart);
        while(getline(cin, info)) {
            if(info.length() == 0)
                break;
            int pos_1 = 0, pos_2 = info.find(",");
            int idx = 0;
            Info p;
            while(1) {
                string sub = info.substr(pos_1, pos_2-pos_1);
                p.d[idx++] = sub;
                pos_1 = pos_2 + 1;
                pos_2 = info.find(",", pos_2 + 1);
                if(pos_2 == string::npos) {
                    sub = info.substr(pos_1, pos_2-pos_1);
                    p.d[idx++] = sub;
                    break;
                }
            }
            p.d[7] = depart;
            r[p.d[2]] = p;
        }
    }
    for(map<string, Info>::iterator i = r.begin(); i != r.end(); i++) {
        cout << "----------------------------------------" << endl;
        Info p = i->second;
        cout << p.d[0] << " " << p.d[1] << " " << p.d[2] << endl;
        cout << p.d[3] << endl;
        cout << "Department: " << p.d[7] << endl;
        cout << "Home Phone: " << p.d[4] << endl;
        cout << "Work Phone: " << p.d[5] << endl;
        cout << "Campus Box: " << p.d[6] << endl;
    }
    return 0;
}