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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <cstdlib>
#include <ctime>
#include <cassert>
namespace Utils {
inline int Random(int min, int max) {
assert(min <= max);
double temp = static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
return static_cast<int>(min + temp * (max - min + 1));
}
std::string GenerateRandomName();
} // end namespace Utils
int main() {
std::srand(static_cast<unsigned>(std::time(0)));
// for testing could use fixed seed
std::cout << std::left;
std::map<std::string, char> studentGrades;
const int count = 1000;
for(int i = 0; i < count; ++i) {
std::string firstName = Utils::GenerateRandomName();
std::string lastName = Utils::GenerateRandomName();
std::string name = firstName;
name += " ";
name += lastName;
// assumes grade = A B C D E F
int gradeOffset = Utils::Random(0, 5);
char grade = static_cast<char>('A' + gradeOffset);
//for debugging
//std::cout << "Add : " << name << " " << grade << "\n";
studentGrades.insert(make_pair(name, grade));
}
const int maxDisplayCount = 10;
int displayCount = 0;
std::cout << "First "
<< std::min(maxDisplayCount, static_cast<int>(studentGrades.size()))
<< " of " << studentGrades.size() << " values:\n\n";
std::map<std::string, char>::iterator iter = studentGrades.begin();
while((iter != studentGrades.end()) && (maxDisplayCount > displayCount)) {
std::cout << std::setw(20) << (*iter).first << " " << (*iter).second << "\n";
++iter;
++displayCount;
}
return 0;
}
namespace RandNameData {
static const char* vowels[]= { "a", "e", "i", "o", "u" };
static const size_t vowelCount = sizeof(vowels)/sizeof(vowels[0]);
static const char* consonants[] = { "b", "c", "d", "f", "g", "h", "j",
"k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z" };
static const size_t consonantCount = sizeof(consonants)/sizeof(consonants[0]);
} // end namespace RandNameData
std::string Utils::GenerateRandomName() {
using namespace RandNameData;
std::string name;
bool vowelWanted = (1 == Random(0, 1));
size_t targetLen = 0;
int temp = Random(1, 663);
if(650 < temp) targetLen = 11;
else if(615 < temp) targetLen = 10;
else if(525 < temp) targetLen = 9;
else if(525 < temp) targetLen = 8;
else if(400 < temp) targetLen = 7;
else if(225 < temp) targetLen = 6;
else if( 80 < temp) targetLen = 5;
else if( 12 < temp) targetLen = 4;
else if( 1 < temp) targetLen = 3;
else targetLen = 2;
while(targetLen > name.length()) {
if(vowelWanted) {
int index = Random(0, vowelCount - 1);
name += vowels[index];
vowelWanted = false;
} else {
int index = Random(0, consonantCount - 1);
name += consonants[index];
vowelWanted = true;
}
}
name[0] = static_cast<char>(toupper(name[0]));
return name;
}
|