Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. struct HistoryEntry{
  6. public:
  7. int month;
  8. char * url;
  9. void setURL(const char * n)
  10. {
  11. delete[] url;
  12. url = new char[strlen(n) + 1];
  13. strcpy(url, n);
  14. }
  15. void setMonth(int _month)
  16. {
  17. month = _month;
  18. }
  19. HistoryEntry() :month(-1), url(nullptr){}
  20. HistoryEntry(int _month, char const* _url) :month(_month), url(nullptr)
  21. {
  22. setURL(_url);
  23. }
  24. HistoryEntry(HistoryEntry & h)
  25. {
  26. this->setMonth(h.getMonth());
  27. this->setURL(h.getURL());
  28. }
  29. char * getURL() const
  30. {
  31. char * copy = new char[strlen(url) + 1];
  32. strcpy(copy, url);
  33. return copy;
  34. }
  35. int getMonth()const{ return month; }
  36. };
  37.  
  38. class BrowserHistory {
  39. int N;
  40. HistoryEntry* history;
  41. int current;
  42.  
  43.  
  44. public:
  45. BrowserHistory() :N(0), current(-1), history(nullptr){};
  46. BrowserHistory(int n) :N(n)
  47. {
  48. history = new HistoryEntry[N];
  49. current = 0;
  50. }
  51.  
  52. ~BrowserHistory()
  53. {
  54. delete[] history;
  55. }
  56.  
  57. int getN()const {return N;}
  58.  
  59. void addWebsite()
  60. {
  61. if (current<N)
  62. {
  63. int month=0;
  64.  
  65. while (month > 12 || month < 1) {
  66. cout << "month: ";
  67. cin >> month;
  68. cout << endl;
  69. }
  70. cout << "URL: ";
  71. char a[100] = {'\0'};
  72. cin >> a;
  73. cout << endl;
  74. history[current].setMonth(month);
  75. history[current].setURL(a);
  76. current++;
  77. }
  78. }
  79. void addWebsite(const HistoryEntry& newSite){
  80. if (current == N)
  81. cout << "Not enough memory for another site in history"<<endl;
  82. else
  83. {
  84. history[current].setMonth(newSite.getMonth());
  85. history[current].setURL(newSite.getURL());
  86. current++;
  87. }
  88. }
  89. void printHisory(){
  90. for (int i = 0; i<current; i++)
  91. {
  92. cout << "date: " << history[i].month << " url: " << history[i].url <<endl;
  93. }
  94. }
  95. int printMonthHistoryCount(int searchedMonth){
  96. if (searchedMonth>12 || searchedMonth<1)
  97. {
  98. cout << "invalid month";
  99. return -1;
  100. }
  101. else
  102. {
  103. int counter = 0;
  104. for (int i = 0; i<current; i++)
  105. {
  106. if (history[i].month == searchedMonth)
  107. {
  108. counter++;
  109. }
  110. }
  111. return counter;
  112. }
  113. }
  114. int mostActiveMonth()
  115. {
  116. int max = 0;
  117. int maxMonth = 0;
  118. for (int i = 1; i <= 12; i++)
  119. {
  120. int currentMonth = printMonthHistoryCount(i);
  121. if (currentMonth>max)
  122. {
  123. max = currentMonth;
  124. max = i;
  125. }
  126.  
  127. }
  128. return max;
  129. }
  130.  
  131. void deleteLastHistoryEntry()
  132. {
  133. if (current>=1)
  134. current--;
  135. else
  136. {
  137. cout << "Cannot delete from empty history! " << endl;
  138. }
  139. }
  140.  
  141. BrowserHistory& concat(const BrowserHistory &bh1, const BrowserHistory &bh2){
  142. int N = bh1.getN() + bh2.getN();
  143. BrowserHistory* newBh = new BrowserHistory(N);
  144. for (int i = 0; i < bh1.current; i++)
  145. {
  146. HistoryEntry entry(bh1.history[i].getMonth(), bh1.history[i].getURL());
  147. newBh->addWebsite(entry);
  148. }
  149. for (int i = 0; i < bh2.current; i++)
  150. {
  151. HistoryEntry entry(bh2.history[i].getMonth(), bh2.history[i].getURL());
  152. newBh->addWebsite(entry);
  153. }
  154. return *newBh;
  155. }
  156. };
  157.  
  158. int main()
  159. {
  160. BrowserHistory bh(10);
  161. BrowserHistory bh2(10);
  162. bh.addWebsite();
  163. bh.addWebsite();
  164. bh.addWebsite();
  165. HistoryEntry newsite(3, "zdrrrrrrr");
  166. bh2.addWebsite(newsite);
  167. bh2.addWebsite(newsite);
  168. bh2.addWebsite(newsite);
  169. bh.printHisory();
  170. bh.deleteLastHistoryEntry();
  171. cout << "----------------------------Deleting last entry---------------" << endl;
  172. bh.printHisory();
  173. cout << endl;
  174. int month;
  175. cout << "Number of sites in history for month: ";
  176. cin >> month;
  177. cout << "Month " << month << " has " << bh.printMonthHistoryCount(month) << " sites in browser history" << endl;
  178. cout << "Most active month: " << bh.mostActiveMonth()<<endl;
  179. BrowserHistory newbh = bh.concat(bh, bh2);
  180. cout << "Print concatenation of bh and bh2" << endl;
  181. newbh.printHisory();
  182. system("pause");
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement