In: Computer Science
Write a program to read in a list of domain names from the provided file (domains.dat), and print the reverse domain names in sorted order. For example, the reverse domain of cs.princeton.edu is edu.princeton.cs. This computation is useful for web log analysis. To do so, create a data type Domain, using reverse domain name order. The H file contains the required functions. The compareTo function is provided for you. This will allow you to compare 2 Domain objects.
Also use the StringHelper class provided. The H file of StringHelper outlines all the functions included.
The final list is sorted by as if the list were not reversed.
Below are the given files to complete program.
Domain.cpp
#include <algorithm> // used for min function
#include "Domain.h"
using namespace std;
int Domain::compareTo(Domain that)const {
vector<string> thatFields = that.getFields();
for (int i = 0; i < min(n, that.getN()); i++) {
string s = fields[n - i - 1];
string t = thatFields[that.getN() - i - 1];
int c = s.compare(t);
if(c!= 0)
return c;
}
return 0; //strings are equal
}
Domain.h
#include <vector>
#include <string>
class Domain{
private:
std::vector<std::string> fields;
int n; //length of vector
public:
Domain(std::string);
int getN()const;
std::vector<std::string> getFields()const;
std::string printDomain()const;
int compareTo(Domain)const;
};
DomainDriver.cpp
using namespace std;
//Function prototypes
void swap(int, int, vector<Domain>&);
void sort(vector<Domain>&);
int main(){
return 0;
}
domains.dat
sjc.edu.bz
cs.princeston.edu
auckland.ac.nz
belize.scotiabank.com
ub.edu.bz
ox.ac.uk
cs.auckland.ac.nz
cs.famu.edu
geeksforgeeks.org
my.uscis.gov
dcc.godaddy.com
group.bnpparibas
ocw.mit.edu
mail.google.com
law.unsw.edu.au
sesp.northwestern.edu
drive.google.com
unsw.edu.au
amazon.co.uk
letour.fr
StringHelper.h
#ifndef STRING_HELPER
#define STRING_HELPER
#include <string>
#include <sstream>
#include <vector>
class StringHelper{
public:
static std::vector<std::string> parse(std::string , char );
static std::string toUpper(const std::string );
template<typename T>
static std::string toString(const T& );
};
#endif
####################################### Domain.cpp ####################################### #include <algorithm> // used for min function #include "Domain.h" using namespace std; int Domain::compareTo(Domain that)const { vector<string> thatFields = that.getFields(); for (int i = 0; i < min(n, that.getN()); i++) { string s = fields[n - i - 1]; string t = thatFields[that.getN() - i - 1]; int c = s.compare(t); if(c!= 0) return c; } return 0; //strings are equal } Domain::Domain(std::string s) { fields = StringHelper::parse(s, '.'); n = fields.size(); } int Domain::getN() const { return n; } std::vector<std::string> Domain::getFields() const { return fields; } std::string Domain::printDomain() const { int i = n-1; stringstream ss; if(i > 0) { ss << fields[i]; } while(i-- > 0) { ss << "." << fields[i]; } return ss.str(); } ####################################### Domain.h ####################################### #include <vector> #include <string> #include <sstream> #include "StringHelper.h" class Domain{ private: std::vector<std::string> fields; int n; //length of vector public: Domain(std::string); int getN() const; std::vector<std::string> getFields() const; std::string printDomain() const; int compareTo(Domain) const; }; ####################################### StringHelper.cpp ####################################### #include "StringHelper.h" std::vector<std::string> StringHelper::parse(std::string s, char c) { std::stringstream ss(s); std::vector<std::string> result; int n=s.find(c); while(n != std::string::npos) { result.push_back(s.substr(0, n)); s = s.substr(n+1); n = s.find(c); } if(!s.empty()) { result.push_back(s); } return result; } std::string StringHelper::toUpper(const std::string s) { std::string result = ""; for(char c: s) { result += toupper(c); } return result; } template<typename T> static std::string toString(const T& x) { std::stringstream ss; ss << x; return ss.str(); } ####################################### StringHelper.h ####################################### #ifndef STRING_HELPER #define STRING_HELPER #include <iostream> #include <string> #include <sstream> #include <vector> #include <algorithm> class StringHelper { public: static std::vector<std::string> parse(std::string, char ); static std::string toUpper(const std::string); template<typename T> static std::string toString(const T&); }; #endif ####################################### domains.dat ####################################### sjc.edu.bz cs.princeston.edu auckland.ac.nz belize.scotiabank.com ub.edu.bz ox.ac.uk cs.auckland.ac.nz cs.famu.edu geeksforgeeks.org my.uscis.gov dcc.godaddy.com group.bnpparibas ocw.mit.edu mail.google.com law.unsw.edu.au sesp.northwestern.edu drive.google.com unsw.edu.au amazon.co.uk letour.fr ####################################### main.cpp ####################################### #include <iostream> #include <fstream> #include <vector> #include "Domain.h" using namespace std; //Function prototypes void swap(int i, int j, vector<Domain> &v) { Domain t = v[i]; v[i] = v[j]; v[j] = t; } void sort(vector<Domain> &v) { int i, j; for (i = 0; i < v.size()-1; i++) for (j = 0; j < v.size()-i-1; j++) if (v[j].compareTo(v[j+1]) > 0) swap(j, j+1, v); } int main() { vector<Domain> domains; string line; ifstream f("domains.dat"); while(getline(f, line)) { domains.push_back(Domain(line)); } f.close(); sort(domains); for(Domain d: domains) { cout << d.printDomain() << endl; } return 0; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.