iPad 738 AM Write a program that reads a text file and count

iPad 7:38 AM Write a program that reads a text file and counts the number of times each English word appears. Display the 25 most frequently used words. 1 of 2 The program requires two classes, the main program and a separate class called UseCount to hold a word and its count. The UseCount class needs two instance variables, one String to hold the English word and an int to hold the count of times this word appeared in the text. A constructor for UseCount should have one parameter to initialize the English word. The count should be initialized to one. The main program needs to sort an array of UseCount objects. To be sorted, the UseCount class must implement the Comparable interface and include a method compareTo. The class statement for UseCount should look like: public class UseCount implements Comparable { and the compareTo method in UseCount should be: For sorting with the Comparable interface/ public int compareTo (UseCount other return other.count-this.count The main program should create an array that can hold 10,000 UseCount objects. After asking the user for the input filename, it should read one English word at a time from the file using the next ) method of a Scanner object. For each word read, search the array of UseCount objects for an object that contains the word just read (ignoring case). If found, increment the count in that object. Ifnot found, create a new object for that word and put it in the array. You will want to keep a count of the number of UseCount objects in the array After all words have been read from the input file, you can sort the array of UseCount object in descending order by the count. There is a static method to sort an array of objects.

Solution

#include #include #include #include #include #include using namespace std; vector lines; vector second; set words; multiset multiwords; void readLines(const char *filename) { string line; ifstream infile; infile.open(filename); if (!infile) { cerr << filename << \" cannot open\" << endl; return; } getline(infile, line); while (!infile.eof()) { lines.push_back(line); getline(infile, line); } infile.close(); } int binary_search(vector &v, int size, int value) { int from = 0; int to = size - 1; while (from <= to) { int mid = (from + to) / 2; int mid_count = multiwords.count(v[mid]); if (value == mid_count) return mid; if (value < mid_count) to = mid - 1; else from = mid + 1; } return from; } int main() { vector words; string x; ifstream inFile; int count = 0; inFile.open(\"bible.txt\"); if (!inFile) { cout << \"Unable to open file\"; exit(1); } while (inFile >> x){ sort(words.begin(), words.end()); } for(int i = 0;i < second.size();i++) { if(x == x+1) { count++; } else return; } inFile.close(); }
 iPad 7:38 AM Write a program that reads a text file and counts the number of times each English word appears. Display the 25 most frequently used words. 1 of 2

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site