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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
using namespace std;
const int SIZE = 5000;
const int NAME_LEN = 21;
const int NUM_RANKS = 11;
struct Name{
char name[NAME_LEN];
int rank[NUM_RANKS];
};
int loadNames(Name [], int);
void displayOneHistogramBar(int);
void displayPersonHistogram(Name [], int, int);
void displayPersonBar(Name [], int, int, int);
int findName(Name [], int);
void convertToUpperCase(char []);
void displayDecade(int);
void sort(Name aList[], int actual_no, int aDecade);
int getDecadeNo();
void menu(Name [], int);
int main()
{
int number_of_names = 0;
Name list[SIZE];
number_of_names = loadNames(list, SIZE);
menu(list, number_of_names);
return 0;
}
// READ TEXT FILE AND PREPARE NAME ARRAY
int loadNames( Name aList[], int aSize )
{
ifstream nameList;
nameList.open("baby_names.txt");
int count = 0;
if(nameList.is_open())
{
while(nameList >> aList[count].name )
{
for(int i = 0; i < NUM_RANKS; ++i)
{
nameList >> aList[count].rank[i];
}
++count;
}
nameList.close();
}
else
cout << "Unable to open file\n";
return count;
}
// DISPLAY A SINGLE HISTOGRAM BAR GIVEN THE RANK VALUE
void displayOneHistogramBar(int aRankValue)
{
char bar[200]{'\0'};
int no_stars = 0;
if(aRankValue > 0)
{
no_stars = (1000 - aRankValue) / 10 + 1;
for(int i = 0; i < no_stars; ++i)
bar[i] = '*';
}
cout << setw(4) << aRankValue << ": " << bar << '\n';
}
// DISPLAY ALL DECADES HISTOGRAM BARS FOR A SINGLE NAME
void displayPersonHistogram( Name aList[], int actual_no, int index)
{
for(int i = 0; i < NUM_RANKS; ++i)
{
displayOneHistogramBar(aList[index].rank[i]);
}
}
// DISPLAY A SELECTED DECADE HISTOGRAM BAR FOR A SINGLE NAME
void displayPersonBar(Name aList[], int actual_no, int index, int aDecade)
{
int rank_no = aDecade - 1;
cout << setw(8) << aList[index].name;
displayOneHistogramBar(aList[index].rank[rank_no]);
}
// FIND THE INDEX OF A NAME IN THE LIST
int findName(Name aList[], int actual_no)
{
char search_name[NAME_LEN];
char list_name[NAME_LEN];
int index = -1;
while( index == -1)
{
cout << "Please enter a name: ";
cin >> search_name;
convertToUpperCase(search_name);
for(int i = 0; i < actual_no; ++i)
{
strcpy(list_name,aList[i].name);
convertToUpperCase(list_name);
if(!strcmp(search_name, list_name))
{
index = i;
return index;
}
}
}
return index;
}
// CONVERT A NAME TO ALL UPPERCASE
void convertToUpperCase(char aName[])
{
for(int i = 0; i < strlen(aName); i++)
aName[i] = toupper(aName[i]);
}
// DISPLAY THE DECADE START AND END YEARS GIVEN A DECADE NO. (1-11 INCLUSIVE)
void displayDecade(int aDecade)
{
int delta = 0;
int start = 1900 + (aDecade - 1) * 10;
if(aDecade == 11)
delta = 5;
else
delta = 9;
cout
<< "Decade " << aDecade << ": " << start << '-' << start + delta << '\n';
}
// SORT NAMES TO GET TOP NAMES FOR A DECADE
void sort(Name aList[], int actual_no, int aDecade, int aTop)
{
int aRank = aDecade - 1;
Name temp;
for(int j = 0; j < actual_no; j++)
{
for(int i = 0; i < actual_no-j; i++)
{
if( aList[i].rank[aRank] > aList[actual_no-1-j].rank[aRank])
{
temp = aList[actual_no-1-j];
aList[actual_no-1-j] = aList[i];
aList[i] = temp;
}
}
}
int count = 0, index = 0;
while(count < aTop && index < actual_no)
{
if(aList[index].rank[aRank] != 0)
{
std::cout
<< setw(12) << aList[index].name
<< setw(5) << aList[index].rank[aRank] << '\n';
count++;
}
index++;
}
}
// GET A DECADE NUMBER (1-11 INCLUSIVE)
int getDecadeNo()
{
int decade = 1;
while(
cout << "Enter decade: "
&& cin >> decade
&& (decade < 1 or decade > 11)
)
{
cout << "Invalid decade\n";
}
return decade;
}
//MENU
void menu(Name aList[], int actual_no)
{
char option = '0';
while(tolower(option) != 'd')
{
char line[] =
"\n+-------------------------------------------------------+\n";
cout
<< line
<< "Welcome to nameApp.\n"
<< "Use menu options to look at various statistics for names\n"
<< "popular between 1900 and 2005.\n\n"
<< "a - Print histogram for a name\n"
<< "b - Compare two names in a decade\n"
<< "c - Print top ten names for a decade\n"
<< "d - Quit(write anomalies"
<< line
<< "Your selection: ";
cin >> option;
switch(option)
{
case 'a':
{
int index = findName(aList, actual_no);
displayPersonHistogram(aList, actual_no, index);
break;
}
case 'b':
{
int index_1 = findName(aList, actual_no);
cout << "Name 1: " << aList[index_1].name << " found\n";
int index_2 = findName(aList, actual_no);
cout << "Name 2: " << aList[index_2].name << " found\n";
int decade = getDecadeNo();
displayDecade(decade);
displayPersonBar(aList, actual_no, index_1, decade);
displayPersonBar(aList, actual_no, index_2, decade);
break;
}
case 'c':
{
int decade = getDecadeNo();
displayDecade(decade);
sort( aList, actual_no, decade, 10);
break;
}
case 'd':
cout << "Option d selected\n";
break;
default:
cout << "*** Incorrect option ... please try again ***\n";
}
}
cout << "Here is a list of anomalies encountered ...\n";
}
|