Question
The following files have been given to you: 1.A C++ header file (trendtracker.h) declaring the Trendtrackerclass. 2.A C++source file (main.cpp) containing a main function with
The following files have been given to you:
1.A C++ header file (trendtracker.h) declaring the Trendtrackerclass.
2.A C++source file (main.cpp) containing a main function with tests.
3.A text file (short.txt) containing 4 hashtags.
4.A text file (med.txt) containing 20 hashtags.
Create a new C++ source file named trendtracker.cpp that implements the Trendtracker class, so that trendtracker.cpp and the provided files compile into a program that runs with no failed tests. Submit the source file trendtracker.cpp
_________"main.cpp"____________________
#include
#include
#include
#include
#include
#include "trendtracker.h"
using namespace std;
inline void _test(const char* expression, const char* file, int line)
{
cerr << "test(" << expression << ") failed in file " << file;
cerr << ", line " << line << "." << endl;
abort();
}
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
int main()
{
// Setup
vector R;
string s, line;
// Test constructor, size(), popularity(), tweeted()
Trendtracker T1("small.txt");
test(T1.size() == 4);
test(T1.popularity("#algorithms") == 89);
test(T1.popularity("#cs4all") == 19);
test(T1.popularity("#datastructures") == -1);
test(T1.popularity("#programming") == 1965);
test(T1.popularity("#C++") == 3333);
T1.tweeted("#programming");
test(T1.popularity("#algorithms") == 89);
test(T1.popularity("#cs4all") == 19);
test(T1.popularity("#datastructures") == -1);
test(T1.popularity("#programming") == 1966);
test(T1.popularity("#C++") == 3333);
T1.tweeted("#programming");
test(T1.popularity("#algorithms") == 89);
test(T1.popularity("#cs4all") == 19);
test(T1.popularity("#datastructures") == -1);
test(T1.popularity("#programming") == 1967);
test(T1.popularity("#C++") == 3333);
T1.tweeted("#programming");
test(T1.popularity("#algorithms") == 89);
test(T1.popularity("#cs4all") == 19);
test(T1.popularity("#datastructures") == -1);
test(T1.popularity("#programming") == 1968);
test(T1.popularity("#C++") == 3333);
T1.tweeted("#cs4all");
test(T1.popularity("#algorithms") == 89);
test(T1.popularity("#cs4all") == 20);
test(T1.popularity("#programming") == 1968);
test(T1.popularity("#C++") == 3333);
T1.tweeted("#algorithms");
test(T1.popularity("#algorithms") == 90);
test(T1.popularity("#cs4all") == 20);
test(T1.popularity("#datastructures") == -1);
test(T1.popularity("#programming") == 1968);
test(T1.popularity("#C++") == 3333);
T1.tweeted("#cs4all");
test(T1.popularity("#algorithms") == 90);
test(T1.popularity("#cs4all") == 21);
test(T1.popularity("#datastructures") == -1);
test(T1.popularity("#programming") == 1968);
test(T1.popularity("#C++") == 3333);
T1.tweeted("#datastructures");
test(T1.popularity("#algorithms") == 90);
test(T1.popularity("#cs4all") == 21);
test(T1.popularity("#datastructures") == -1);
test(T1.popularity("#programming") == 1968);
test(T1.popularity("#C++") == 3333);
/* Test top_ten_trends().*/
Trendtracker T3("medium.txt");
T3.top_ten_trends(R);
test(R.size() == 20);
test(R[0] == "#linearsearch");
T3.tweeted("#convexhull");
T3.tweeted("#convexhull");
T3.top_ten_trends(R);
test(R[0] == "#convexhull");
test(R[1] == "#linearsearch");
test(R[2] == "#triangulation");
for (int i = 0; i < 100; ++i)
T3.tweeted("#dog");
T3.top_ten_trends(R);
test(R[0] == "#dog");
//Test BiSearch() on T3.S
int index=BiSearch(2012);
std::cout<<"The hashtag we seek for is "<< Inhashtag(index)<
cout << "Assignment complete." << endl;
}
_____"trendtracker.h"____
#ifndef TRENDTRACKER_H
#define TRENDTRACKER_H
#include
#include
using namespace std;
class Trendtracker
{
// For the mandatory running times below:
// n is the number of hashtags in the Trendtracker.
public:
/* Creates a Trendtracker containing hashtags
found in the provided file.
The file is promised to have the following format:
string1, popularity
string2, popularity
...
stringN, popularity
where string1 < string2 < ... < stringN
Must run in O(n) time. */
Trendtracker(string filename);
/* Inserts a hashtag of the popularity pop into the Trendtracker.
If the input hashtag ht exists in Trendtracker, add max{pop, 0} to
the pop value of this hashtage in vector E.
Otherwise, create a new Entry type temp with hashtag equal to ht
and pop equal the maximum value of zero and input pop.
Must run in O(n) time.
*/
void insert(string ht, int pop);
/* Return the number of hashtags in the Trendtracker.
Must run in O(1) time.*/
int size();
/* Adds 1 to the total number times a hashtag has been tweeted.
If the hashtag does not exist in TrendTracker, does nothing.
Must run in O(n) time.
*/
void tweeted(string ht);
/* Returns the number of times a hashtag has been tweeted.
If the hashtag does not exist in Trendtracker, returns -1.
Must run in O(n) time.
*/
int popularity(string name);
/* Fills the provided vector with the 10 most-tweeted hashtags,
in order from most-tweeted to least-tweeted.
If there are fewer than 10 hashtags, then the vector is filled
with all hashtags (in most-tweeted to least-tweeted order).
Meanwhile, store the indices in vector S
in an nonincreasing order of popularity (i.e., in most-tweeted to least-tweeted order)
Must run in O(n) time.
*/
void top_ten_trends(vector &T);
/*
Do binary search on S by the given popularity pop;
Returns the index of E containing an Entry X with X.pop equal to the given popularity pop;
If no such hashtag is found, returns -1.
*/
int BiSearch(int pop);
/*
Return E[index].hashtag
If index is out of range, returns "/n Error: Index is Out Of Range".
*/
string Inhashtag(int index);
private:
// A struct representing a hashtag and
// the number of times it has been tweeted.
struct Entry
{
public:
string hashtag;
int pop;
};
// Entries containing each hashtag and its popularity.
vector E;
/* Stores indices of the ten most-tweeted entries in E. */
vector S;
};
#endif
medium.txt-----
#C++, 1900 #algorithms,89 #cs4all,19 #programming,1965 #datastructures,345 #binarysearch,123 #linearsearch,2018 #convexhull,2017 #triangulation,2012 #linearprogramming,1960 #nearestneighbor,1980 #lineararrange,1970 #rangesearch,1990 #voronoi,2000 #curve,1958 #motion,1989 #cnn,1943 #dog,1923 #rnn,1998 #lstm,1999
small.txt-------
#C++, 3333 #algorithms,89 #cs4all,19 #programming,1965
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started