namesdemo.cpp

/***************************************************************************
 * nameservercpp  -  Program to serve of last name statistics
 *
 * copyright : (C) 2018 by Jim Skon, Kenyon College
 *
 * This program create an index US Census name
 * Data on the frequency of names in response to requestes.  
 * It then allows you to look up any name, giving the 10 closest matches
 * 
 * 
 *
 ***************************************************************************/
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include "NameMap.h"
#include "NameEntry.h"

using namespace std;

const string path = "/home/class/SoftDev/namedata/";
const string lastFile = path+"dist.all.last";
const string maleFile = path+"dist.male.first";
const string femaleFile = path+"dist.female.first";


/* Server main line,create name MAP, wait for and serve requests */
int main() {
    // Build the name maps
    NameMap lastMap(lastFile);
    NameMap femaleMap(femaleFile);
    NameMap maleMap(maleFile);
    vector<NameEntry> matchList;

    NameEntry entry;
    string name;
    char sel;
    // Check for map creation success
    if (!lastMap.NameMapSuccess() || !femaleMap.NameMapSuccess() || !maleMap.NameMapSuccess()) {
        cout << "failure to load name data!" << endl;
        exit(1);
    }
    cout << "Name data loaded!" << endl;
    cout << "Look up US name statistics" << endl;
    /* See what user wants */
    do {

        cout << "Select one: " << endl << "  1. A Last name" << endl
                << "  2. Female first names" << endl << "  3. Male First Names" << endl
                << "  0. End Program." << endl << "Select:";
        cin >> sel;

        if (sel == '1' || sel == '2' || sel == '3') {

            cout << "Ender name to check:";
            cin >> name;

            /* Convert verse to upper case*/
            transform(name.begin(), name.end(), name.begin(), ::toupper);

            // Try to find the record
            switch (sel) {
                case '1':
                    matchList = lastMap.getMatches(name);
                    break;
                case '2':
                    matchList = femaleMap.getMatches(name);
                    break;
                case '3':
                    matchList = maleMap.getMatches(name);
                    break;
                default:
                    cout << "Invalid option \"" << sel << "\"." << endl; 
            }
 
            cout << "10 best matches:" << endl;
            cout << "Name\t%\tRank" << endl;
            for (int i = 0; i < matchList.size(); i++) {
                cout << matchList.at(i).name << "\t" <<  matchList.at(i).percent << "\t" << matchList.at(i).rank << endl;
            }

        }
    } while (sel != '0');
}

NameEntry.cpp

/***************************************************************************
 * NameEntry.cpp  -  Object to store name data for a single name
 *
 * copyright : (C) 2018 by Jim Skon, Kenyon College 
 *                                                          
 * This is part of a program create an index US Census name
 * Data on the frequency of names in response to requestes.  
 * It then allows you to look up any name, giving the 10 closest matches
 *                                                                                                                          
 * ***************************************************************************/
#include "NameEntry.h"

NameEntry::NameEntry() {
    name = ""; // Last name
    percent = ""; // Frequency of occurrence of a given name
    cumulative = ""; // cumulative frequency of all name up to and including this name
    rank = "";
}

NameEntry.h

/***************************************************************************
 * NameEntry.h  -  Object to store name data for a single name
 *
 * copyright : (C) 2018 by Jim Skon, Kenyon College
 *
 * This is part of a program create an index US Census name
 * Data on the frequency of names in response to requestes.  
 * It then allows you to look up any name, giving the 10 closest matches
 * 
 * 
 *
 ***************************************************************************/
#ifndef NAMEENTRY_H
#define NAMEENTRY_H
#include <string>

using namespace std;

class NameEntry {
public:
    NameEntry();
    string name; // Last name
    string percent; // Frequency of occurrence of a given name
    string cumulative; // cumulative frequency of all name up to and including this name
    string rank; // Rank of this Name in terms of frequency
private:

};

#endif /* NAMEENTRY_H */

NameMap.cpp

/***************************************************************************
 * NameMap.cpp - Object to store a map of all names and data about that name, 
 * and allow insertion and and looking up of names.
 *
 * copyright : (C) 2018 by Jim Skon, Kenyon College
 *
 * This is part of a program create an index US Census name
 * Data on the frequency of names in response to requestes.  
 * It then allows you to look up any name, giving the 10 closest matches
 * 
 ***************************************************************************/
#include "NameMap.cpp"

NameMap::NameMap(string filename) {

    NameEntry nameData;
    fstream infile(filename);
    if (infile.good()) {
        while (1) {
            infile >> nameData.name;
            infile >> nameData.percent;
            infile >> nameData.cumulative;
            infile >> nameData.rank;
            if (infile.fail()) break;
            nameMap[nameData.name] = nameData;
        }
        infile.close();
        success = true;
    } else {
        success = false;
    }
}

bool NameMap::NameMapSuccess() {
    return success;
}

vector<NameEntry> NameMap::getMatches(string name) {
    vector<NameEntry> matchList;
    NameEntry match;
    // Look up the closest lower match
    map<string, NameEntry>::iterator it = nameMap.lower_bound(name);

    // back up (up to) 5 places
    for (int i = 0; i < 5 && (it != nameMap.begin()); i++) {
        it--;
    }

    // Get (up to) 10 results
    for (int i = 0; i < 10 && (it != nameMap.end()); i++) {
        
        match = (*it).second;
        matchList.push_back(match);
        it++;
    }
    return matchList;;
}

NameMap.h

/***************************************************************************
 * NameMap.h - Object to store a map of all names and data about that name
 *
 * copyright : (C) 2018 by Jim Skon, Kenyon College
 *
 * This is part of a program create an index US Census name
 * Data on the frequency of names in response to requestes.  
 * It then allows you to look up any name, giving the 10 closest matches
 * 
 * 
 *
 ***************************************************************************/
#ifndef NAMEMAP_H
#define NAMEMAP_H
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <vector>
#include "NameEntry.h"

using namespace std;

class NameMap {
public:
    NameMap(string filename);
    bool NameMapSuccess();
    vector<NameEntry> getMatches(string name);

private:
    map<string, NameEntry> nameMap;
    
    bool success;
};

#endif /* NAMEMAP_H */

Makefile

# MakeFile to build the Sample US CENSUS Name Data lookup program 
# For CSC3004 Software Development
# C++ compiler to use
CC= g++

#For Optimization
#CFLAGS= -O2
#For debugging
CFLAGS= -std=c++14

RM= /bin/rm -f

all: namesdemo

NameEntry.o: NameEntry.cpp NameEntry.h
	$(CC) $(CFLAGS) NameEntry.cpp -c

NameMap.o: NameMap.cpp NameMap.h NameEntry.h
	$(CC) $(CFLAGS) NameMap.cpp -c

namesdemo.o: namesdemo.cpp NameEntry.h NameMap.h
	$(CC) $(CFLAGS) namesdemo.cpp -c

namesdemo: namesdemo.o NameEntry.o NameMap.o
	$(CC) $(CFLAGS) namesdemo.o NameEntry.o NameMap.o -o namesdemo

clean:
	rm -f *.o  namesdemo
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2018-01-19 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback