#include <cctype> #include <fstream> #include <iostream> #include <string> #incl
ID: 3623623 • Letter: #
Question
#include <cctype>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
/// Add more header files if necessary
class counter {
public:
counter();
~counter();
// Loads contents of a file into _words. Returns false if unsuccessful.
bool load(const std::string& file_name);
// Counts letters.
void count();
// Prints character frequencies on the console.
void report() const;
private:
const static size_t _letters = 26;
int _count[_letters]; // letter counts
std::vector _words; // words read from the input file
int _total; // total count (the sum of all elements of _count)
// Converts a char to the corresponding count index.
static int _index(const char c);
// Converts a count index to the corresponding char.
static char _char(const int i);
};
counter::counter() :
_total(0) {
/// Initialize letter counts
}
counter::~counter() {
/// Release any allocated memory or resources
}
bool counter::load(const std::string& file) {
std::ifstream ifstream(file.c_str());
if (!ifstream)
return false;
while (!ifstream.eof()) {
std::string word;
ifstream >> word;
_words.push_back(word);
}
ifstream.close();
return true;
}
/// Define member function count() corresponding to its declaration above.
/// This function should iterate all words loaded from the input file,
/// access each character of each word, and for every character that is a letter,
/// convert the letter to a count index using the static member function _index,
/// and finally increment the corresponding count and the total count.
/// Note: use isalpha(char c) to determine if a character is a letter.
/// Define member function report() corresponding to its declaration above.
/// This function should iterate letter counts and for each count, output the
/// following line to the console:
/// a letter, followed by a colon, the count of the letter, and the frequency
/// of the letter expressed as a percent of the total number of letters.
int counter::_index(const char c) {
char lc = static_cast(tolower(c));
return lc - 'a';
}
char counter::_char(const int i) {
/// Calculate the character value corresponding to index i,
/// replace the '?' returned below with the calculated character code
return '?';
}
int main(int argc, char** argv) {
if (argc != 2)
std::cout << "Usage: counter " << std::endl;
else {
/// Create an instance of counter
/// Load file data, exit if load failed
/// Count letter frequencies
/// Report results
}
}
Explanation / Answer
Dear,
Added only count function and modified code
int countArray[letters];
vector< string > words; // words read from the input file
int total; // total count (the sum of all elements of _count)
// Converts a char to the corresponding count index.
static int index(const char c);
// Converts a count index to the corresponding char.
static char charcount(const int i);
};
counter::counter() :
total(0) {
/// Initialize letter counts
}
counter::~counter() {
/// Release any allocated memory or resources
}
bool counter::load(const std::string& file) {
std::ifstream ifstream(file.c_str());
if (!ifstream)
return false;
while (!ifstream.eof()) {
std::string word;
ifstream >> word;
words.push_back(word);
}
ifstream.close();
return true;
}
void counter::count()
{
for (vector::iterator i =words.begin(); i != words.end(); ++i)
{
string word;
word=words[i];
for(int j=0;j< word.length;j++)
{
ind=index(word[i]);
countArray[ind]++;
total++;
}
}
}
/// Define member function count() corresponding to its declaration above.
/// This function should iterate all words loaded from the input file,
/// access each character of each word, and for every character that is a letter,
/// convert the letter to a count index using the static member function _index,
/// and finally increment the corresponding count and the total count.
/// Note: use isalpha(char c) to determine if a character is a letter.
int counter::_index(const char c)
{
char lc = static_cast(tolower(c));
return lc - 'a';
}
char counter::_char(const int i) {
/// Calculate the character value corresponding to index i,
/// replace the '?' returned below with the calculated character code
return static (i);
}
int main(int argc, char** argv)
{
fstream infile;
if (argc != 2)
std::cout << "Usage: counter " << std::endl;
else {
counter object;
char fname[20];
cout<<"Enter file name:";
cin>>fname;
object.load(fname,ios::in);
object.count();
/// Load file data, exit if load failed
/// Count letter frequencies
/// Report results
}
}
Hope this will help you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.