#include <iostream>
#include <string>
#include <algorithm> #include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
int get6Words(vector< vector<string> > &vw, istream &input);
int get5Words(vector< vector<string> > &vw, istream &input, int, int);
void printArr(string [][3]);
void printArr2File(ostream &outfile, string [][3]);
void printVvtoFile(ostream &outfile, const vector<vector<string> > &vv, unsigned size);
void bubbleSort(vector<vector<string> > &vw, const unsigned size);
void printVv(const vector<vector<string> > &, unsigned);
bool fit2downWith2across(vector<vector<string> > &vw, string a[][3], unsigned wordCount);
bool gotLastSquare(vector<vector<string> > &vw, string a[][3], unsigned wordCount);
int main() {
char c;
unsigned wordCount;
vector<vector<string> > vw(1200); ifstream sixChem( "sixChemWords.txt", ios::in); if( !sixChem ) {
cerr << "File could not be opened." << endl;
exit(1);
}
wordCount = get6Words(vw, sixChem);
cout << vw[wordCount-1][0];
vw.pop_back(); wordCount -= 1; sixChem.close();
ifstream fiveChem122( "fiveChem122.txt", ios::in);
if( !fiveChem122 ) {
cerr << "File could not be opened." << endl;
exit(1);
}
wordCount = get5Words(vw, fiveChem122, wordCount, 221);
vw.pop_back(); wordCount -= 1;
ifstream fiveChem212( "fiveChem212.txt", ios::in); if( !fiveChem212 ) {
cerr << "File could not be opened." << endl;
exit(1);
}
wordCount = get5Words(vw, fiveChem212, wordCount, 212);
vw.pop_back(); wordCount -= 1;
ifstream fiveChem221( "fiveChem221.txt", ios::in); if( !fiveChem221 ) {
cerr << "File could not be opened." << endl;
exit(1);
}
wordCount = get5Words(vw, fiveChem221, wordCount, 122);
vw.pop_back(); wordCount -= 1;
fiveChem122.close(); fiveChem212.close(); fiveChem221.close();
bubbleSort(vw,wordCount);
ofstream chemWords("chemWordsSorted.txt", ios::out);
printVvtoFile(chemWords, vw, wordCount);
bool gotaMatch = 0; unsigned i,j,k,m,n;
string a[3][3];
ofstream chemSquares("chemSquareSolns.txt", ios::out);
for(i = 0; i < wordCount; ++i) { for(j = 0; j < 3; ++j) a[0][j] = vw[i][j];
k = i+1; while(vw[k][0] == a[0][0] && k < wordCount) {
if(vw[k][1] != a[0][1] &&
vw[k][1] != a[0][2] &&
vw[k][2] != a[0][1] &&
vw[k][2] != a[0][2]) { for(j = 1; j < 3; ++j) a[j][0] = vw[k][j];
m = 0; while(vw[m][0] < a[0][1] && m < wordCount) { ++m; }
n = 0; while(vw[n][0] < a[1][0] && n < wordCount) {
++n; }
for(int q = m; vw[q][0] == a[0][1]; ++q)
for(int r = n; vw[r][0] == a[1][0]; ++r) {
if(vw[q][1]==vw[r][1]) {
a[1][1] = vw[q][1]; a[1][2] = vw[r][2]; a[2][1] = vw[q][2];
if(fit2downWith2across(vw,a,wordCount)) {
if(gotLastSquare(vw,a,wordCount)) {
printArr(a);
printArr2File(chemSquares, a );
} } } } ++k; } } cin >> c; return 0;
} int get6Words(vector<vector<string> > &vw, istream &input) {
string cSymbol = "";
unsigned j=0; input.clear(); char letter;
while( !input.eof() ) {
for(int k = 0; k < 3; ++k) {
cSymbol = "";
for(int i = 0; i < 2; ++ i) {
input >> letter;
cSymbol += letter;
} vw[j].push_back(cSymbol);
} ++j;
} return j;
} int get5Words(vector< vector<string> > &vw, istream &input, int j, int pattern) {
char letter;
string cSymbol; int i,k,p; while( !input.eof() ) {
p = pattern;
for(k = 0; k < 3; ++k) {
cSymbol = "";
for(i = 0; i < p%10; ++i) {
input >> letter; cSymbol += letter; } p /= 10; vw[j].push_back(cSymbol);
} ++j;
} return j;
} bool fit2downWith2across(vector<vector<string> > &vw, string a[][3], unsigned wordCount) {
bool foundFirst = false, foundSecond = false;
unsigned x=0,y=0; while(vw[x][0]<a[2][0] && x < wordCount) ++x;
while(vw[y][0]<a[0][2] && y < wordCount) ++y;
while(x < wordCount && vw[x][0] == a[2][0] && !foundFirst) {
if(vw[x][1] == a[2][1] && a[2][1] != a[0][0]
&& a[2][1] != a[1][0]
&& a[2][1] != a[0][2])
foundFirst = true;
++x;
}
while(y < wordCount && vw[y][0] == a[0][2] && !foundSecond) {
if(vw[y][1] == a[1][2] && a[1][2] != a[0][0]
&& a[1][2] != a[0][1]
&& a[1][2] != a[2][0]
&& a[1][2] != a[2][1])
foundSecond = true;
++y;
}
return (foundFirst && foundSecond);
}
bool gotLastSquare(vector<vector<string> > &vw, string a[][3], unsigned wordCount) {
bool found = false; unsigned x=0,y=0; while(vw[x][0]<a[2][0] && x < wordCount) ++x;
while(vw[y][0]<a[0][2] && y < wordCount) ++y;
while(vw[x][0]==a[2][0] && vw[x][1]<a[2][1] && x < wordCount) ++x;
while(vw[y][0]==a[0][2] && vw[y][1]<a[1][2] && y < wordCount) ++y;
if( vw[x][2]==vw[y][2] && vw[x][2]!=a[0][0]
&& vw[x][2] != a[1][0]
&& vw[x][2] != a[0][1]
&& vw[x][2] != a[1][1])
{ a[2][2] = vw[x][2];
found = true;
}
return found;
}
void printArr(string a[][3]) {
cout << endl << endl;
for(int h = 0; h < 3; ++h) {
for(int i = 0; i < 3; ++i)
cout << a[h][i] << " ";
cout << endl;
}
} void printArr2File(ostream &outfile, string a[][3] ) {
outfile << endl << endl;
for(int h = 0; h < 3; ++h) {
for(int i = 0; i < 3; ++i)
outfile << a[h][i] << " ";
outfile << endl;
}
} void printVv(const vector<vector<string> > &vv, unsigned size) {
int wrap1 = 0;
cout << endl << endl;
for(unsigned i = 0; i < size; ++i) {
for(unsigned j = 0; j < 3; ++j)
cout << vv[i][j];
cout << " ";
++wrap1;
if (wrap1%10 == 0) cout << endl;
}
} void printVvtoFile(ostream &outfile, const vector<vector<string> > &vv, unsigned size) {
int wrap1 = 0;
outfile << endl << endl;
for(unsigned i = 0; i < size; ++i) {
for(unsigned j = 0; j < 3; ++j)
outfile << vv[i][j];
outfile << " ";
++wrap1;
if (wrap1%10 == 0) outfile << endl;
}
} void bubbleSort(vector<vector<string> > &vw, const unsigned size) {
for( unsigned pass = 0; pass < size - 1; pass++)
for( unsigned k = 0; k < size - 1; k++)
if( vw[k][0] > vw[k+1][0])
swap( vw[k],vw[k+1] );
unsigned z = 0;
unsigned symCount = 0;
while (z < size - 1 ) {
symCount = 0;
string currWord = vw[z][0];
while(currWord == vw[z+symCount][0] && z+symCount < size) ++symCount;
for( unsigned pass = 0; pass < size - 1; pass++)
for( unsigned k = z; k < z + symCount - 1; k++)
if( vw[k][1] > vw[k+1][1])
swap( vw[k],vw[k+1] );
z +=symCount;
} }
|