None

by coder on December 31st, 2009
No notes
Syntax: C++
Show lines - Hide lines - Show in textbox - Download
  1. /*
  2. Rozwiązanie ćwiczenia numer 3 z wikipedii.
  3. */
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <functional>
  10. #include <list>
  11. #include <set>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16. // funktor, ktory informuje czy wprowadzone slowo
  17. // jest zakonczone wyznaczonym sufixem
  18.  
  19. class EndsWith : public std::unary_function<string, bool>
  20. {
  21. const string sufix;
  22. public:
  23.  
  24. EndsWith(const string suf) : sufix(suf) {}
  25.  
  26. bool operator()(const string word) const
  27. {
  28. // szukanie wystapienia wprowadzonej sekwencji
  29. unsigned const index = word.rfind(sufix);
  30. if (index == string::npos)
  31. return false;
  32.  
  33. // sprawdzenie czy sekwencja jest sufiksem
  34. unsigned const rightPositon = word.size() - sufix.size();
  35. if (index != rightPositon)
  36. return false;
  37.  
  38. return true;
  39. }
  40. };
  41.  
  42. // typ reprezentujący płeć
  43.  
  44. enum Sex
  45. {
  46. Male, Female
  47. };
  48.  
  49.  
  50. // typ reprezentujacy osoba z bazy
  51.  
  52. class Person
  53. {
  54. string pesel;
  55. Sex sex;
  56. string firstName;
  57. string secondName;
  58.  
  59. public:
  60.  
  61. Person(string pPesel, Sex pSex, string pfName, string psName) :
  62. pesel(pPesel), sex(pSex), firstName(pfName), secondName(psName) {}
  63.  
  64. inline string getPesel() const
  65. {
  66. return pesel;
  67. }
  68.  
  69. inline Sex getSex() const
  70. {
  71. return sex;
  72. }
  73.  
  74. inline string getFirstName() const
  75. {
  76. return firstName;
  77. }
  78.  
  79. inline string getSecondName() const
  80. {
  81. return secondName;
  82. }
  83.  
  84. };
  85.  
  86. // reprezentacja klasy programu
  87.  
  88. class Appliaction
  89. {
  90. bool fileError;
  91. list<Person> people;
  92. ifstream fileInput;
  93. public:
  94. Appliaction(const int argc, char*argv[]);
  95. ~Appliaction();
  96. bool run();
  97. private:
  98. void getDataFromFile();
  99. void showResult1() const;
  100. void showResult2() const;
  101. };
  102.  
  103. Appliaction::Appliaction(const int argc, char*argv[]) : fileError(false)
  104. {
  105. const unsigned rightCountArgc = 2;
  106. // kontrola właściwej ilości argumentów wiersza
  107. if (argc != rightCountArgc)
  108. {
  109. fileError = true;
  110. return;
  111. }
  112. // sprawdzenie pliku
  113. fileInput.open(argv[1]);
  114. if (!fileInput.is_open())
  115. fileError = true;
  116. }
  117.  
  118. Appliaction::~Appliaction()
  119. {
  120. if (!fileError)
  121. fileInput.close();
  122. }
  123.  
  124. bool Appliaction::run()
  125. {
  126. if (fileError)
  127. return false;
  128. getDataFromFile();
  129. // realizacja zadań do wykonania
  130. showResult1();
  131. showResult2();
  132. }
  133.  
  134. void Appliaction::getDataFromFile()
  135. {
  136. string line;
  137. // zignorowanie naglowka przetwarzanego pliku (pomijam 3 linie)
  138. for (unsigned i = 0; i != 3 && !fileInput.eof(); ++i)
  139. getline(fileInput, line);
  140.  
  141. // tymczasowe dane na potrzeby odbioru z wejscia
  142. string pesel;
  143. char chSex;
  144. Sex sex;
  145. string firstName;
  146. string secondName;
  147.  
  148. while (!fileInput.eof())
  149. {
  150. // pobieranie danych
  151. fileInput >> pesel >> chSex >> firstName >> secondName;
  152. // konwersja znakow M i K na typ Sex
  153. if (chSex == 'M')
  154. sex = Male;
  155. else
  156. sex = Female;
  157. // tworzenie nowej osoby na liscie
  158. people.push_back(Person(pesel, sex, firstName, secondName));
  159. }
  160. }
  161.  
  162. void Appliaction::showResult1() const
  163. {
  164. // zbior wszystkich nazwisk
  165. set<string> allSecondNames;
  166. // zbior właściwych nazwisk do wyświetlenia
  167. set<string> resultSecondNames;
  168. // sufix jakim mają kończyć się szukane nazwiska
  169. const string sufix = "ski";
  170. // pobranie do zbioru wszystkich nazwisk
  171. list<Person>::const_iterator it = people.begin();
  172. for (; it != people.end(); ++it)
  173. allSecondNames.insert(it->getSecondName());
  174. // skopiowanie nazwisk zakończonych właściwym sufixem
  175. remove_copy_if(allSecondNames.begin(), allSecondNames.end(),
  176. inserter(resultSecondNames, resultSecondNames.begin()),
  177. not1(EndsWith(sufix)));
  178. // wyświetlenie rezultatu
  179. cout << "Zadanie 1\n\t";
  180. copy(resultSecondNames.begin(), resultSecondNames.end(),
  181. ostream_iterator<string > (cout, "\n\t"));
  182. }
  183.  
  184. void Appliaction::showResult2() const
  185. {
  186. // zbior wszystkich imion
  187. set<string> allFirstNames;
  188. // zbior zenskich imion, sortowanych alfabetycznie
  189. set<string, less<string> > femaleFirstNames;
  190. // zbior meskich imion, sortowanych alfabetycznie
  191. set<string, less<string> > maleFirstNames;
  192. // sufix sluzacy do rozpoznania plciu po imieniu
  193. const string femaleSuffix = "a";
  194. // pobranie wszystkich imion
  195. list<Person>::const_iterator it = people.begin();
  196. for (; it != people.end(); ++it)
  197. allFirstNames.insert(it->getFirstName());
  198. // skopiowanie kobiecych imion
  199. remove_copy_if(allFirstNames.begin(), allFirstNames.end(),
  200. inserter(femaleFirstNames, femaleFirstNames.begin()),
  201. not1(EndsWith(femaleSuffix)));
  202. // skopiowanie meskich imion, sposobem roznicy zbiorow
  203. set_difference(allFirstNames.begin(), allFirstNames.end(),
  204. femaleFirstNames.begin(), femaleFirstNames.end(),
  205. inserter(maleFirstNames, maleFirstNames.begin()));
  206. // wyswietlenie rezultatu
  207. cout << "\n\nZadanie 2:\nZenskie imiona:\n\t";
  208. copy(femaleFirstNames.begin(), femaleFirstNames.end(),
  209. ostream_iterator<string > (cout, "\n\t"));
  210. cout << "\nMeskie imiona:\n\t";
  211. copy(maleFirstNames.begin(), maleFirstNames.end(),
  212. ostream_iterator<string > (cout, "\n\t"));
  213. cout << "\n";
  214. }
  215.  
  216. int main(int argc, char * argv[])
  217. {
  218. Appliaction theApp(argc, argv);
  219. theApp.run();
  220. return 0;
  221. }

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS