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
|
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
const int totalNames=10000;
void sortNamesB(std::string,unsigned int,std::size_t);
void sortNamesG(std::string,unsigned int,std::size_t);
void printTopNames(std::string boyName[], unsigned int boyFreq[], std::size_t numBoys,
std::string girlName[], unsigned int girlFreq[], std::size_t numGirls,
std::size_t numNames);
std::size_t highestFreqB(unsigned int,std::size_t);
std::size_t highestFreqG(unsigned int,std::size_t);
int main(int argc,char *argv[]){
std::string name,gender;
std::size_t freq;
ifstream fin;
unsigned int girlFreq[totalNames],boyFreq[totalNames];
std::string girlName[totalNames],boyName[totalNames];
std::size_t numBoys = 0,numGirls = 0;
if (argc < 3){
cout << "Wrong number of arguments" << endl << "Correct usage: filename #namesToPrint" << endl;
return -1;
}
std::size_t numNames = atoi(argv[2]);
numBoys = numNames;
numGirls = numNames;
fin.open(argv[1]);
if (fin.fail()){
cout << argv[1] << " not opened";
return 1;
}
do{
while(numBoys < totalNames && numGirls < totalNames && fin >> name >> gender >> freq){
if(gender == "M"){
boyName[numBoys] = name;
boyFreq[numBoys] = freq;
numBoys++;
}
else{
girlName[numGirls] = name;
girlFreq[numGirls] = freq;
numGirls++;
}
}
} while(numGirls<numNames && numBoys<numNames);
cout << argv[2] << " Most Popular Boys and Girls Names" << endl << endl;
std::cout.width(11); std::cout << std::left << "Girls";
std::cout.width(5); std::cout << std::right << "Frequency";
cout << " ";
std::cout.width(11); std::cout << std::left << "Boys";
std::cout.width(4); std::cout << std::right << "Frequency" << endl;
printTopNames(boyName,boyFreq,numBoys,girlName,girlFreq,numGirls,numNames);
fin.close();
return 0;
}
std::size_t highestFreqB (unsigned int boyFreq[],std::size_t numBoys){
std::size_t pos = 0;
for(std::size_t i=0;i<numBoys;i++){
if(boyFreq[i]>pos){
pos=i;
}
}
return pos;
}
std::size_t highestFreqG (unsigned int girlFreq[],std::size_t numGirls){
std::size_t pos = 0;
for(std::size_t i=0;i<numGirls;i++){
if(girlFreq[i]>pos){
pos=i;
}
}
return pos;
}
void sortNamesB(std::string boyName[], unsigned int boyFreq[], std::size_t numBoys){
if (numBoys > 0){
std::size_t posHighestB = highestFreqB(boyFreq,numBoys);
using std::swap;
swap(boyName[0],boyName[posHighestB]);
swap(boyFreq[0],boyFreq[posHighestB]);
sortNamesB(boyName+1,boyFreq+1,numBoys-1);
}
}
void sortNamesG(std::string girlName[], unsigned int girlFreq[], std::size_t numGirls){
if (numGirls > 0){
std::size_t posHighestG = highestFreqG(girlFreq,numGirls);
using std::swap;
swap(girlName[0],girlName[posHighestG]);
swap(girlFreq[0],girlFreq[posHighestG]);
sortNamesG(girlName+1,girlFreq+1,numGirls-1);
}
}
void printTopNames(std::string boyName[totalNames], unsigned int boyFreq[totalNames], std::size_t numBoys,
std::string girlName[totalNames], unsigned int girlFreq[totalNames], std::size_t numGirls,
std::size_t numNames){
sortNamesB(boyName,boyFreq,numBoys);
sortNamesG(girlName,girlFreq,numGirls);
for (std::size_t i=0;i<numNames;i++){
std::cout.width(11); std::cout << std::left << girlName[i];
std::cout.width(5); std::cout << std::right << girlFreq[i];
std::cout << " ";
std::cout.width(11); std::cout << std::left << boyName[i];
std::cout.width(4); std::cout << std::right << boyFreq[i] << endl;
}
}
|