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

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...
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.
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++.
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...
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
Write a C program using system call I/O to a) open an existing text file passed...
Write a C program using system call I/O to a) open an existing text file passed to your program as a command line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated content of the file
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT