Write the program as explained in the text and use a map with keys and values. C
ID: 3706160 • Letter: W
Question
Write the program as explained in the text and use a map with keys and values. Create a text file to test the program (samples below). String tokenizing is covered in chapter 10 (pages 600-601). Defining a map – page 1055. Sample main and buildMap shown below. using c++.
Sample function declarations and main:
Sample buildMap function:
// The buildMap function opens a file and builds the word map.
bool buildMap(map> &wordMap)
{
ifstream inputFile; // The input file
string line; // To hold a line from the file
int lineCount = 0; // To hold the line numbers
vector tokens; // To hold tokens
// Open the file.
bool status = openFile(inputFile);
// If the file was successfully opened...
if (status)
{
// Get the first line from the file.
getline(inputFile, line);
// While we're not at the end of the file...
while (inputFile)
{
// Update the line counter.
lineCount++;
// Clear previous tokens.
tokens.clear();
// Split the line into tokens.
split(line, ' ', tokens);
// Create elements from the tokens.
for (auto element : tokens)
{
// Create an empty set.
set <int>mySet;
// Emplace the new element.
wordMap.emplace(element, mySet);
// Add the current line number to the element's set.
wordMap[element].insert(lineCount);
}
// Get the next line from the file.
getline(inputFile, line); }
// Close the input file.
inputFile.close();
}
return status;
}
Sample input file: File Edit Format View Help she sells sea shells b the sea shore and makes a few dollars to feed her sea turtle from the shells that she has sold Sample output file: File Edit Format View Help a: 3 and: 2 by: 1 dollars: 3 feed: 3 few: 3 from: 4 has: 5 her: 3 makes: 2 sea: 1 2 4 sells: 1 she: 15 shells: 1 4 shore: 2 sold.: 5 that: 4 the: 2 4 to: 3 turtle: 4Explanation / Answer
Edits: Implemented trim() function, If the input contains more than 2 spaces consecutively, trim() function required.
=================================================================================
#include "stdafx.h" //Remove this line if are not running in visual studio.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
// Functions prototypes
void split(const string &, char, vector<string> &);
bool openFile(ifstream &);
bool buildmap(map<string, set<int>> &);
void writeIndex(map<string, set<int>>);
string trim(string &);
bool openFile(ifstream &inputFile) {
inputFile.open("input.txt"); //opening input text file for reading
if (!inputFile) { //if not able to open the file
cerr << "Unable to open input.txt file";
return false;
}
return true; // file is opened successfully
}
//Left trim a string
string trim_left(const string& str)
{
const std::string pattern = " ";
return str.substr(str.find_first_not_of(pattern));
}
//Right trim a string
string trim_right(const string& str)
{
const std::string pattern = " ";
return str.substr(0, str.find_last_not_of(pattern) + 1);
}
//Left and Right trim a string
std::string trim(string& str)
{
if (str.length() != 0) //If string is not empty
str = trim_right(str);
//If string is not empty. If a "space" is trimmed, str will have empty string and any operation will cause program to crash.
if (str.length() != 0)
str = trim_left(str);
return str;
}
void split(const string &line, char delim, vector<string> &tokens) {
string token;
auto i = 0;
auto pos = line.find(delim); //finding positioin of delim
while (pos != string::npos) { // if position of the string is not the end
token = line.substr(i, pos - i); //fetch the token
token = trim(token); //trim it
//if(token.compare("") != 0)
if(token.length() != 0) //If it is not empty token then we push into token. i.e. if space is trimmed it will have empty string.
tokens.push_back(token);
i = ++pos;
//find next delim position. If it is end of string, then we push the token.
pos = line.find(delim, pos);
if (pos == string::npos) {
token = line.substr(i, line.length());
token = trim(token);
if (token.length() != 0) //If it is not an empty string.
tokens.push_back(token);
}
}
}
bool buildmap(map<string, set<int>> &wordMap) {
ifstream inputFile; // The input file
string line; // To hold a line from the file
int lineCount = 0; // To hold the line numbers
vector<string> tokens; // To hold tokens TODO
// Open the file.
bool status = openFile(inputFile);
// If the file was successfully opened...
if (status)
{
// Get the first line from the file.
getline(inputFile, line);
// While we're not at the end of the file...
while (inputFile)
{
// Update the line counter.
lineCount++;
// Clear previous tokens.
tokens.clear();
// Split the line into tokens.
split(line, ' ', tokens);
// Create elements from the tokens.
for (auto element : tokens)
{
// Create an empty set.
set <int>mySet;
// Emplace the new element.
wordMap.emplace(element, mySet);
// Add the current line number to the element's set.
wordMap[element].insert(lineCount);
}
// Get the next line from the file.
getline(inputFile, line);
}
// Close the input file.
inputFile.close();
}
return status;
}
void writeIndex(map<string, set<int>> wordMap) {
// open a file to write the wordMap
ofstream outfile("output.txt");
if (!outfile) { //not able to open
cerr << "Unable to open output.txt file ";
}
map <string, set<int>> ::iterator itr; //iterator for map
set <int> ::iterator itrs; //iterator for set
for (itr = wordMap.begin(); itr != wordMap.end(); itr++) {
outfile << itr->first << ":"; // wirte the word to file
// write the set elements (line numbers)
for (itrs = itr->second.begin(); itrs != itr->second.end(); itrs++)
outfile << " " << *itrs;
outfile << endl;
}
outfile.close();
}
int main() {
//define a map with string and set of integers named wordmap
map <string, set<int>> wordMap;
if (buildmap(wordMap)) {
cout << "The map has " << wordMap.size() << " elements. ";
writeIndex(wordMap);
}
system("pause");
return 0;
}
==============================================================
input fie:
she sells sea shells by
the sea shore and makes
a few dollars to feed her
sea turtle from the shells that
she has sold
=====================================
Output:
a: 3
and: 2
by: 1
dollars: 3
feed: 3
few: 3
from: 4
has: 5
her: 3
makes: 2
sea: 1 2 4
sells: 1
she: 1 5
shells: 1 4
shore: 2
sold: 5
that: 4
the: 2 4
to: 3
turtle: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.