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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include <fstream>
#include <iostream>
using namespace std;
void drawUpDown(fstream &file, bool &pen_status, bool &bold, int &numLength)
{
char c = ' ';
//if pen is up
if (pen_status == false)
{
//loop to draw line/move pointer
for(int i = 1; i <= numLength; i++)
{
//move file pointer up one line
file.seekp(-52, ios::cur);
}
}
//if pen is on canvas/down
if (pen_status == true)
{
//loop to draw line/move pointer
for(int i = 1; i <= numLength; i++)
{
//move pointer down one line
file.seekp(52, ios::cur);
//if bold, use #, otherwise use *
if(bold == true)
{
file << "#";
//move pointer back to offset drawing movement
file.seekp(-1, ios::cur);
}
else
{
//if a bold line is in place, do not overwrite
c = file.peek();
if(c != '#')
{
file << "*";
//move pointer back to offset drawing movement
file.seekp(-1, ios::cur);
}
}
}
}
}
void drawLeftRight(fstream &file, bool &pen_status, bool &bold, int &numLength, char &direction)
{
char c = ' ';
//if pen is up/off of canvas
if (pen_status == false)
{
for(int i = 1; i <= numLength; i++)
{
//move pointer to the end of the line in order to draw to the right
file.seekp((0 - numLength), ios::beg);
}
}
//if pen is down/on canvas
if (pen_status == true)
{
if (direction == ios::cur)
{
//turn left drawing to right drawing
file.seekp((0 - numLength), ios::cur);
}
//loop to draw line
for(int i = 1; i <= numLength; i++)
{
//if bold, use #, otherwise use *
if(bold)
file << "#";
else
{
//if a bold line is in place, do not overwrite
c = file.peek();
if(c != '#')
file << "*";
}
}
}
//if pen is up, move without drawing
else
file.seekp(1, ios::cur);
}
void printFile(fstream &file)
{
//open a file stream to read characters
ifstream print("paint.txt");
//loop through the file char by char
while(!print.eof())
{
//print each character
cout << (char)print.get();
}
//print newlines to add buffer between prints
cout << endl << endl;
//close the file
print.close();
}
int main()
{
//open file stream for commands
ifstream commands("commands.txt");
//create a new file for output called paint.txt
ofstream create("paint.txt");
create.close();
//convert paint to fstream to access i/o functions.
fstream paint("paint.txt");
char c, direction, ch = ' ';
bool pen_status, bold = false;
int length = 0;
//loop to set up 50x50 canvas to draw on
for(int i = 1; i <= 50; i++)
{
for(int j = 1; j <= 50; j++)
paint.put(' ');
paint.put('\n');
}
//reset pointer to start of file
paint.seekp(0, ios::beg);
//loop through commands char by char
while(commands.get(c))
{
//if input command is 1, pen is raised
if(c == '1')
pen_status = false;
//if input command is 2, pen is on canvas
else if(c == '2')
pen_status = true;
//if input command is 3, get ready to call functions to draw on canvas
else if(c == '3')
{
//determine the direction by skipping any spaces and commas that are found in input file
do commands.get(ch);
while((ch == ' ')||(ch == ','));
direction = ch;
//read numlength
commands.unget();
commands >> length;
//if direction goes up, call drawUpDown
if((direction == 'N')||(direction == 'n')||(direction == 'S')||(direction == 's'))
drawUpDown(paint, pen_status, bold, length);
//any other command of direction call draw LeftRight
else
drawLeftRight(paint, pen_status, bold, length, direction);
}
//if command is 4, print output to the console
else if(c == '4')
printFile(paint);
//if command is B, set bold to on
else if(c == 'B')
bold = true;
//if command is b, set bold to off
else
bold = false;
}
//print final canvas
printFile(paint);
//close files.
commands.close();
paint.close();
return 0;
}
|