Question

In: Computer Science

Write a program to read in a list of domain names from the provided file (domains.dat),...

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

Solutions

Expert Solution

#######################################
          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.


Related Solutions

Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a python program to read from a file the names and grades of a class...
Write a python program to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
Write a pyhton program to read from a file the names and grades of a class...
Write a pyhton program to read from a file the names and grades of a class of students, to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a dataInput() function that reads data from a file and stores it and returns it as a dictionary....
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of tuples, then print the list. [6 marks] Question7: Write Python code to do the following operations using request library. [12 marks] Check if the following webpage is available or not: https://edition.cnn.com/news.html [4 marks] Send a get request to the following webpage and show the result: http://api.open-notify.org/iss-pass.json [4 marks] The webpage from part 2 is expecting some parameters as shown from the results. Now create...
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
C++ Write a program that reads in a list of 10 names as input from a...
C++ Write a program that reads in a list of 10 names as input from a user and places them in an array. The program will prompt for a name and return the number of times that name was entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in. For this lab, use the string data type as opposed to char to...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT