Coding_for_Crosswords_in_Py.../a.c
2021-07-15 17:45:54 +02:00

71 lines
1.2 KiB
C

#include <iostream> // library for printing
#include <string> // support for strings
#include <vector> // support for vectors
#include <assert.h>
#include <fstream> // support for reading files
using namespace std;
// For compiling C++ code
// g++ a.c -o a
struct Grid
{
Grid(string n)
{
name = n;
}
int rows() const { return lines.size(); }
int cols() const
{
if (lines.empty())
{
return 0;
}
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);
}
}
}
void Check() const
{
for (string s : lines)
{
assert(s.size() == cols());
}
}
void Print() const
{
cout << "Grid: " << name
<< " (rows=" << rows()
<< ", cols=" << cols() << ")\n";
for (string s : lines)
{
cout << " " << s << "\n";
}
}
string name; // string are initialized empty
vector<string> lines;
};
int main()
{
Grid grid("MY GRID");
grid.LoadFromFile("test");
grid.Check();
grid.Print();
}