Question: Can you help me pass all test cases in c++. Please do not change the provided canvas.h file and the provided main.cpp file. You can
Can you help me pass all test cases in c++. Please do not change the provided canvas.h file and the provided main.cpp file. You can find these files below. Thank you. Only canvas.pp

------canvas.cpp---- The error should be in string Canvas::to_string()
#include#include "canvas.h" Canvas::Canvas(int width) { this->_width = width; if (width == 0) { return; } this->C = new char*[width]; for (int i = 0; i C[i] = new char[5]; for (int j = 0; j C[i][j] = ' '; } } } Canvas::Canvas(char x) { this->_width = 5; this->C = new char*[5]; if (x == 'A' || x == 'B' || x == 'C' || x == 'D') { for (int i = 0; i C[i] = new char[5]; } // first row for (int i = 0; i C[i][0] = '#'; } else if (i == 0 || i == 4) { this->C[i][0] = ' '; } } else if (x == 'B') { if (i != 4) { this->C[i][0] = '#'; } else { this->C[i][0] = ' '; } } else if (x == 'C') { if (i != 0) { this->C[i][0] = '#'; } else { this->C[i][0] = ' '; } } else if (x == 'D') { if (i != 4) { this->C[i][0] = '#'; } else { this->C[i][0] = ' '; } } } // second row, only even indexed positions for (int i = 0; i C[i][1] = '#'; } else { this->C[i][1] = ' '; } } else if (x == 'B') { if (i == 0 || i == 4) { this->C[i][1] = '#'; } else { this->C[i][1] = ' '; } } else if (x == 'C') { if (i == 0) { this->C[i][1] = '#'; } else { this->C[i][1] = ' '; } } else if (x == 'D') { if (i == 0 || i == 4) { this->C[i][1] = '#'; } else { this->C[i][1] = ' '; } } } // third row, according to char for (int i = 0; i C[i][2] = '#'; } else if (x == 'B') { if (i != 4) { this->C[i][2] = '#'; } else { this->C[i][2] = ' '; } } else if (x == 'C') { if (i == 0) { this->C[i][2] = '#'; } else { this->C[i][2] = ' '; } } else if (x == 'D') { if (i == 0 || i == 4) { this->C[i][2] = '#'; } else { this->C[i][2] = ' '; } } } // fourth row, only even indexed positions for (int i = 0; i C[i][3] = '#'; } else { this->C[i][3] = ' '; } } else if (x == 'B') { if (i == 0 || i == 4) { this->C[i][3] = '#'; } else { this->C[i][3] = ' '; } } else if (x == 'C') { if (i == 0) { this->C[i][3] = '#'; } else { this->C[i][3] = ' '; } } else if (x == 'D') { if (i == 0 || i == 4) { this->C[i][3] = '#'; } else { this->C[i][3] = ' '; } } } // fifth row, only even indexed positions for (int i = 0; i C[i][4] = '#'; } else { this->C[i][4] = ' '; } } else if (x == 'B') { if (i != 4) { this->C[i][4] = '#'; } else { this->C[i][4] = ' '; } } else if (x == 'C') { if (i != 0) { this->C[i][4] = '#'; } else { this->C[i][4] = ' '; } } else if (x == 'D') { if (i != 4) { this->C[i][4] = '#'; } else { this->C[i][4] = ' '; } } } return; } for (int i = 0; i C[i] = new char[5]; memset(this->C[i], ' ', 5); } } int Canvas::width() { return this->_width; } string Canvas::to_string() { if (this->_width == 0) { return ""; } string str = ""; for (int j = 0; j _width; i++) { if (str[str.length()-1] == ' ' && this->C[i][j] == ' ') { continue; } str += this->C[i][j]; } str += " "; } return str; } void Canvas::replace(char old_char, char new_char) { if (this->_width == 0) { return; } for (int i = 0; i _width; i++) { for (int j = 0; j C[i][j] == old_char) { this->C[i][j] = new_char; } } } } Canvas::~Canvas() { for (int i = 0; i _width; i++) { delete this->C[i]; } delete this->C; }
----------main.cpp-----------
#include
using namespace std;
inline void _test(const char* expression, const char* file, int line) { cerr
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
int main() {
// Test Canvas(int)
Canvas C0(0); test(C0.width() == 0); test(C0.to_string() == string(""));
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(" ") + " " + " " + " " + " ");
cout
---------------canvas.h----------------------------------
ifndef CANVAS_H #define CANVAS_H
#include
using namespace std;
// In this homework, you'll 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. If the width is 0, // the canvas is empty (pointer is NULL and _width is 0) 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);
// 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 (' '). // If the canvas is empty, returns an empty string. 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);
// 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
In this homework, you'll implement a class that represents "ASCII art" images and operations on them, represented as two-dimensional char arrays allocated on the heap (using the new operator). The following files are given to you: 1. A C++ header file (canvas.h) declaring the Canvas class. 2. A C++ source file (main.cpp) containing a main() function with tests. Create a new C++ source file named canvas.cpp that implements the class declared in canvas.h so that canvas.cpp and the provided files compile into a program that runs with no failed tests
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
