Advertisement
Guest User

Untitled

a guest
May 31st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. // Homework3Seminar.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cstring>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10.  
  11.  
  12. class Human {
  13. protected:
  14. char* firstName;
  15. char* lastName;
  16. public:
  17. char* getFirstName() const { return firstName; }
  18. char* getLastName() const { return lastName; }
  19. virtual void print() {
  20. cout << "Name: " << firstName << " " << lastName << endl;
  21. }
  22.  
  23. Human(char* _firstName = " ", char* _lastName = " ") {
  24. firstName = new char[strlen(_firstName) + 1];
  25. strcpy(firstName, _firstName);
  26. lastName = new char[strlen(_lastName) + 1];
  27. strcpy(lastName, _lastName);
  28. }
  29.  
  30. Human(const Human& other) {
  31. firstName = new char[strlen(other.getFirstName()) + 1];
  32. strcpy(firstName, other.getFirstName());
  33. lastName = new char[strlen(other.getLastName()) + 1];
  34. strcpy(lastName, other.getLastName());
  35. }
  36.  
  37. Human operator=(const Human& other) {
  38. if (this != &other) {
  39. delete[] firstName;
  40. firstName = new char[strlen(other.getFirstName()) + 1];
  41. strcpy(firstName, other.getFirstName());
  42. delete[] lastName;
  43. lastName = new char[strlen(other.getLastName()) + 1];
  44. strcpy(lastName, other.getLastName());
  45. }
  46.  
  47. return *this;
  48. }
  49.  
  50.  
  51.  
  52. };
  53.  
  54.  
  55. class Student : public Human {
  56. protected:
  57. double grade;
  58. public:
  59. Student() : Human(), grade(0) {}
  60.  
  61. Student(double _grade, char* _firstName, char* _lastName) : Human(_firstName, _lastName) {
  62. grade = _grade;
  63. }
  64.  
  65. Student(const Student& other) {
  66. firstName = new char[strlen(other.getFirstName()) + 1];
  67. strcpy(firstName, other.getFirstName());
  68. lastName = new char[strlen(other.getLastName()) + 1];
  69. strcpy(lastName, other.getLastName());
  70. grade = other.grade;
  71. }
  72.  
  73.  
  74. bool operator<(Student const & b)
  75. {
  76. return this->getGrade() < b.getGrade();
  77. }
  78.  
  79. Student operator=(const Student& other) {
  80. if (this != &other) {
  81. delete[] firstName;
  82. firstName = new char[strlen(other.getFirstName()) + 1];
  83. strcpy(firstName, other.getFirstName());
  84. delete[] lastName;
  85. lastName = new char[strlen(other.getLastName()) + 1];
  86. strcpy(lastName, other.getLastName());
  87. grade = other.grade;
  88. }
  89.  
  90. return *this;
  91. }
  92.  
  93.  
  94. double getGrade() const {
  95. return this->grade;
  96. }
  97.  
  98. void print() {
  99. cout << "Name: " << firstName << " " << lastName << endl;
  100. cout << "Grade: " << grade << endl;
  101.  
  102. }
  103. };
  104.  
  105. class Worker : public Human {
  106. protected:
  107. double weekSalary;
  108. double workHoursPerDay;
  109. public:
  110. Worker(char* _firstName, char* _lastName, double _weekSalary, double _workHoursPerDay) : Human(_firstName, _lastName) {
  111. weekSalary = _weekSalary;
  112. workHoursPerDay = _workHoursPerDay;
  113. }
  114. double getWeekSalary() const { return weekSalary; }
  115. double getWorkHoursPerDay() const { return workHoursPerDay; }
  116.  
  117. Worker(const Worker& other) {
  118. firstName = new char[strlen(other.getFirstName()) + 1];
  119. strcpy(firstName, other.getFirstName());
  120. lastName = new char[strlen(other.getLastName()) + 1];
  121. strcpy(lastName, other.getLastName());
  122. weekSalary = other.getWeekSalary();
  123. workHoursPerDay = other.getWorkHoursPerDay();
  124. }
  125. Worker operator=(const Worker& other) {
  126. if (this != &other) {
  127. delete[] firstName;
  128. firstName = new char[strlen(other.getFirstName()) + 1];
  129. strcpy(firstName, other.getFirstName());
  130. delete[] lastName;
  131. lastName = new char[strlen(other.getLastName()) + 1];
  132. strcpy(lastName, other.getLastName());
  133. weekSalary = other.getWeekSalary();
  134. workHoursPerDay = other.getWorkHoursPerDay();
  135. }
  136. return *this;
  137. }
  138. double MoneyPerHour() {
  139. return weekSalary / (7 * workHoursPerDay);
  140. }
  141. void print() {
  142. cout << "Name: " << firstName << " " << lastName << endl;
  143. cout << "Week salary: " << weekSalary << endl;
  144. cout << "Worked hours per day: " << workHoursPerDay << endl;
  145. cout << "Money per hour earned: " << MoneyPerHour() << endl;
  146. }
  147. };
  148.  
  149.  
  150.  
  151.  
  152. int main()
  153. {
  154. Student students[10] = { Student(11 , "BEmil" , "Todorovv"),
  155. Student(2 , "EDmil" , "Todorovv"),
  156. Student(73 , "HEmil" , "Todorovv"),
  157. Student(44 , "JEmil" , "Todorovv"),
  158. Student(5 , "IEmil" , "Todorovv"),
  159. Student(62 , "OEmil" , "Todorovv"),
  160. Student(71 , "FEmil" , "Todorovv"),
  161. Student(80 , "AEmil" , "Todorovv"),
  162. Student(9 , "HEmil" , "Todorovv"),
  163. Student(115 , "HHEmil" , "Todorovv")
  164. };
  165. sort(begin(students), end(students)); // probvah go ot liubopitstvo
  166.  
  167.  
  168. for (int i = 0; i < 10; i++)
  169. {
  170. students[i].print();
  171. cout << endl;
  172. }
  173.  
  174. Worker workers[10] = { Worker("QEmil" , "Todorovv",200,4),
  175. Worker("REmil" , "Todorovv",200,5),
  176. Worker("CEmil" , "Todorovv",300,6),
  177. Worker("BEmil" , "Todorovv",400,7),
  178. Worker("REmil" , "Todorovv",500,8),
  179. Worker("FEmil" , "Todorovv",600,9),
  180. Worker("NEmil" , "Todorovv",700,8),
  181. Worker("GEmil" , "Todorovv",800,8),
  182. Worker("HEmil" , "Todorovv",900,8),
  183. Worker("AEmil" , "Todorovv",1000,8)
  184. };
  185. int minimalPosition; //normalna sortirovka
  186. for (int i = 0; i < 10; i++)
  187. {
  188. minimalPosition = i;
  189. for (int j = i + 1; j < 10; j++)
  190. {
  191. if (workers[j].MoneyPerHour() > workers[minimalPosition].MoneyPerHour())
  192. {
  193. minimalPosition = j;
  194. }
  195. }
  196. if (minimalPosition != i)
  197. {
  198. Worker flag = workers[i];
  199. workers[i] = workers[minimalPosition];
  200. workers[minimalPosition] = flag;
  201. }
  202. }
  203. for (int i = 0; i < 10; i++)
  204. {
  205. workers[i].print();
  206. cout << endl;
  207. }
  208.  
  209.  
  210. cout << "------------------------------------------------------------" << endl;
  211. cout << endl;
  212. Human** doubleArray = new Human*[20];
  213. for (int i = 0; i < 10; i++)
  214. {
  215. doubleArray[i] = new Worker(workers[i]);
  216. }
  217. for (int i = 10; i < 20; i++)
  218. {
  219. doubleArray[i] = new Student(students[i - 10]);
  220. }
  221. // normalna sortirovka
  222. short k;
  223. Human* max;
  224.  
  225. for (int i = 0; i < 19; i++) {
  226. max = doubleArray[i];
  227. k = i;
  228. for (int j = i + 1; j < 20; j++)
  229. if (strcmp(doubleArray[j]->getFirstName(), max->getFirstName()) == 0) {
  230. if (strcmp(doubleArray[j]->getLastName(), max->getLastName()) < 0) {
  231. max = doubleArray[j];
  232. k = j;
  233. }
  234. }
  235. else {
  236. if (strcmp(doubleArray[j]->getFirstName(), max->getFirstName()) < 0) {
  237. max = doubleArray[j];
  238. k = j;
  239. }
  240. }
  241. Human* swap;
  242. swap = doubleArray[k];
  243. doubleArray[k] = doubleArray[i];
  244. doubleArray[i] = swap;
  245. }
  246. for (int i = 0; i < 20; i++)
  247. {
  248. doubleArray[i]->print();
  249. cout << endl;
  250. }
  251. return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement