//browser_history.h #ifndef BROWSERHISTORY_H #define BROWSERHISTORY_H #include <
ID: 3794559 • Letter: #
Question
//browser_history.h
#ifndef BROWSERHISTORY_H
#define BROWSERHISTORY_H
#include <string>
#include <stack>
using namespace std;
class BrowserHistory
{
public:
BrowserHistory(string default_url);
string current_url();
void go_to_url(string url);
void back();
bool can_go_back();
int past_url_count();
void forward();
bool can_go_forward();
int future_url_count();
private:
stack<string> past;
stack<string> future;
};
#endif
//********************************************************************************************
//main.cpp
#include <fstream>
#include <iostream>
#include <cassert>
#include "browser_history.h"
int main()
{
// Back: []
// Current: google
// Forward: []
BrowserHistory bh("http://google.com");
assert(bh.current_url() == "http://google.com");
assert(!bh.can_go_back() && !bh.can_go_forward());
assert(bh.past_url_count() == 0 && bh.future_url_count() == 0);
cout << "30% earned." << endl;
// Back: [google, netflix, amazon]
// Current: utrgv
// Forward: []
bh.go_to_url("http://netflix.com");
bh.go_to_url("http://amazon.com");
bh.go_to_url("http://utrgv.edu");
assert(bh.current_url() == "http://utrgv.edu");
cout << "40% earned." << endl;
assert(bh.can_go_back() && !bh.can_go_forward());
cout << "50% earned." << endl;
assert(bh.past_url_count() == 3 && bh.future_url_count() == 0);
cout << "55% earned." << endl;
// Back: [google]
// Current: netflix
// Forward: [amazon, utrgv]
bh.back();
bh.back();
assert(bh.current_url() == "http://netflix.com");
cout << "65% earned." << endl;
assert(bh.can_go_back() && bh.can_go_forward());
cout << "70% earned." << endl;
assert(bh.past_url_count() == 1 && bh.future_url_count() == 2);
cout << "75% earned." << endl;
// Back: [google, netflix]
// Current: amazon
// Forward: [utrgv]
bh.forward();
assert(bh.current_url() == "http://amazon.com");
cout << "80% earned." << endl;
assert(bh.can_go_back() && bh.can_go_forward());
cout << "85% earned." << endl;
assert(bh.past_url_count() == 2 && bh.future_url_count() == 1);
cout << "90% earned." << endl;
// Back: [google, netflix, amazon]
// Current: youtube
// Forward: []
bh.go_to_url("http://youtube.com");
assert(bh.current_url() == "http://youtube.com");
assert(bh.can_go_back() && !bh.can_go_forward());
assert(bh.past_url_count() == 3 && bh.future_url_count() == 0);
bh.forward(); // Can't go forward any more, so do nothing
assert(bh.current_url() == "http://youtube.com");
cout << "95% earned." << endl;
bh.back();
bh.back();
bh.back();
// Back: []
// Current: google
// Forward: [netflix, amazon, youtube]
assert(bh.current_url() == "http://google.com");
bh.back(); // Can't go back any more, so do nothing
assert(bh.current_url() == "http://google.com");
cout << "100% earned." << endl;
}
//Objective
/*
Every modern web browser has the “backward/forward” feature that enables users to traverse the sequence of webpages visited. Pressing the “Back” button revisits the previous webpage, while pressing the “Forward” button returns to the most recently visited webpage. Implement this feature using just two stacks.
Create a new C++ source file named browser history.cpp that implements the class declared in browser history.h, so that main.cpp and browser history.cpp compile into a program that runs with no failed assertions.
*/
Explanation / Answer
// browser_history.cpp
#include "browser_history.h"
#include <string>
BrowserHistory::BrowserHistory(string default_url)
{
future.push(default_url);
}
string BrowserHistory::current_url()
{
return future.top();
}
bool BrowserHistory::can_go_back()
{
return ! past.empty();
}
bool BrowserHistory::can_go_forward()
{
return future_url_count() != 0;
}
void BrowserHistory::forward()
{
if (can_go_forward())
{
string url = future.top();
future.pop();
past.push(url);
}
}
void BrowserHistory::go_to_url(string url)
{
string prev_url = future.top();
future.pop();
past.push(prev_url);
future=stack<std::string>();
future.push(url);
}
void BrowserHistory::back()
{
if (can_go_back())
{
string url = past.top();
past.pop();
future.push(url);
}
}
int BrowserHistory::future_url_count()
{
return future.size() -1;
}
int BrowserHistory::past_url_count()
{
return past.size();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.