Rudia
Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору #include<iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person(); Person(string surname, int freq) { m_name = surname; m_freq = freq; } bool isgreater(Person* pers) { if (getFreq()>pers->getFreq()) { return true; } else { return false; } } void print() { if (m_name.size()<6) { cout << "\n\"" << m_name << "\"\t\t" << m_freq; } else { cout << "\n\"" << m_name << "\"\t" << m_freq; } } string getName() { return m_name; } int getFreq() { return m_freq; } private: string m_name; int m_freq; }; int main(int argc, char* argv[]) { const int MAX = 50000; Person* people[MAX]; int pcount = 0; if (argc != 2) { cout << "\nProgram should be started with parameter: program.exe inputfile.txt"; return -1; } ifstream namelist(argv[1]); if (!namelist) { cout << "\nUnable to open file"; return -1; } char str[80]; int i,j; while (!namelist.eof()) { namelist >> str >> i; people[pcount++] = new Person(str,i); } Person* point; int adr; int prg = 0; for (i=0; i<pcount-1; i++) //сортировка - займет несколько минут при таком количестве классов, для тестирования лучше урезать файл до пару сотен записей { point = people[i]; adr = i; for (j=i+1; j<pcount; j++) { if (people[j]->isgreater(point)) { point = people[j]; adr = j; } } if (adr!=i) { people[adr] = people[i]; people[i] = point; } } cout << "\n10 most frequent:"; for (i=0; i<10; i++) { people[i]->print(); } cout << "\n10 less frequent:"; for (i=pcount-1; i>pcount-11; i--) { people[i]->print(); } cout << endl << "Total records:" <<pcount; int flag = 1; string s; while (true) { cout << "\nInput surname, or type 0 to exit:"; cin >> s; if (s == "0") { break; } else { for (j=0; j<pcount; j++) { if (people[j]->getName() == s) { people[j]->print(); cout << "\nOn position:" << j; break; if (j == pcount-1) { j++; } } } if (j == pcount) { cout << "\Such surname was not found"; } } } } |