I've made a program in c++ that takes in a file and the morph to apply to it through the command line. But, so far. My algorithm is taking in a file like :
.............
.............
..XXX.....X..
..XXX.....X..
..XXX........
..XXX........
..XXXXXXX....
..XXXXXXX....
..XXXXXXX....
.............
.............
and prints out something that looks like this :
.............
..X.X.....X..
.XXXXX...XXX.
..XXX.....X..
.XXXXX.......
..XXX.X.X....
.XXXXXXXXX...
..XXXXXXX....
.XXXXXXXXX...
..X.X.X.X....
.............
When it should be printing out something that looks like this:
.............
..XXX.....X..
.XXXXX...XXX.
.XXXXX...XXX.
.XXXXX....X..
.XXXXXXXX....
.XXXXXXXXX...
.XXXXXXXXX...
.XXXXXXXXX...
..XXXXXXX....
.............
I've made my program to go through each and every index and check to see if its the item they want to dilate, then look above, below, left, and right. To add another copy of its index to its surrounding grid. But why is my program printing out like that?
Here is the code I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
//look up line by line parsing
using namespace std;
void replacee(vector<vector<char>> &vec, char oldd, char neww)
{
for (vector<char> &v : vec) // reference to innver vector
{
replace(v.begin(), v.end(), oldd, neww); // standard library algorithm
}
}
void dialationn(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
void erode(vector<vector<char>> &vec, char suspect, char n)
{
for (int i = 0; i < vec.size(); i ++) {
for (int j = 0; j < vec[i].size(); j++) {
if (vec[i][j] == suspect) {
if (i > 0) {
vec[i-1][j] = n;
}
if (j > 0) {
vec[i][j-1] = n;
}
if (i + 1<vec.size()) {
vec[i+1][j] = n;
}
if (j + 1<vec[i].size()) {
vec[i][j+1] = n;
}
}
}
}
replacee(vec, n, suspect);
}
int main(int argc, char* argv[]) {
fstream fin; char ch;
string name (argv[1]); //File Name.
vector<vector<char>> data;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
string argument2 (argv[2]);
while(fin)
{
ch = fin.get();
if(ch!='\n') {
temp.push_back(ch);
}
else
{
data.push_back(temp);
temp.clear();
}
}
if (argument2 == "replace") {
string argument4 (argv[4]);
string argument3 (argv[3]);
replacee(data, argument3[0], argument4[0]);
for (int i = 0; i < data.size(); i ++) {
for (int j = 0; j < data[i].size(); j++) {
cout << data[i][j];
}
cout << endl;
}
}
if (argument2 == "dialate") {
string argument3 (argv[3]);
dialationn(data, argument3[0], 'i');
for (int m = 0; m < data.size(); m ++) {
for (int n = 0; n < data[m].size(); n++) {
cout << data[m][n];
}
cout << endl;
}
}
fin.close();
}
The main function takes the first argument as the text file name to parse it. Then, it looks at the second argument to see what the user wants to do, then based on argument 2, it calls the appropriate function replacee
or dialate
to apply a morph. After the char* containing the file has been modified from one of the functions, it then prints it out.
Note: the dialatee
function changes all the characters it sees fit to 'i' then calls the replacee
function to turn them back into the original output. This is done to prevent the function from reading already modified characters and applying unintentional morphs.