Family Tree


General Statement: Given the family relationships below, output all persons who fit the input characteristic. These characteristics are SIBLING, PARENT, CHILD, GRANDPARENT, and GRANDCHILD.

Family #1: Ann and Marty have children Bill, Cathy, and Frank.
Bill and Alice have no children.
Cathy and Don have children Matt and Sally.
Frank and Jill have child Sarah.

Family #2: Debbie and Phil have children Jill and Betty.
Jill and Frank have child Sarah.
Betty and Paul have children Mary, Jane, and Bart.

NOTE: The 2 families are connected by the marriage of Frank and Jill.

Input: The first line of the data set for this problem is an integer that represents the number of data sets that follow. The first word in the data set is the relationship, and the second is the name of the person whose relative(s) is/are to be located.

Output: Output the names of all who meet the relationship criterion.
All letters are upper case.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.

Sample Input:
3
SIBLING BART
PARENT BETTY
GRANDCHILD MARTY

Sample Output:
MARY JANE
DEBBIE PHIL
MATT SALLY SARAH

Solutions :

#include <iostream>
using namespace std;

void sibling (string name)
{
    if ( name == "BILL" )
        cout << "CATHY FRANK";
    else if ( name == "CATHY" )
        cout << "BILL FRANK";
    else if ( name == "FRANK" )
        cout << "BILL CATHY";

    else if ( name == "MATT" )
        cout << "SALLY";
    else if ( name == "SALLY" )
        cout << "MATT";

    else if ( name == "JILL" )
        cout << "BETTY";
    else if ( name == "BETTY" )
        cout << "JILL";

    else if ( name == "MARY" )
        cout << "JANE BART";
    else if ( name == "JANE" )
        cout << "MARY BART";
    else if ( name == "BART" )
        cout << "MARY JANE";

    cout << endl;
}

void parent (string name)
{
    if ( name == "BILL" || name == "CATHY" || name == "FRANK" )
        cout << "ANN MARTY";

    else if ( name == "JILL" || name == "BETTY" )
        cout << "DEBBIE PHIL";

    else if ( name == "MATT" || name == "SALLY" )
        cout << "CATHY DON";

    else if ( name == "SARAH" )
        cout << "FRANK JILL";

    else if ( name == "MARY" || name == "JANE" || name == "BART" )
        cout << "BETTY PAUL";

    cout << endl;
}

void child (string name)
{
    if ( name == "ANN" || name == "MARTY" )
        cout << "BILL CATHY FRANK";

    else if ( name == "CATHY" || name == "DON" )
        cout << "MATT SALLY";

    else if ( name == "FRANK" || name == "JILL" )
        cout << "SARAH";

    else if ( name == "DEBBIE" || name == "PHIL" )
        cout << "JILL BATTY";

    else if ( name == "BETTY" || name == "PAUL" )
        cout << "MARY JANE BART";

    cout << endl;
}

void grandparent (string name)
{
    if ( name == "MATT" || name == "SALLY" || name == "SARAH" )
        cout << "ANN MARTY";

    else if ( name == "MARY" || name == "JANE" || name == "BART" )
        cout << "DEBBIE PHIL";

    cout << endl;
}

void grandchild (string name)
{
    if ( name == "ANN" || name == "MARTY" )
        cout << "MATT SALLY SARAH";

    else if ( name == "DEBBIE" || name == "PHIL" )
        cout << "MARY JANE BART";

    cout << endl;
}

int main ()
{
    int dataset;
    cin >> dataset;
    getchar ();

    while ( dataset-- ) {
        string relation;
        cin >> relation;

        string name;
        cin >> name;

        if ( relation == "SIBLING" )
            sibling (name);

        else if ( relation == "PARENT" )
            parent (name);

        else if ( relation == "CHILD" )
            child (name);

        else if ( relation == "GRANDPARENT" )
            grandparent (name);

        else
            grandchild (name);
    }

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s