Here\'s the link for the .txt files if needed: https://www.dropbox.com/sh/h2y27k
ID: 3593807 • Letter: H
Question
Here's the link for the .txt files if needed: https://www.dropbox.com/sh/h2y27kbuytaz5da/AAAnwCbmvLNnzAMFen4EpKfKa?dl=0
Here's the main.cpp that can't be changed:
/*main.cpp*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//
// function declarations:
//
int GetFileStats(string filename, string& firstKey, string& firstValue);
bool SearchForKey(string filename, string key, string& value, int& rank);
//
// main:
//
int main()
{
string filename;
cin >> filename;
cout << "**Starting**" << endl;
cout << "File: '" << filename << "'" << endl;
cout << endl;
//
// First, let's get some statistics about the data file:
//
int N, rank;
string key, value;
bool found;
N = GetFileStats(filename, key, value);
cout << "# of entries: " << N << endl;
cout << "Top-ranked: (" << key << ", " << value << ")" << endl;
cout << endl;
//
// Now let's input keys from the keyboard and look for them in the file:
//
cin >> key;
while (key != "#")
{
found = SearchForKey(filename, key, value, rank);
if (found == true)
{
cout << ">>Found key: " << key << endl;
cout << " Value= " << value << endl;
cout << " Rank= " << rank << endl;
}
else // not found:
{
cout << ">>Not found: " << key << endl;
}
cin >> key;
}
cout << endl;
cout << "**End**" << endl;
return 0;
}
Here's the search.cpp that needs to be changed for the assignment:
/*search.cpp*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//
// GetFileStats
//
// Returns the # of (key, value) pairs in the given file. Also returns the first (key, value)
// pair via the parameters firstKey and firstValue.
//
// NOTE: it is assumed the file contains at least one (key, value) pair, in this format:
//
// value key
// value key
// value key
// # #
//
// where value and key are one word strings (or numbers that will be input as strings).
// If the file cannot be opened, the program will be exited.
//
int GetFileStats(string filename, string& firstKey, string& firstValue)
{
//
// Initialize return value / parameters:
//
firstKey = "?";
firstValue = "?";
int N = 0;
//
// TODO: open and read the (key, value) pairs from the file. The goal is
// to simply count how many pairs there are and return this value N. Also,
// we need to return the first (key, value) pair via the firstKey and
// firstValue parameters; just assign to the parameters to "return" the
// values. Don't forget to close the file when you're done.
//
return N;
}
//
// SearchForKey
//
// Opens the given file, searches for the given key, and if found, returns true; if the
// key is not found then false is returned.
//
// If the key is found, the value will be returned via the "value" parameter; likewise
// the rank will be returned via the "rank" parameter. The rank is simply the line #
// where the (key, value) pair was found: 1 for first line, 2 for second line, etc.
// If the key is not found, value and rank should be ignored.
//
// NOTE: it is assumed the file contains at least one (key, value) pair, in this format:
//
// value key
// value key
// value key
// # #
//
// where value and key are one word strings (or numbers that will be input as strings).
// If the file cannot be opened, the program will be exited.
//
bool SearchForKey(string filename, string key, string& value, int& rank)
{
//
// initialize return value / parameters:
//
value = "<<not found>>";
rank = 0;
bool found = false; // not found:
//
// TODO: search for the key, and if found, we want to return the associated value.
// But we also need to return the "rank" via the rank parameter; the rank is the
// line number where the (key, value) appears: 1 for first line, 2 for second
// line, and so on. Finally, return true if key was found, false if not.
//
// Don't forget to close the file when you're done.
//
return found;
}
Explanation / Answer
#include"search.cpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//
// function declarations:
//
int GetFileStats(string filename, string& firstKey, string& firstValue);
bool SearchForKey(string filename, string key, string& value, int& rank);
//
// main:
//
int main()
{
string filename;
cout<<" Enter the file name: ";
cin >> filename;
cout << "**Starting**" << endl;
cout << "File: '" << filename << "'" << endl;
cout << endl;
//
// First, let's get some statistics about the data file:
//
int N, rank;
string key, value;
bool found;
N = GetFileStats(filename, key, value);
cout << " # of entries: " << N << endl;
cout << " Top-ranked: (" << key << ", " << value << ")" << endl;
cout << endl;
//
// Now let's input keys from the keyboard and look for them in the file:
//
cout<<" Enter the key to search: ";
cin >> key;
// Loops till key value is not '#'
while (key != "#")
{
found = SearchForKey(filename, key, value, rank);
// Checks the found status if it is true display the information
if (found == true)
{
cout << " >>Found key: " << key << endl;
cout << " Value= " << value << endl;
cout << " Rank= " << rank << endl;
}
else // not found:
{
cout << ">>Not found: " << key << endl;
}
// Accepts key value from the user
cout<<" Enter the key to search: ";
cin >> key;
}// End of while loop
cout << endl;
cout << "**End**" << endl;
return 0;
} //End of main function
Sample Run:
Enter the file name: words-by-freq.txt
**Starting**
File: 'words-by-freq.txt'
# of entries: 8000
Top-ranked: (the, 5627187200)
Enter the key to search: the
>>Found key: the
Value= 5627187200
Rank= 1
Enter the key to search: c++
>>Not found: c++
Enter the key to search: meanwhile
>>Found key: meanwhile
Value= 1692630
Rank= 4152
Enter the key to search: you
>>Found key: you
Value= 604890300
Rank= 17
Enter the key to search: #
**End**