Question

In: Computer Science

Write a C++ program to open and read a text file and count each unique token...

Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function.

Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object word stores the token "dream" and its frequency 100:

TokenFreq word;

word.value="dream";

word.freq=100;

Implement the function vector<TokenFreq> getTokenFreq( string inFile_name); This function reads the specified input file line by line, identifies all the unique tokens in the file and the frequency of each token. It stores all the identified (token, freq) pairs in a vector and returns this vector to the calling function. Don't forget to close the file before exiting the function.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

struct TokenFreq
{
   string value;
   int freq;
};

vector<TokenFreq> getTokenFreq(string inFile_Name)
{

}

int main()
{
  
}

Solutions

Expert Solution

#include&lt;iostream&gt;
#include&lt;cstdio&gt;
#include&lt;cstdlib&gt;
using namespace std;

/*
* Node Declaration
*/
struct node
*start, *last;
int counter = 0;
/*
* category Declaration
*/
class double_clist
begin = NULL;
last = NULL;          
}
};

/*
* Main: Contains Menu
*/
int main()
worker = new(struct node);
temp-&gt;info = value;
temp-&gt;next = NULL;
temp-&gt;prev = NULL;
come back temp;
}
/*
*INSERTS component AT starting
*/
void double_clist::insert_begin()
{
int value;
cout&lt;&lt;endl&lt;&lt;"Enter the component to be inserted: ";
cin&gt;&gt;value;
struct node *temp;
temporary worker = create_node(value);
if (start == last &amp;&amp; begin == NULL)
{
cout&lt;&lt;"Element inserted in empty list"&lt;&lt;endl;
begin = last = temp;
start-&gt;next = last-&gt;next = NULL;
start-&gt;prev = last-&gt;prev = NULL;
}
else
{
temp-&gt;next = start;
start-&gt;prev = temp;
begin = temp;
start-&gt;prev = last;
last-&gt;next = start;
cout&lt;&lt;"Element inserted"&lt;&lt;endl;
}
}

/*
*INSERTS ELEMNET eventually
*/
void double_clist::insert_last()
{
int value;
cout&lt;&lt;endl&lt;&lt;"Enter the component to be inserted: ";
cin&gt;&gt;value;
struct node *temp;
temporary worker = create_node(value);
if (start == last &amp;&amp; begin == NULL)
{
cout&lt;&lt;"Element inserted in empty list"&lt;&lt;endl;
begin = last = temp;
start-&gt;next = last-&gt;next = NULL;
start-&gt;prev = last-&gt;prev = NULL;
}
else
  
}
/*
*INSERTS component AT POSITION
*/
void double_clist::insert_pos()
{
int value, pos, i;
cout&lt;&lt;endl&lt;&lt;"Enter the component to be inserted: ";
cin&gt;&gt;value;
cout&lt;&lt;endl&lt;&lt;"Enter the postion of component inserted: ";
cin&gt;&gt;pos;
struct node *temp, *s, *ptr;
temporary worker = create_node(value);
if (start == last &amp;&amp; begin == NULL)
{
if (pos == 1)
{
begin = last = temp;
start-&gt;next = last-&gt;next = NULL;
start-&gt;prev = last-&gt;prev = NULL;
}
else
  
ptr-&gt;next = s-&gt;next;
s-&gt;next-&gt;prev = ptr;
if (pos == counter)
  
counter--;
free(s);
cout&lt;&lt;"Element Deleted"&lt;&lt;endl;
}
/*
* Update worth of a selected node
*/
void double_clist::update()
{
int value, i, pos;
if (start == last &amp;&amp; begin == NULL)
  
s-&gt;info = value;
cout&lt;&lt;"Node Updated"&lt;&lt;endl;
}
/*
* Search component within the list
*/
void double_clist::search()
zero, value, i;
bool flag = false;
struct node *s;
if (start == last &amp;&amp; begin == NULL)
  


Related Solutions

Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Your assignment is to write a C++ program to read a text file containing the information...
Your assignment is to write a C++ program to read a text file containing the information of the employees of a company, load them into memory and perform some basic human resources operations. This assignment will help you practice: multiple file programming, classes, public and private methods, dynamic memory allocation, constructors and destructors, singly linked list and files. Implementation This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of the required classes....
Write a program that reads a text file and reports the total count of words of...
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word...
Query the user for the name of a file. Open the file, read it, and count...
Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.
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.
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT