Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- using namespace std;
- struct HistoryEntry{
- public:
- int month;
- char * url;
- void setURL(const char * n)
- {
- delete[] url;
- url = new char[strlen(n) + 1];
- strcpy(url, n);
- }
- void setMonth(int _month)
- {
- month = _month;
- }
- HistoryEntry() :month(-1), url(nullptr){}
- HistoryEntry(int _month, char const* _url) :month(_month), url(nullptr)
- {
- setURL(_url);
- }
- HistoryEntry(HistoryEntry & h)
- {
- this->setMonth(h.getMonth());
- this->setURL(h.getURL());
- }
- char * getURL() const
- {
- char * copy = new char[strlen(url) + 1];
- strcpy(copy, url);
- return copy;
- }
- int getMonth()const{ return month; }
- };
- class BrowserHistory {
- int N;
- HistoryEntry* history;
- int current;
- public:
- BrowserHistory() :N(0), current(-1), history(nullptr){};
- BrowserHistory(int n) :N(n)
- {
- history = new HistoryEntry[N];
- current = 0;
- }
- ~BrowserHistory()
- {
- delete[] history;
- }
- int getN()const {return N;}
- void addWebsite()
- {
- if (current<N)
- {
- int month=0;
- while (month > 12 || month < 1) {
- cout << "month: ";
- cin >> month;
- cout << endl;
- }
- cout << "URL: ";
- char a[100] = {'\0'};
- cin >> a;
- cout << endl;
- history[current].setMonth(month);
- history[current].setURL(a);
- current++;
- }
- }
- void addWebsite(const HistoryEntry& newSite){
- if (current == N)
- cout << "Not enough memory for another site in history"<<endl;
- else
- {
- history[current].setMonth(newSite.getMonth());
- history[current].setURL(newSite.getURL());
- current++;
- }
- }
- void printHisory(){
- for (int i = 0; i<current; i++)
- {
- cout << "date: " << history[i].month << " url: " << history[i].url <<endl;
- }
- }
- int printMonthHistoryCount(int searchedMonth){
- if (searchedMonth>12 || searchedMonth<1)
- {
- cout << "invalid month";
- return -1;
- }
- else
- {
- int counter = 0;
- for (int i = 0; i<current; i++)
- {
- if (history[i].month == searchedMonth)
- {
- counter++;
- }
- }
- return counter;
- }
- }
- int mostActiveMonth()
- {
- int max = 0;
- int maxMonth = 0;
- for (int i = 1; i <= 12; i++)
- {
- int currentMonth = printMonthHistoryCount(i);
- if (currentMonth>max)
- {
- max = currentMonth;
- max = i;
- }
- }
- return max;
- }
- void deleteLastHistoryEntry()
- {
- if (current>=1)
- current--;
- else
- {
- cout << "Cannot delete from empty history! " << endl;
- }
- }
- BrowserHistory& concat(const BrowserHistory &bh1, const BrowserHistory &bh2){
- int N = bh1.getN() + bh2.getN();
- BrowserHistory* newBh = new BrowserHistory(N);
- for (int i = 0; i < bh1.current; i++)
- {
- HistoryEntry entry(bh1.history[i].getMonth(), bh1.history[i].getURL());
- newBh->addWebsite(entry);
- }
- for (int i = 0; i < bh2.current; i++)
- {
- HistoryEntry entry(bh2.history[i].getMonth(), bh2.history[i].getURL());
- newBh->addWebsite(entry);
- }
- return *newBh;
- }
- };
- int main()
- {
- BrowserHistory bh(10);
- BrowserHistory bh2(10);
- bh.addWebsite();
- bh.addWebsite();
- bh.addWebsite();
- HistoryEntry newsite(3, "zdrrrrrrr");
- bh2.addWebsite(newsite);
- bh2.addWebsite(newsite);
- bh2.addWebsite(newsite);
- bh.printHisory();
- bh.deleteLastHistoryEntry();
- cout << "----------------------------Deleting last entry---------------" << endl;
- bh.printHisory();
- cout << endl;
- int month;
- cout << "Number of sites in history for month: ";
- cin >> month;
- cout << "Month " << month << " has " << bh.printMonthHistoryCount(month) << " sites in browser history" << endl;
- cout << "Most active month: " << bh.mostActiveMonth()<<endl;
- BrowserHistory newbh = bh.concat(bh, bh2);
- cout << "Print concatenation of bh and bh2" << endl;
- newbh.printHisory();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement