Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with writing this code! Thanks in advance. I can\'t figure out th

ID: 3537814 • Letter: P

Question

Please help me with writing this code! Thanks in advance. I can't figure out the functions. I got the class but please provide the class as well. I am unsure being correct. Thanks in advance, again.

Web browsers commonly allow you to navigate through a "history" of web pages which have previously been visited. The mechanism is somewhat like a stack, in that the most recently visited pages are at the top of the history and revisited when the "back" button is pressed.

However, the history does not really have infinite capacity. In reality, there may exist a fixed limit on the size of the history. The issue arises as to what should happen when the capacity is exhausted and a new item is pushed onto the history. If it only has room to save 50 pages in its history and yet you visit more pages, it will handle the overflow by making room in the history for a new page by throwing away the page which is on the very bottom of the history (i.e., the least recently visited page). The oldest url is removed from the least-recently viewed end of the history, then a new url is added to the most-recently viewed end of the history.

In this assignment, we define a new ADT (class) which we call a BrowserHistory. You will be required to give two different implementations that inherit from the parent BrowserHistory class, as described below.

Your program should meet the functional requirements and include a minimum of the following:

Use the input file weblog_unique.url as input data. Feel free to optionally use the getData function from the weblogclient.cpp of project 1 for the file input. Or if you'd rather write your own file input function but need a refresher on file input, check out this page: C++ File Input/Output (scroll down to Text Files section)

Create a container class called BrowserHistory. Optionally, the BrowserHistory class can be an abstract class, but this is not required. Two classes must inherit from BrowserHistory: BrowserHistoryA and BrowserHistoryB as described above. Restrict the size of each container to 50 urls maximum.

Create functions for your implementation that includes at a minimum the following functions. You decide which class or classes the functions should belong to.

A function for adding an item to the history that properly handles overflow when the history is full as directed above.

A function for displaying the most recently added page in the history.

A function for displaying the entire history.

A client program that uses all of the functions of your classes. The client function should call the display function after loading the entire input file to demonstrate its contents. If the overflow is handled correctly, the history should contain the last 50 urls from the input file.

Explanation / Answer

#include<iostream>
#include<fstream>
#include <list>
#include <vector>
#include<string>
using namespace std;

const int MAX_URLS=50;
class BrowserHistory
{
protected:
    string recent_url;
    int no_of_urls;
public:
BrowserHistory()
{
    recent_url = "";
    no_of_urls=0;
}
virtual void add_to_array(string url) {};
string most_recently_accessed_url()
{
    // to get most recently surfed URL.
return recent_url;
}
virtual void display_history(){};
};

class BrowserHistoryA:public BrowserHistory
{
private:
list<string> URL_A;
public:
BrowserHistoryA():BrowserHistory()
{
}
void add_to_array(string url)
{
recent_url = url;
if(no_of_urls == MAX_URLS)
{
//cout << " got a new URL in browser history A so deleting " << *(URL_A.begin()) <<endl;
//cout << " new URL is " << url << endl;
   //once it reaches 50 remove from back and add to front.
URL_A.pop_back();
URL_A.push_front(url);
}
else
{
URL_A.push_front(url);
no_of_urls++;
}
}
void display_history()
{
for(list<string>::iterator i=URL_A.begin(); i != URL_A.end(); ++i)
cout << *i <<endl;
}
};

class BrowserHistoryB:public BrowserHistory
{
private:
vector<string> URL_B;
public:
BrowserHistoryB():BrowserHistory()
{
}
void add_to_array(string url)
{
recent_url = url;
if(no_of_urls == MAX_URLS)
{
//cout << " got a new URL in browser history B so deleting " << *(URL_B.begin()) <<endl;
//cout << " new URL is " << url << endl;
   //once it reaches 50 remove from front and add to back;
URL_B.erase(URL_B.begin());
URL_B.push_back(url);
}
else
{
URL_B.push_back(url);
no_of_urls++;
}
}
void display_history()
{
for(vector<string>::iterator i=URL_B.begin(); i != URL_B.end(); ++i)
cout << *i << endl;
}
};

int main()
{
BrowserHistory *BA = new BrowserHistoryA();
BrowserHistory *BB = new BrowserHistoryB();
string str;
ifstream input("input.txt");
if(!input)
{
cout << " unable to open that file. exiting "<<endl;
return 0;
}
while(!input.eof())
{
getline(input,str);
BA->add_to_array(str);
BB->add_to_array(str);
}
input.close();
cout << "from browser history A, most recently accessed url " << BA->most_recently_accessed_url() <<endl;
cout << "from browser history B, most recently accessed url " << BB->most_recently_accessed_url() <<endl;
cout << "printing browser history of A" << endl;
BA->display_history();
cout << "printing browser history of B" << endl;
BB->display_history();
//system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote