Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class GroupProject
  6. {
  7. public:
  8. GroupProject();
  9.  
  10. GroupProject(const GroupProject& copyMe); //kominn
  11. ~GroupProject(); //kominn
  12. void operator= (const GroupProject &copyMe); //kominn
  13.  
  14. friend GroupProject operator + (const GroupProject &A, const GroupProject &B);
  15. friend istream& operator >> (istream &ins, GroupProject &gp);
  16. friend ostream& operator << (ostream &outs, const GroupProject &gp);
  17.  
  18. int get_Count() const;
  19.  
  20. private:
  21.  
  22. void add_member(string nafn);
  23. void make_space_for_one_more(string nafn);
  24. bool is_member(string nafn);
  25. void sortgroup();
  26.  
  27. int n;
  28. string *p;
  29. };
  30.  
  31. GroupProject::GroupProject()
  32. {
  33. n = 0;
  34. p = NULL;
  35. }
  36.  
  37.  
  38. GroupProject::GroupProject(const GroupProject& copyMe) //Copy-Constructor
  39. {
  40. n = copyMe.n;
  41.  
  42. if(n == 0)
  43. {
  44. p = NULL;
  45. }
  46. else
  47. {
  48. p = new string[n];
  49. for(int i = 0; i < n; i++)
  50. {
  51. p[i] = copyMe.p[i];
  52. }
  53. }
  54. }
  55.  
  56. GroupProject::~GroupProject() //Destructor
  57. {
  58. if(p != NULL)
  59. {
  60. delete []p;
  61. p = NULL;
  62. }
  63. }
  64.  
  65. void GroupProject::operator = (const GroupProject &copyMe)
  66. {
  67. if(n != copyMe.n)
  68. {
  69. n = copyMe.n;
  70. if(p != NULL)
  71. {
  72. delete [] p;
  73. }
  74.  
  75. if(n == 0)
  76. {
  77. p = NULL;
  78. }
  79. else
  80. {
  81. p = new string [n];
  82. }
  83. }
  84. for (int i = 0; i < n; i++)
  85. {
  86. p[i] = copyMe.p[i];
  87. }
  88. }
  89.  
  90. GroupProject operator + (const GroupProject &A, const GroupProject &B)
  91. {
  92. GroupProject C;
  93.  
  94. for(int i = 0; i < A.n; i++)
  95. {
  96. C.add_member(A.p[i]);
  97. }
  98.  
  99. for(int i = 0; i < B.n; i++)
  100. {
  101. C.add_member(B.p[i]);
  102. }
  103.  
  104. return C;
  105.  
  106. }
  107.  
  108. ostream& operator << (ostream &outs, const GroupProject &gp)
  109. {
  110. for(int i = 0; i < gp.n; i++)
  111. {
  112. outs << gp.p[i] << endl;
  113. }
  114. return outs;
  115. }
  116.  
  117.  
  118. istream& operator >> (istream &ins, GroupProject &gp)
  119. {
  120. int tala;
  121. string nafn;
  122. ins >> tala;
  123.  
  124. for (int i = 0; i < tala; i++)
  125. {
  126. ins >> nafn;
  127. gp.add_member(nafn);
  128. }
  129. return ins;
  130. }
  131.  
  132. int GroupProject::get_Count() const
  133. {
  134. return n;
  135. }
  136.  
  137. void GroupProject::add_member(string nafn)
  138. {
  139. if ( !is_member(nafn) )
  140. {
  141. make_space_for_one_more(nafn);
  142. sortgroup();
  143. }
  144. }
  145.  
  146. void GroupProject::make_space_for_one_more(string nafn)
  147. {
  148. string *new_p = new string[ n+1 ];
  149.  
  150. for (int i = 0; i < n; i++)
  151. {
  152. new_p[i] = p[i];
  153. }
  154. new_p[n] = nafn;
  155. n = n+1;
  156.  
  157. if (p != NULL)
  158. {
  159. delete [] p;
  160. }
  161.  
  162. p = new_p;
  163. }
  164.  
  165. void GroupProject::sortgroup()
  166. {
  167. int i, j;
  168. string tmp;
  169.  
  170. for (i = 1; i < n; i++)
  171. {
  172. tmp = p[i];
  173. j = i;
  174. while (j > 0 && p[j - 1] > tmp)
  175. {
  176. p[j] = p[j - 1];
  177. j--;
  178. }
  179. p[j] = tmp;
  180. }
  181. }
  182.  
  183. void printStudentCount(string title, GroupProject gp);
  184.  
  185.  
  186.  
  187. int main()
  188. {
  189. /*GroupProject A,B;
  190. cin >> A >> B;
  191. printStudentCount("Team A", A);
  192. printStudentCount("Team B", B);
  193. cout << "Team A:" << endl << A << endl;
  194. cout << "Team B:" << endl << B << endl;
  195. cout << "Team A+B:" << endl << A+B << endl;
  196. cout << "Team A-B:" << endl << A-B << endl;*/
  197. return 0;
  198. }
  199.  
  200.  
  201. void printStudentCount(string title, GroupProject gp)
  202. {
  203. cout << title << " has ";
  204. cout << gp.get_Count() << " student";
  205. if (gp.get_Count() > 1)
  206. {
  207. cout << "s";
  208. }
  209. cout << "." << endl;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement