1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
int numberOfNames = 0;
class SurnameInfo
{
private:
char *name;
int counter;
float pctrace[6];
public:
char getname(){ return *name; };
int getcounter(){ return counter; };
float getpctrace(int i){ return pctrace[i]; };
void setname(char*, int);
void setcounter(int);
void setpctrace(float, int);
void printname() { cout << name; };
~SurnameInfo();
};
void SurnameInfo::setname(char * nm, int len)
{
name = new char[len + 1];
memset(name, len + 1, 0);
memcpy(name,nm,len);
name[len] = 0;
}
void SurnameInfo::setcounter(int ct)
{
counter = ct;
}
void SurnameInfo::setpctrace(float prace, int i)
{
pctrace[i] = prace;
}
SurnameInfo::~SurnameInfo()
{
delete[] name;
}
const int MAXARRAY = 151671;
SurnameInfo * surnames[MAXARRAY];
const int MAXLINE = MAXARRAY;
void processLine(char *, int);
void mostPopular(int);
void searchName();
int main()
{
char searchaname;
ifstream inputfile;
inputfile.open("names.csv");
char line[MAXLINE];
if (!inputfile) return 0;
inputfile.getline(line, MAXLINE);
inputfile.getline(line, MAXLINE);
while (!inputfile.eof())
{
processLine(line, numberOfNames++);
inputfile.getline(line, MAXLINE);
}
do
{
cout << "Do you want to search for a name? (y/n) ";
cin >> searchaname;
if (searchaname == 'y')
searchName();
}while (searchaname == 'y');
system("CLS");
cout << "Here is a list of names most common in each race: " << endl;
cout << "Press enter to continue.";
cin.clear(); cin.sync(); cin.get();
system("CLS");
cout << "Highest Population: \n\nWHITE: \n";
mostPopular(0);
cout << "\n\nBLACK:\n";
mostPopular(1);
cout << "\n\nASIAN AND PACIFIC ISLANDER: \n";
mostPopular(2);
cout << "\n\nAMERICAN INDIAN OR ALASKAN NATIVE: \n";
mostPopular(3);
cout << "\n\n2 OR MORE RACES: \n";
mostPopular(4);
cout << "\n\nHISPANIC: \n";
mostPopular(5);
inputfile.close();
cin.clear(); cin.sync(); cin.get();
return 0;
}
void processLine(char *line, int n)
{
surnames[n] = new SurnameInfo;
char * pch = strtok(line, ",");
int len = strlen(pch);
surnames[n]->setname(pch,len);
surnames[n]->setcounter(atoi(strtok(NULL, ",")));
for (int i = 0; i < 6; i++)
{
pch = strtok(NULL, ",");
surnames[n]->setpctrace(pch[0] == '(' ? -1 : atof(pch), i);
}
}
void mostPopular(int race)
{
const int TOPS = 20;
int tops[TOPS + 1] = { 0 };
int kept = 0;
for (int i = 0; i < numberOfNames; i++)
{
if (surnames[i]->getcounter() < 10000) continue;
int j = kept - 1;
for (; j >= 0; j--)
{
if (surnames[i]->getpctrace(race)*surnames[i]->getcounter() > surnames[tops[j]]->getpctrace(race)*surnames[tops[j]]->getcounter())
tops[j + 1] = tops[j];
else break;
}
if (j + 1 < TOPS) tops[j + 1] = i;
if (kept < TOPS) kept++;
}
for (int i = 0; i < TOPS; i++)
{
surnames[tops[i]]->printname(); cout << " " << surnames[tops[i]]->getpctrace(race)*surnames[tops[i]]->getcounter()/100 << endl;
}
}
void binarysearch(char *name2, int &index)
{
int low = 0; int high = numberOfNames - 1;
while (low <= high)
{
int mid = (low + high) / 2;
int name3 = *surnames[mid]->getname();
if (name3 == name2)
index = mid;
else if (name3 > name2)
high = mid - 1;
else
low = mid + 1;
}
if (low > high) index = -1;
}
void searchName()
{
char inputName[100];
cin.ignore();
cout << "Enter a name (all caps): ";
cin.getline(inputName,100);
int index2 = -1;
binarysearch(inputName, index2);
if (index2 != -1)
{
surnames[index2]->printname();
cout << " appears " << surnames[index2]->getcounter() << " times." << endl;
}
}
|