//trendtracker.h
#ifndef TRENDTRACKER_H
#define TRENDTRACKER_H
#include <vector>
#include <string>
using namespace std;
class Trendtracker
{
public:
Trendtracker();
void insert(string ht);
int size();
void tweeted(string ht);
int popularity(string name);
string top_trend();
void top_three_trends(vector<string>
&T);
void top_k_trends(vector<string> &T, int
k);
private:
class Entry
{
public:
string hashtag;
int pop;
};
vector<Entry> E;
};
#endif
//trendtracker.cpp
#include"trendtracker.h"
using namespace std;
// Creates a new Trendtracker tracking no hashtags.
//
// Must run in O(1) time.
Trendtracker::Trendtracker(){
}
// Inserts a hashtag (tweeted 0 times) into the
Trendtracker.
// If the hashtag already is in Trendtracker, does nothing.
//
// Must run in O(n) time.
void Trendtracker::insert(string ht){
Entry t;
int n = E.size();
t.hashtag = ht;
t.pop = 0;
for (int i = 0; i < n; i++) {
if (ht == E[i].hashtag) {
break;
}
}
}
// Return the number of hashtags in the Trendtracker.
//
// Must run in O(1) time.
int Trendtracker::size(){
return E.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 Trendtracker::tweeted(string ht) {
int n = E.size();
for (int i = 0; i < n; i++) {
if (E[i].hashtag == ht) {
E[i].pop++;
}
}
}
// 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 Trendtracker::popularity(string name){
int i = 0;
int n = E.size();
bool check = 0;
for (int i = 0; i < n; i++) {
if (name == E[i].hashtag) {
check = 1;
}
}
if (check == 1) {
return E[i].pop;
}
else {
return -1;
}
}
// Returns a most-tweeted hashtag.
// If the Trendtracker has no hashtags, returns "".
//
// Must run in O(n) time.
string Trendtracker::top_trend(){
if (E.size() == 0) {
return "";
}
int popular;
for (int i = 0, n = E.size(), popular = 0; i < n;
i++) {
if (E[i].pop > E[popular].pop)
{
popular =
i;
}
}
return E[popular].hashtag;
}
// Fills the provided vector with the 3 most-tweeted
hashtags,
// in order from most-tweeted to least-tweeted.
//
// If there are fewer than 3 hashtags, then the vector is
filled
// with all hashtags (in most-tweeted to least-tweeted
order).
//
// Must run in O(n) time.
void Trendtracker::top_three_trends(vector<string>
&T){
int f, s, t;
int n = E.size();
for (int i = 0, f = s = t = n; i < n; i++) {
if (E[i].pop > E[f].pop) {
t = s;
s = f;
f = i;
}
else if (E[i].pop > E[s].pop)
{
t = s;
s = i;
}
else if(E[i].pop > E[t].pop)
{
t = i;
}
}
T.push_back(E[f].hashtag);
if (s != n && s != f)
T.push_back(E[s].hashtag);
if (t != n && t != s)
T.push_back(E[t].hashtag);
}
// Fills the provided vector with the k most-tweeted
hashtags,
// in order from most-tweeted to least-tweeted.
//
// If there are fewer than k hashtags, then the vector is
filled
// with all hashtags (in most-tweeted to least-tweeted
order).
//
// Must run in O(nk) time.
void Trendtracker::top_k_trends(vector<string> &T, int
k){}
Try this code for trendtracker
#include "trendtracker.h"
Trendtracker::Trendtracker(string filename){
ifstream myFile;
myFile.open(filename);
string tag;
if(myFile.fail()){
cerr << "Error opening file" << endl;
exit(1);
}
while( myFile >> tag){
Entry e;
e.hashtag = tag;
e.pop = 0;
E.push_back(e);
}
myFile.close();
}
// Return the number of hashtags in the Trendtracker.
//
// Must run in O(1) time.
int Trendtracker::size(){
return static_cast<int>(E.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(log(n)) time.
void Trendtracker::tweeted(string ht){
//uses the index that search returns to access the given hashtag
and increase its popularity
E[search(ht)].pop++;
//check for duplicates
bool duplicate = false;
for(int i = 0; i < S.size(); i++){
if(S[i] == search(ht))
duplicate = true;
}
if(!duplicate)
S.push_back(search(ht));
//sort the elements of S by the pops in the entrys they refer
to(from large to small)
//insertion sort
for(int i = 0; i < S.size(); i++){
int j = i;
while(j > 0 && E[S[j]].pop > E[S[j-1]].pop){
int temp = S[j-1];
S[j-1] = S[j];
S[j] = temp;
j--;
}
}
//if s has more than 3 elements, remove the last one
if(S.size() > 3)
S.pop_back();
//printS();
}
// Returns the number of times a hashtag has been tweeted.
// If the hashtag does not exist in Trendtracker, returns -1.
//
// Must run in O(log(n)) time.
int Trendtracker::popularity(string name){
if(search(name) != -1)
return E[search(name)].pop;
return -1;
}
// Returns a most-tweeted hashtag.
// If the Trendtracker has no hashtags, returns "".
//
// Must run in O(1) time.
string Trendtracker:: top_trend(){
if(E.size() == 0)
return "";
return E[S[0]].hashtag;
}
// Fills the provided vector with the 3 most-tweeted
hashtags,
// in order from most-tweeted to least-tweeted.
//
// If there are fewer than 3 hashtags, then the vector is
filled
// with all hashtags (in most-tweeted to least-tweeted
order).
//
// Must run in O(1) time.
void Trendtracker::top_three_trends(vector<string>
&T){
T.clear();
if(E.size() == 1) //test case for tiny.txt
T.push_back(E[0].hashtag);
else{
if(S.size() == 0){
T.push_back(E[0].hashtag);
T.push_back(E[1].hashtag);
T.push_back(E[1].hashtag);
}
else if(S.size() == 1){
T.push_back(E[S[0]].hashtag);
T.push_back(E[1].hashtag);
T.push_back(E[2].hashtag);
}
else if(S.size() == 2){
T.push_back(E[S[0]].hashtag);
T.push_back(E[S[1]].hashtag);
T.push_back(E[2].hashtag);
}else if(S.size() == 3){
T.push_back(E[S[0]].hashtag);
T.push_back(E[S[1]].hashtag);
T.push_back(E[S[2]].hashtag);
}
}
}
// Optional helper method.
// Returns the index of E containing an Entry with hashtag
ht.
// If no such hashtag is found, returns -1.
//
// Should run in O(log(n)).
int Trendtracker::search(string ht){
int low = 0;
int high = E.size() - 1;
while(low <= high){
int mid = ((high + low)/2);
if(E[mid].hashtag < ht)
low = mid + 1;
else if(E[mid].hashtag > ht)
high = mid - 1;
else
return mid; //found
}
return -1;
}
//trendtracker.h #ifndef TRENDTRACKER_H #define TRENDTRACKER_H #include <vector> #include <string> using namespace std; class Trendtracker { public:...
//stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what) : what(what) {} string getWhat() {return what;} private: string what; }; #endif //stack_test_app.cpp #include <iostream> #include <sstream> #include <string> #include "stack.h" using namespace std; /********************************************* * The 'contains' function template goes here * *********************************************/ int main() { cout << boolalpha; cout << "--- stack of int" << endl; Stack<int> si; cout << "si intially " << si << endl; ...
#ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...
/ Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...
Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public: Server(); Server(string, int); ~Server(); string getPiece(int); private: string *ascii; mutex access; }; #endif -------------------------------------------------------------------------------------------------------------------------- Server.cpp #include "Server.h" #include #include Server::Server(){} Server::Server(string filename, int threads) { vector loaded; ascii = new string[threads]; ifstream in; string line; in.open(filename); if (!in.is_open()) { cout << "Could not open file...
In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...
#include using namespace std; vector split_string(string); // Complete the findMedian function below. int findMedian(vector arr) { } int main() { ofstream fout(getenv("OUTPUT_PATH")); int n; cin >> n; cin.ignore(numeric_limits::max(), '\n'); string arr_temp_temp; getline(cin, arr_temp_temp); vector arr_temp = split_string(arr_temp_temp); vector arr(n); for (int i = 0; i < n; i++) { int arr_item = stoi(arr_temp[i]); arr[i] = arr_item; } int result = findMedian(arr); fout << result << "\n"; fout.close(); return 0; } vector split_string(string input_string) { string::iterator new_end = unique(input_string.begin(), input_string.end(), []...
Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) { vector <int> v1; // Print the number of 2s that divide n while (n%2 == 0) { printf("%d ", 2); n = n/2; v1.push_back(2); } // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i <=...
#include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int N = nums.size(); vector<int> result; vector<int> a(101); vector<int> b(101); for (int i = 0; i < N; i++) { a[nums[i]]++; // what does this mean? } for (int i = 1; i < 101; i++) { b[i] = a[i - 1] + b[i - 1]; } for (int i = 0; i < N; i++) { result.push_back(b[nums[i]]); } for (int i = 0; i...
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...
#include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...