Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct HistoryEntry {
  7.     int date;
  8.     char* URL;
  9. };
  10.  
  11. class BrowserHistory {
  12. public:
  13.     BrowserHistory();
  14.     BrowserHistory(int N);
  15.     BrowserHistory operator= (BrowserHistory const&);
  16.     ~BrowserHistory();
  17.     void addSite();
  18.     void addSite(const HistoryEntry&);
  19.     void printAll();
  20.     int countSitesMonth(int month);
  21.     int mostVisitedMonth();
  22.     void DeleteLastSite();
  23.     BrowserHistory concat2(BrowserHistory, BrowserHistory);
  24.  
  25. private:
  26.     HistoryEntry* history;
  27.     int siteCounter;
  28.     int siteLimit;
  29. };
  30.  
  31. BrowserHistory::BrowserHistory() {
  32.     history->date = NULL;
  33.     history->URL = NULL;
  34. }
  35.  
  36. BrowserHistory::BrowserHistory(int N) {
  37.     history = new HistoryEntry[N];
  38.     for (int i = 0; i < N; i++) {
  39.         history[i].date = 0;
  40.         history[i].URL = NULL;
  41.         siteCounter = 0;
  42.     }
  43.     siteLimit = N;
  44. }
  45.  
  46. BrowserHistory BrowserHistory::operator=(BrowserHistory const &other) {
  47.     if (this != &other) {
  48.         for (int i = 0; i < other.siteCounter; i++) {
  49.             history[i] = other.history[i];
  50.             siteCounter = other.siteCounter;
  51.         }
  52.     }
  53.     return *this;
  54. }
  55.  
  56. BrowserHistory::~BrowserHistory() {
  57.     delete[] history;
  58. }
  59.  
  60. void BrowserHistory::addSite() {
  61.     if (++siteCounter < siteLimit) {
  62.         cout << "Month: ";
  63.         cin >> history[siteCounter].date;
  64.         cout << "URL: ";
  65.         cin >> history[siteCounter].URL;
  66.         siteCounter++;
  67.     }
  68.     else {
  69.         cout << "The history is full!" << endl;
  70.     }
  71. }
  72.  
  73. void BrowserHistory::addSite(const HistoryEntry& x) {
  74.     if (++siteCounter < siteLimit) {
  75.         history[siteCounter].date = x.date;
  76.         history[siteCounter].URL = x.URL;
  77.         siteCounter++;
  78.     }
  79.     else {
  80.         cout << "The history is full!" << endl;
  81.     }
  82. }
  83.  
  84. void BrowserHistory::printAll() {
  85.     for (int i = 0; i < siteCounter; i++) {
  86.         cout << history[i].date << " " << history[i].URL << endl;
  87.     }
  88. }
  89.  
  90. int BrowserHistory::countSitesMonth(int month) {
  91.     int localCount = 0;
  92.     for (int i = 0; i < siteCounter; i++) {
  93.         if (history[i].date == month) {
  94.             localCount++;
  95.         }
  96.     }
  97.     return localCount;
  98. }
  99.  
  100. int BrowserHistory::mostVisitedMonth() {
  101.     int months[12] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
  102.     for (int i = 0; i < siteCounter; i++) {
  103.         months[history[i].date - 1]++;
  104.     }
  105.     int max = 0;
  106.     for (int i = 0; i < 12; i++) {
  107.         if (months[i] > max) {
  108.             max = i+1;
  109.         }
  110.     }
  111.     return max;
  112. }
  113.  
  114. void BrowserHistory::DeleteLastSite() {
  115.     siteCounter--;
  116. }
  117.  
  118. BrowserHistory BrowserHistory::concat2(BrowserHistory left, BrowserHistory right) {
  119.     BrowserHistory newHistory(left.siteCounter+right.siteCounter);
  120.     for (int i = 0; i < left.siteCounter; i++) {
  121.         newHistory.history[i] = left.history[i];
  122.     }
  123.     int j = 0;
  124.     for (int i = left.siteCounter; i < left.siteCounter + right.siteCounter; i++){
  125.         newHistory.history[i] = right.history[j];
  126.         j++;
  127.     }
  128.     return newHistory;
  129. }
  130.  
  131. int main()
  132. {
  133.     BrowserHistory mine;
  134.    
  135.     mine.addSite();
  136.     //mine.addSite(5, "google.bg");
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement