Coding_for_Crosswords_in_Py.../a.c

71 lines
1.2 KiB
C
Raw Normal View History

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-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-14 21:10:44 +02:00
Grid grid("MY GRID");
2021-07-13 17:27:14 +02:00
2021-07-15 17:45:54 +02:00
grid.LoadFromFile("test");
2021-07-14 21:10:44 +02:00
grid.Check();
grid.Print();
2021-07-11 19:11:55 +02:00
}