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 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream>
#include <fstream>
using namespace std;
//Global CONSTANTS
const int ROWS = 100, // Program Specs given maximum number of rows for field
COLS = 100; // Program specs given maximum number of columns for field
// Function prototypes
void testFile(ifstream &);
void boardSize(ifstream &, int &, int &);
void setBoard(ifstream &, int, int, char[][COLS]);
void formatBoard(int, int, char[][COLS], char[][COLS]);
void output();
int main(){
// Open input and output files specified in program specs
ifstream inputFile("inputdata.dat");
ofstream outputFile("outputdata.dat");
// Declare program variables
int n = 0, // Program Specs given variable for number of rows on field
m = 0; // Program Specs given variable for number of columns on field
char inputField[ROWS][COLS]; // 2D Char Variable Array for field input
char outputField[ROWS][COLS]; // 2D Char Variable Array for field output
testFile(inputFile);
boardSize(inputFile, n, m);
setBoard(inputFile, n, m, inputField);
formatBoard(n, m, inputField, outputField);
output();
system("pause");
return 0;
}
void testFile(ifstream &inputFile){
if (inputFile)
return;
else
cout << "Error opening file. Check for proper file name and try again." << endl;
system("pause");
EXIT_FAILURE;
}
void boardSize(ifstream &inputFile, int &n, int &m){
inputFile >> n;
inputFile >> m;
}
void setBoard(ifstream &inputFile, int n, int m, char inputField[][COLS]){
for(int row = 0; row < n; row++){
for(int column = 0; column < m; column++){
inputFile >> inputField[row][column];
}
}
}
void formatBoard(int n, int m, char inputField[][COLS], char outputField[][COLS]){
for (int row = 0; row < n; row++){
for(int column = 0; column < m; column++){
if (inputField[row][column] == '*'){
outputField[row][column] = '*';
outputField[row][column+1];
outputField[row+1][column];
outputField[row+1][column+1];
}
if (row > 0 && column > 0 && outputField[row-1][column-1] != '*')
++outputField[row-1][column-1];
if (row > 0 && outputField[row-1][column] != '*')
++outputField[row-1][column];
if (row > 0 && outputField[row-1][column+1] != '*')
++outputField[row-1][column+1];
if (column > 0 && outputField[row][column-1] != '*')
++outputField[row][column-1];
if (column > 0 && outputField[row+1][column-1] != '*')
++outputField[row+1][column-1];
}
}
cout << inputField[0][0] << inputField[0][1] << inputField[0][2] << inputField[0][2] << endl;
cout << inputField[1][0] << inputField[1][1] << inputField[1][2] << inputField[1][2] << endl;
cout << inputField[2][0] << inputField[2][1] << inputField[2][2] << inputField[2][2] << endl;
cout << inputField[3][0] << inputField[3][1] << inputField[3][2] << inputField[3][2] << endl;
cout << endl << endl;
cout << outputField[0][0] << outputField[0][1] << outputField[0][2] << outputField[0][2] << endl;
cout << outputField[1][0] << outputField[1][1] << outputField[1][2] << outputField[1][2] << endl;
cout << outputField[2][0] << outputField[2][1] << outputField[2][2] << outputField[2][2] << endl;
cout << outputField[3][0] << outputField[3][1] << outputField[3][2] << outputField[3][2] << endl;
}
void output(){
return;
}
|