Programming C and C++ Need help on the Trend File Output section,need to create
ID: 3689332 • Letter: P
Question
Programming C and C++
Need help on the Trend File Output section,need to create a c and h file with a new class, as well as the main code. Don't know how to start or approach this. The last picture is the sample input/output. The lines of code at the end are the correct hashtag.h and hashtag.cpp files.
Hashtag.h code:
class Hashtag {
private:
string word; // The hashtag itself
int startCount; // Number of occurrences in start file
int endCount; // Number of occurrences in end file
int startRank; // Rank in start file
int endRank; // Rank in end file
public:
Hashtag(string word); // Default constructor
Hashtag(string word, int startCount, int endCount = 0);
// Getter and setter functions for hashtag word, counts, and ranks
string getWord() const;
int getStartCount() const;
int getEndCount() const;
int getStartRank() const;
int getEndRank() const;
void setStartRank(int rank);
void setEndRank(int rank);
// Functions to increment start or end counts
void IncrementStartCount();
void IncrementEndCount();
// Overloaded < operator for Hashtag objects
bool operator<(const Hashtag& rhs) const;
};
Hashtag.cpp code:
Hashtag::Hashtag(string word)
{
this->word = word;
}
Hashtag::Hashtag(string word, int startCount, int endCount = 0)
{
this->word = word;
this->startCount = startCount;
this->endCount = endCount;
}
string Hashtag::getWord() const
{
return this->word;
}
int Hashtag::getStartCount() const
{
return this->startCount;
}
int Hashtag::getEndcount() const
{
return this->endCount;
}
int Hashtag::getStartRank() const
{
return this->startRank;
}
int Hashtag::getEndRank() const
{
return this->endRank;
}
void Hashtag::setStartRank(int rank)
{
this->startRank = rank;
}
void Hashtag::setEndRank(int rank)
{
this->endRank = rank;
}
void Hashtag::IncrementStartCount()
{
this->startCount = this->startCount + 1;
}
void Hashtag::IncrementEndCount()
{
this->endCount = this->endCount + 1;
}
bool Hashtag::operator<(const Hashtag& rhs) const
{
if (rhs.getEndcount() < lhs.getEndcount())
{
return true;
}
if (rhs.getEndcount() == lhs.getEndcount() && rhs.getEndcount() != 0 && lhs.getEndcount() != 0 && lhs.getWord() < rhs.getWord())
{
return true;
}
if (rhs.getEndcount() == 0 && lhs.endCount() == 0 && rhs.getStartCount() < lhs.getStartCount())
{
return true;
}
if (rhs.getEndcount() == 0 && lhs.endCount() == 0 && rhs.getStartCount() == lhs.getStartCount() && lhs.getWord() == rhs.getWord())
{
return true;
}
return false;
}
Explanation / Answer
Hashtag.h code:
class Hashtag {
private:
string word; // The hashtag itself
int startCount; // Number of occurrences in start file
int endCount; // Number of occurrences in end file
int startRank; // Rank in start file
int endRank; // Rank in end file
public:
Hashtag(string word); // Default constructor
Hashtag(string word, int startCount, int endCount = 0);
// Getter and setter functions for hashtag word, counts, and ranks
string getWord() const;
int getStartCount() const;
int getEndCount() const;
int getStartRank() const;
int getEndRank() const;
void setStartRank(int rank);
void setEndRank(int rank);
// Functions to increment start or end counts
void IncrementStartCount();
void IncrementEndCount();
// Overloaded < operator for Hashtag objects
bool operator<(const Hashtag& rhs) const;
};
Hashtag.cpp code:
Hashtag::Hashtag(string word)
{
this->word = word;
}
Hashtag::Hashtag(string word, int startCount, int endCount = 0)
{
this->word = word;
this->startCount = startCount;
this->endCount = endCount;
}
string Hashtag::getWord() const
{
return this->word;
}
int Hashtag::getStartCount() const
{
return this->startCount;
}
int Hashtag::getEndcount() const
{
return this->endCount;
}
int Hashtag::getStartRank() const
{
return this->startRank;
}
int Hashtag::getEndRank() const
{
return this->endRank;
}
void Hashtag::setStartRank(int rank)
{
this->startRank = rank;
}
void Hashtag::setEndRank(int rank)
{
this->endRank = rank;
}
void Hashtag::IncrementStartCount()
{
this->startCount = this->startCount + 1;
}
void Hashtag::IncrementEndCount()
{
this->endCount = this->endCount + 1;
}
bool Hashtag::operator<(const Hashtag& rhs) const
{
if (rhs.getEndcount() < lhs.getEndcount())
{
return true;
}
if (rhs.getEndcount() == lhs.getEndcount() && rhs.getEndcount() != 0 && lhs.getEndcount() != 0 && lhs.getWord() < rhs.getWord())
{
return true;
}
if (rhs.getEndcount() == 0 && lhs.endCount() == 0 && rhs.getStartCount() < lhs.getStartCount())
{
return true;
}
if (rhs.getEndcount() == 0 && lhs.endCount() == 0 && rhs.getStartCount() == lhs.getStartCount() && lhs.getWord() == rhs.getWord())
{
return true;
}
return false;
}
#include <iterator>
#include <iostream>
#include <string>
#include <vector>
int main()
{
Hashtag tag1;
const unsigned int wordsCollection = 6;
int now = 0; // number of words in array
std::vector<std::string> collection;
std::string word;
for (unsigned int i = 0; i < wordsCollection; ++i)
{
std::cin >> word;
collection.push_back(word);
}
std::vector<std::string>::iterator iter = myStrings.begin();
std::vector<std::string>::iterator end = myStrings.end();
int size = sizeof(collection)/sizeof(collection[0])
for(int i=0; i<size; i++)
{
for( int j=i+1;j<size;j++)
{
if(collection[i]==collection[j])
{
for(int k=j; k<size; k++)
collection[k]=collection[k+1];
now++;
}
}
cout<<tag1.startRank<<":"<<collection[j]<<"("<<endRank<<")";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.