Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Header File: #ifndef BROWSERHISTORY_H #define BROWSERHISTORY_H #include #include stack.h using namespace std; class BrowserHistory { public: // Creates a new browser history with only one

Header File:

#ifndef BROWSERHISTORY_H #define BROWSERHISTORY_H

#include #include "stack.h"

using namespace std;

class BrowserHistory { public: // Creates a new browser history with only one page visited: default_url BrowserHistory(string default_url);

// Returns the current page. string current_url();

// Moves the browser to a new page url via, // e.g., clicking a link, typing into the address bar, etc. void go_to_url(string url);

// Moves back (into the past) by one url. // // If there is no past url to move to, does nothing. void back();

// Returns whether there is a url in the past, // i.e. whether the back button can be pushed. bool can_go_back();

// Returns how many urls are in the past, // i.e. how many times in a row the back button could be pushed. int past_url_count();

// Moves forward (into the future) by one url. // // If there is no future url to move to, does nothing. void forward();

// Returns whether there is a url in future, // i.e. whether the future button can be pushed. bool can_go_forward();

// Returns how many urls are in the future, // i.e. how many times in a row the forward button could be pushed. int future_url_count();

private: // Two stacks of strings - these are the only variables you need! // See http://www.cplusplus.com/reference/stack/stack/ for class reference. Stack past; Stack future; };

#endif

Main File:

#include #include "browserhistory.h"

inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file << ", line " << line << "." << endl; abort(); }

#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

void interactive_mode() { BrowserHistory bh("http://google.com"); cout << "Starting at " << bh.current_url() << "." << endl; cout << "Choose (B)ack, (F)orward, or enter a url to go to." << endl;

string line; while (cin) { string line; getline(cin, line); if (line == "B") { if (bh.can_go_back()) { bh.back(); cout << " Now at " << bh.current_url() << endl; } else cout << " Cannot go back." << endl;

} else if (line == "F") { if (bh.can_go_forward()) { bh.forward(); cout << " Now at " << bh.current_url() << endl; } else cout << " Cannot go forward." << endl; } else if (line.size() > 0) { bh.go_to_url(line); cout << " Now at " << bh.current_url() << endl; } } exit(0); }

int main() { // Uncomment line below to use your BrowserHistory interactively. // // interactive_mode();

// Comments below indicate the current (expected) state of the history. // Example: [url1, url2, url3, (url4), url5, url6] // The urls are listed oldest to newest from left to right, // and the url in parentheses is the current url.

// History: [(google.com)] BrowserHistory bh("http://google.com"); test(bh.current_url() == "http://google.com"); test(!bh.can_go_back()); test(!bh.can_go_forward()); test(bh.past_url_count() == 0); test(bh.future_url_count() == 0);

bh.go_to_url("http://netflix.com"); bh.go_to_url("http://amazon.com"); bh.go_to_url("http://utrgv.edu"); // History: [google.com, netflix.com, amazon.com, (utrgv.edu)] test(bh.current_url() == "http://utrgv.edu"); test(bh.can_go_back()); test(!bh.can_go_forward()); test(bh.past_url_count() == 3); test(bh.future_url_count() == 0);

bh.back(); bh.back(); // History: [google.com, (netflix.com), amazon.com, utrgv.edu] test(bh.current_url() == "http://netflix.com"); test(bh.can_go_back()); test(bh.can_go_forward()); test(bh.past_url_count() == 1); test(bh.future_url_count() == 2);

bh.forward(); // History: [google.com, netflix.com, (amazon.com), utrgv.edu] test(bh.current_url() == "http://amazon.com"); test(bh.can_go_back()); test(bh.can_go_forward()); test(bh.past_url_count() == 2); test(bh.future_url_count() == 1);

// History: [google.com, netflix.com, amazon.com, (youtube.com)] bh.go_to_url("http://youtube.com"); test(bh.current_url() == "http://youtube.com"); test(bh.can_go_back()); test(!bh.can_go_forward()); test(bh.past_url_count() == 3); test(bh.future_url_count() == 0); bh.forward(); // Can't go forward any more, so do nothing test(bh.current_url() == "http://youtube.com");

bh.back(); bh.back(); bh.back(); // History: [(google.com), netflix.com, amazon.com, youtube.com] test(bh.current_url() == "http://google.com"); bh.back(); // Can't go back any more, so do nothing test(bh.current_url() == "http://google.com");

// Larger test string url("http://url00.com"); for (int i = 0; i < 100; ++i) { url[10] = i / 10 + '0'; url[11] = i % 10 + '0'; bh.go_to_url(url); } // History: [url00.com, url01.com, url02.com, ..., (url99.com)] for (int i = 0; i < 49; ++i) bh.back(); // History: [url00.com, url01.com, ..., (url50.com), ..., url99.com] test(bh.current_url() == "http://url50.com");

cout << "Assignment complete." << endl; system("pause"); }

stack:

#include "stack.h" #include using namespace std;

Stack::Stack() { count = 0; }; void Stack::push(string item) { data[count] = item; count++; }; void Stack::pop() { if (count > 0) { count--; } else { data[count] = ""; }

}; string Stack::top() { if (count > 0) { return data[count - 1]; } else { return ""; }

}; int Stack::size() {

return count;

};

Implement the body of the header functions to display "assignment complete" with no errors or warnings!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago