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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
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);
void displayNames(Name [], int);
void selectSort(Name[], int );
int findName(Name [], int);
void convertToUpperCase(char []);
void doMenu(Name [], int,int );
void displayAllHistograms(Name [], int);
void displayDecade(int);
int getDecadeNo();
int main()
{
int number_of_names = 0;
Name list[SIZE];
number_of_names = loadNames(list, SIZE);
displayNames(list, number_of_names);
displayAllHistograms(list, number_of_names);
int found_index = findName(list, number_of_names);
if (found_index != -1)
displayPersonHistogram(list, number_of_names, found_index);
else
cout << "Name not found\n";
return 0;
}
int loadNames( Name aList[], int aSize )
{
ifstream nameList;
nameList.open("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;
}
void displayAllHistograms(Name aList[], int actual_number_names) {
for (int i = 0; i < actual_number_names; ++i) {
cout << "Histogram for name, " << aList[i].name << '\n';
displayPersonHistogram(aList, actual_number_names, i);
}
}
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';
}
void displayPersonHistogram( Name aList[], int actual_no, int index)
{
for(int i = 0; i < NUM_RANKS; ++i)
{
displayOneHistogramBar(aList[index].rank[i]);
}
}
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]);
}
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;
}
void displayNames( Name list[], int actual_number_names)
{
for(int i = 0; i < actual_number_names; ++i)
{
cout << list[i].name << '\n';
for(int j = 0; j < NUM_RANKS; ++j)
{
cout << list[i].rank[j] << ' ';
}
std::cout << '\n';
}
}
void convertToUpperCase(char aName[])
{
for(int i = 0; i < strlen(aName); i++)
aName[i] = toupper(aName[i]);
}
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';
}
int getDecadeNo()
{
int decade = 1;
while(
cout << "Enter decade: "
&& cin >> decade
&& (decade < 1 or decade > 11)
)
{
cout << "Invalid decade\n";
}
return decade;
}
void selectSort(Name aList[], int actual_no)
{
char left[NAME_LEN];
char right[NAME_LEN];
int pos_min;
for (int i = 0; i < actual_no - 1; i++)
{
pos_min = i;
for (int j = i + 1; j < actual_no; j++)
{
strcpy(right, aList[i].name); // get the name to the right
convertToUpperCase(right); // convert to upper case
strcpy(left, aList[pos_min].name); // get the name to the left
convertToUpperCase(left); // convert to uppper case
if (right < left) { // compare both left and right names
pos_min = j; // if left <right, let i and j swap places
}
}
if (pos_min != i)
{
strcpy(right, aList[i].name); // get the name to the right
convertToUpperCase(right); // convert to upper case
strcpy(left, aList[pos_min].name); // get the name to the left
convertToUpperCase(left); // convert to uppper case
swap(right, left); // swap is a library function. It will swap the places
}
}
}
void doMenu(Name aList[], int actual_no,int index)
{
bool again = true;
do
{
cout << "\ta - Print histogram for a name\n";
cout << "\tb - Compare two names in a decade\n";
cout << "\tc - Print top ten names for a decade\n";
cout << "\td - Quit(write anomalies)\n";
char userChoice = ' ';
cout << "\n\tYour selection : ";
cin >> userChoice;
cin.get();
switch (userChoice)
{
case 'a':
displayPersonHistogram(aList, actual_no,index);
again = true;
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);
again=true;
break;
}
case 'c':
selectSort(aList, actual_no);
again = true;
break;
case 'd':
// do d stuff
again = false;
break;
default:
cout << "Wrong choice of character. Must be a, b, c or d\n";
break;
}
} while (again);
}
|