1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
const int MAX_ROWS = 20;
const int MAX_COLUMNS = 20;
const int MAX_WORD_COUNT = 25;
const int MAX_WORD_LENGTH = 32;
const string FILE_NAME = "puzzle.txt";
struct Word
{
char mWords[MAX_WORD_COUNT];
int mRowFound;
int mColumnFound;
bool mbIsFound;
};
void readPuzzle (ifstream, char, int &, int &);
void printPuzzle (int, int, char);
void readWords (ifstream, Word);
void printWords (const Word);
int main ()
{
ifstream inputFile;
char puzzleArray[MAX_ROWS][MAX_COLUMNS];
int Rows = 0;
int Columns = 0;
readPuzzle (inputFile, puzzleArray, Rows, Columns);
printPuzzle (Rows, Columns, puzzleArray);
}
void readPuzzle (ifstream inputFile, char puzzleArray [][MAX_COLUMNS],
int &Rows, int &Columns)
{
inputFile.open(FILE_NAME);
if (inputFile.fail())
{
cout << "Error: failure to open file." << endl;
}
inputFile >> Rows;
inputFile >> Columns;
inputFile.peek();
while (!inputFile.eof())
{
for (int i = 0; i < MAX_ROWS; i++)
{
for (int j = 0; j < MAX_COLUMNS; j++)
{
inputFile >> puzzleArray[i][j];
}
}
}
}
void printPuzzle (int Rows, int Columns, char puzzleArray[][MAX_COLUMNS])
{
cout << "***************************\n";
cout << "* Find A Word *\n";
cout << "***************************\n\n";
cout << "Number of Rows: " << Rows << endl;
cout << "Number of Columns: " << Columns << endl << endl;
cout << "Puzzle\n";
cout << "------\n\n";
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Columns; j++)
{
cout << puzzleArray[i][j];
}
cout << endl;
}
}
void readWords (ifstream inputFile, Word sWords[])
{
int counter = 0;
inputFile.peek();
while (!inputFile.eof() && counter < MAX_WORD_COUNT)
{
inputFile.getline(sWords.mWords[counter], MAX_WORD_LENGTH);
inputFile.get();
counter++;
}
}
void printWords (const Word sWords[])
{
cout << "\nWords\n";
cout << "-----\n\n";
for (int i = 0; i < MAX_WORD_COUNT; i++)
{
cout << sWords.mWords[i];
cout << endl;
}
}
|