Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a class that represents ASCII art images and operations on them, respresented as two-dimensional char arrays allocated on the heap (using the new keyword).

Implement a class that represents ASCII art images and operations on them, respresented as two-dimensional char arrays allocated on the heap (using the new keyword).

Using canvas.h and main.cpp files below Create a new C++ source file named canvas.cpp that implements the Canvas class, so that canvas.cpp and the provided files compile into a program that runs with no failed tests.

(canvas.h)

#ifndef CANVAS_H

#define CANVAS_H

#include

using namespace std;

// manipulate ASCII art images

// consisting of a rectangular grid of chararacter pixels.

class Canvas

{

public:

// Allocates a canvas of the given width and height 5 that

// consists entirely of ' ' (space) chars.

Canvas(int width);

// Allocates a canvas with width 5 and height 5 that looks like:

//

// ### #### #### ####

// # # # # # # #

// ##### or #### or # or # #

// # # # # # # #

// # # #### #### ####

//

// depending upon which character ('A', 'B', 'C', or 'D') is

// given as a parameter. If some other character is given,

// allocates a canvas of ' ' chars with width 5 and height 5.

Canvas(char x);

// Allocates a canvas containing the sequence of characters

// in the string with 2 columns of space between each pair

// of adjacent characters. For instance, Canvas("BADCAB")

// should yield:

//

// #### ### #### #### ### ####

// # # # # # # # # # # #

// #### ##### # # # ##### ####

// # # # # # # # # # # #

// #### # # #### #### # # ####

//

// Any characters in s not from {'A', 'B', 'C', 'D'} should be

// replaced with empty 5x5 space, just like previous constructor.

Canvas(string s);

// Returns the width of the canvas.

int width();

// Returns the entire canvas as a single string, consisting of each row

// of the canvas, followed by the newline character (' ').

string to_string();

// Replaces every instance in the canvas of old_char with new_char.

// For instance, if old_char is '#' and new char is '@', then:

//

// ### @@@

// # # @ @

// ##### becomes @@@@@

// # # @ @

// # # @ @

//

void replace(char old_char, char new_char);

// Adds a character to the Canvas's sequence of characters.

void add(char x);

// Destructor. Deallocates all of the memory allocated by the canvas.

~Canvas();

private:

// A canvas is represented as a 2D char array, i.e.

// an array of pointers to char (sub)arrays.

// Each subarray corresponds to a column of the image.

char** C;

int _width;

};

#endif

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

(main.cpp)

#include

#include

#include

#include "canvas.h"

using namespace std;

inline void _test(const char* expression, const char* file, int line)

{

cerr << "test(" << expression << ") failed in file " << file;

cerr << ", line " << line << "." << endl;

abort();

}

#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

void interactive_mode(char ink)

{

cout << "Enter a string and press Enter ";

cout << "to see the ASCII art version." << endl;

while (cin)

{

string line;

getline(cin, line);

string invalid_chars;

for (int i = 0; i < line.size(); ++i)

if (line[i] < 'A' || line[i] > 'D')

invalid_chars += line[i];

if (invalid_chars.size() > 0)

{

cout << " String contained invalid chars: ";

for (int i = 0; i < invalid_chars.size(); ++i)

{

cout << "'" << invalid_chars[i] << "'";

if (i != invalid_chars.size() - 1)

cout << ", ";

}

cout << "." << endl;

}

else if (line.size() > 0)

{

Canvas C(line);

C.replace('#', ink);

cout << C.to_string();

}

}

exit(0);

}

int main()

{

// Interactive mode.

// Uncomment the "interactive_mode..." line below to

// use your Canvas implementation interactively.

//

// The parameter specifies the character used to fill letters.

//

// interactive_mode('@');

// Test Canvas(int)

Canvas C1(3);

test(C1.width() == 3);

test(C1.to_string() == string(" ")

+ " "

+ " "

+ " "

+ " ");

Canvas C2(4);

test(C2.width() == 4);

test(C2.to_string() == string(" ")

+ " "

+ " "

+ " "

+ " ");

Canvas C3(7);

test(C3.width() == 7);

test(C3.to_string() == string(" ")

+ " "

+ " "

+ " "

+ " ");

// Test Canvas(char)

Canvas C4('A');

test(C4.width() == 5);

test(C4.to_string() == string(" ### ")

+ "# # "

+ "##### "

+ "# # "

+ "# # ");

Canvas C5('B');

test(C5.width() == 5);

test(C5.to_string() == string("#### ")

+ "# # "

+ "#### "

+ "# # "

+ "#### ");

Canvas C6('C');

test(C6.width() == 5);

test(C6.to_string() == string(" #### ")

+ "# "

+ "# "

+ "# "

+ " #### ");

Canvas C7('D');

test(C7.width() == 5);

test(C7.to_string() == string("#### ")

+ "# # "

+ "# # "

+ "# # "

+ "#### ");

Canvas C8('E');

test(C8.width() == 5);

test(C8.to_string() == string(" ")

+ " "

+ " "

+ " "

+ " ");

// Test replace()

C5.replace('#', '@');

test(C5.width() == 5);

test(C5.to_string() == string("@@@@ ")

+ "@ @ "

+ "@@@@ "

+ "@ @ "

+ "@@@@ ");

C5.replace(' ', '-');

test(C5.width() == 5);

test(C5.to_string() == string("@@@@- ")

+ "@---@ "

+ "@@@@- "

+ "@---@ "

+ "@@@@- ");

C5.replace('-', '@');

test(C5.width() == 5);

test(C5.to_string() == string("@@@@@ ")

+ "@@@@@ "

+ "@@@@@ "

+ "@@@@@ "

+ "@@@@@ ");

C5.replace('@', '$');

test(C5.width() == 5);

test(C5.to_string() == string("$$$$$ ")

+ "$$$$$ "

+ "$$$$$ "

+ "$$$$$ "

+ "$$$$$ ");

C6.replace(' ', '*');

test(C6.width() == 5);

test(C6.to_string() == string("*#### ")

+ "#**** "

+ "#**** "

+ "#**** "

+ "*#### ");

C7.replace('#', '*');

test(C7.width() == 5);

test(C7.to_string() == string("**** ")

+ "* * "

+ "* * "

+ "* * "

+ "**** ");

C8.replace('#', ' ');

test(C8.width() == 5);

test(C8.to_string() == string(" ")

+ " "

+ " "

+ " "

+ " ");

// Test add()

Canvas C9('A');

C9.add('C');

test(C9.width() == 12);

test(C9.to_string() == string(" ### #### ")

+ "# # # "

+ "##### # "

+ "# # # "

+ "# # #### ");

C9.add('B');

test(C9.width() == 19);

test(C9.to_string() == string(" ### #### #### ")

+ "# # # # # "

+ "##### # #### "

+ "# # # # # "

+ "# # #### #### ");

C9.add('D');

test(C9.width() == 26);

test(C9.to_string() == string(" ### #### #### #### ")

+ "# # # # # # # "

+ "##### # #### # # "

+ "# # # # # # # "

+ "# # #### #### #### ");

C9.add('!');

test(C9.width() == 33);

test(C9.to_string() == string(" ### #### #### #### ")

+ "# # # # # # # "

+ "##### # #### # # "

+ "# # # # # # # "

+ "# # #### #### #### ");

C9.add('C');

test(C9.width() == 40);

test(C9.to_string() == string(" ### #### #### #### #### ")

+ "# # # # # # # # "

+ "##### # #### # # # "

+ "# # # # # # # # "

+ "# # #### #### #### #### ");

// Test Canvas(string)

Canvas C10("ADD");

test(C10.width() == 19);

test(C10.to_string() == string(" ### #### #### ")

+ "# # # # # # "

+ "##### # # # # "

+ "# # # # # # "

+ "# # #### #### ");

Canvas C11("VBAD!");

test(C11.width() == 33);

test(C11.to_string() == string(" #### ### #### ")

+ " # # # # # # "

+ " #### ##### # # "

+ " # # # # # # "

+ " #### # # #### ");

Canvas C12("DAD CAB");

test(C12.width() == 47);

test(C12.to_string() == string("#### ### #### #### ### #### ")

+ "# # # # # # # # # # # "

+ "# # ##### # # # ##### #### "

+ "# # # # # # # # # # # "

+ "#### # # #### #### # # #### ");

cout << "Assignment complete." << endl;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

=+e. Fuel efficiency and 3-year operating cost

Answered: 1 week ago

Question

Identify the types of informal reports.

Answered: 1 week ago

Question

Write messages that are used for the various stages of collection.

Answered: 1 week ago