Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. struct HistoryEntry{
  6. int month;
  7. char *url;
  8. /*~HistoryEntry(){
  9. delete url;
  10. }*/
  11.  
  12. HistoryEntry(int _month, const char *_url){
  13. month = _month;
  14. url = new char[strlen(_url)+1];
  15. strcpy(url, _url);
  16. }
  17.  
  18. HistoryEntry(){
  19. }
  20. };
  21.  
  22.  
  23. class BrowserHistory{
  24. private:
  25. HistoryEntry *history;
  26. int size;
  27. int capacity;
  28. void del();
  29. public:
  30. BrowserHistory(int N){
  31. history = new HistoryEntry [N];
  32. size = 0;
  33. capacity = N;
  34. }
  35. /* ~BrowserHistory(){
  36. delete[] history;
  37. }*/
  38.  
  39. void push_back(int _month, const char* _url){
  40. if(size<capacity){
  41. HistoryEntry he = HistoryEntry(_month, _url);
  42. history[size]=he;
  43. size++;
  44. }
  45. else cout<<"Limit is reached!\n";
  46. }
  47. void push_back(const HistoryEntry& h){
  48. if(size<capacity){
  49. history[size]=h;
  50. size++;
  51. }
  52. else cout<<"Limit is reached!\n";
  53. }
  54. void print(){
  55. for(int i=0; i<size; i++){
  56. cout<<history[i].month<<" "<<history[i].url<<endl;
  57. }
  58. }
  59. int per_month(int month)const{
  60. int result=0;
  61. for(int i=0; i<size; i++){
  62. if(history[i].month == month){
  63. result++;
  64. }
  65. }
  66. return result;
  67. }
  68. int most_entries()const{
  69. int max_num=0, max_month=0, cnt = 0;
  70. for(int i=1; i<=12; i++){
  71. for(int j = 0; j < size; j++){
  72. if(history[j].month == i){
  73. cnt++;
  74. }
  75. }
  76.  
  77. if(cnt > max_num){
  78. max_num = cnt;
  79. cnt = 0;
  80. max_month = i;
  81. }
  82. }
  83. return max_month;
  84. }
  85. void delete_last(){
  86. if(size > 0){
  87. size--;
  88. }
  89. }
  90.  
  91.  
  92. BrowserHistory concat(const BrowserHistory& other)const{
  93. BrowserHistory result(capacity+other.capacity);
  94. for(int i=0; i<size; i++){
  95. result.push_back(history[i]);
  96. }
  97.  
  98.  
  99. for(int j=0; j<other.size; j++){
  100. result.push_back(other.history[j]);
  101. }
  102. return result;
  103. }
  104.  
  105. };
  106.  
  107. int main()
  108. {
  109. BrowserHistory b(5);
  110. b.push_back(3, 'c');
  111. b.push_back(HistoryEntry(3, 'v'));
  112. b.print();
  113. cout<<b.most_entries()<<endl;
  114. cout<<b.per_month(3)<<endl;
  115. b.push_back(HistoryEntry(7, 'f'));
  116. b.print();
  117. b.delete_last();
  118. BrowserHistory c=b.concat(b);
  119. cout<<"b+b: \n";
  120. c.print();
  121.  
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement