2021-07-13 17:27:14 +02:00
|
|
|
#include <iostream> // library for printing
|
|
|
|
#include <string> // support for strings
|
|
|
|
#include <vector> // support for vectors
|
|
|
|
#include <assert.h>
|
2021-07-15 17:45:54 +02:00
|
|
|
#include <fstream> // support for reading files
|
2021-07-13 17:27:14 +02:00
|
|
|
using namespace std;
|
2021-07-11 19:11:55 +02:00
|
|
|
|
|
|
|
// For compiling C++ code
|
|
|
|
// g++ a.c -o a
|
2021-07-16 19:17:37 +02:00
|
|
|
|
|
|
|
class Library {
|
|
|
|
public:
|
|
|
|
void ComputeStats() {
|
|
|
|
assert(counts.empty());
|
|
|
|
counts.resize(18);
|
|
|
|
for (string s : words) {
|
|
|
|
int len = s.length();
|
|
|
|
if (len < 18) {
|
|
|
|
counts[len]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void PrintStats() const {
|
|
|
|
cout << "Here are the counts of each word length:\n";
|
|
|
|
for (int i=1; i<counts.size(); i++) {
|
|
|
|
cout << "[" << i << "] " << counts[i] << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
string GetWord(int i) const {
|
|
|
|
assert(i >= 0 && i < words.size());
|
|
|
|
return words[i];
|
|
|
|
}
|
|
|
|
void ReadFromFile(string filename) {
|
|
|
|
ifstream f;
|
|
|
|
f.open(filename);
|
|
|
|
while (!f.eof())
|
|
|
|
{
|
|
|
|
string line;
|
|
|
|
getline(f, line);
|
|
|
|
// cout << line << "\n";
|
|
|
|
if (!line.empty())
|
|
|
|
{
|
|
|
|
int len = line.length();
|
|
|
|
if (line[len-1] == '\r') {
|
|
|
|
line = line.substr(0, len-1);
|
|
|
|
}
|
|
|
|
words.push_back(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cout << "Read " << words.size() << " words from file '"
|
|
|
|
<< filename << "'\n";
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
vector<string> words;
|
|
|
|
vector<int> counts;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-07-14 21:10:44 +02:00
|
|
|
struct Grid
|
|
|
|
{
|
2021-07-15 17:45:54 +02:00
|
|
|
Grid(string n)
|
|
|
|
{
|
2021-07-14 21:10:44 +02:00
|
|
|
name = n;
|
|
|
|
}
|
|
|
|
int rows() const { return lines.size(); }
|
2021-07-15 17:45:54 +02:00
|
|
|
int cols() const
|
|
|
|
{
|
|
|
|
if (lines.empty())
|
|
|
|
{
|
2021-07-14 21:10:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2021-07-15 17:45:54 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return lines[0].size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LoadFromFile(string filename)
|
|
|
|
{
|
|
|
|
ifstream f;
|
|
|
|
f.open("test");
|
|
|
|
while (!f.eof())
|
|
|
|
{
|
|
|
|
string line;
|
|
|
|
getline(f, line);
|
|
|
|
// cout << line << "\n";
|
|
|
|
if (!line.empty() && line[0] != '#')
|
|
|
|
{
|
|
|
|
lines.push_back(line);
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 21:10:44 +02:00
|
|
|
}
|
2021-07-15 17:45:54 +02:00
|
|
|
void Check() const
|
|
|
|
{
|
2021-07-14 21:10:44 +02:00
|
|
|
for (string s : lines)
|
|
|
|
{
|
|
|
|
assert(s.size() == cols());
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 17:45:54 +02:00
|
|
|
void Print() const
|
|
|
|
{
|
2021-07-14 21:10:44 +02:00
|
|
|
cout << "Grid: " << name
|
2021-07-15 17:45:54 +02:00
|
|
|
<< " (rows=" << rows()
|
|
|
|
<< ", cols=" << cols() << ")\n";
|
|
|
|
for (string s : lines)
|
|
|
|
{
|
2021-07-14 21:10:44 +02:00
|
|
|
cout << " " << s << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
string name; // string are initialized empty
|
|
|
|
vector<string> lines;
|
|
|
|
};
|
2021-07-11 19:11:55 +02:00
|
|
|
|
2021-07-13 17:27:14 +02:00
|
|
|
int main()
|
|
|
|
{
|
2021-07-16 19:17:37 +02:00
|
|
|
Library lib;
|
|
|
|
lib.ReadFromFile("top_12000.txt");
|
|
|
|
lib.ComputeStats();
|
|
|
|
lib.PrintStats();
|
2021-07-13 17:27:14 +02:00
|
|
|
|
2021-07-16 19:17:37 +02:00
|
|
|
//Grid grid("MY GRID");
|
|
|
|
//grid.LoadFromFile("test");
|
|
|
|
//grid.Check();
|
|
|
|
//grid.Print();
|
2021-07-11 19:11:55 +02:00
|
|
|
}
|