Question

help implementing canvas.cpp but with only #include "canvas.h", class header file doesnt have height ----------------------------------------------------------------------------------------------------------------------- canvas.h...

help implementing canvas.cpp but with only #include "canvas.h",

class header file doesnt have height

-----------------------------------------------------------------------------------------------------------------------

canvas.h

#ifndef CANVAS_H
#define CANVAS_H

#include <string>

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.
                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 ('\n').
                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 <string>
#include <cstdlib>
#include <iostream>
#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("   \n") 
                                    + "   \n"
                                    + "   \n"
                                    + "   \n"
                                    + "   \n");
        Canvas C2(4);
        test(C2.width() == 4);
        test(C2.to_string() == string("    \n")
                                    + "    \n"
                                    + "    \n"
                                    + "    \n"
                                    + "    \n");
        Canvas C3(7);
        test(C3.width() == 7);
        test(C3.to_string() == string("       \n")
                                    + "       \n"
                                    + "       \n"
                                    + "       \n"
                                    + "       \n");


        // Test Canvas(char)
        Canvas C4('A');
        test(C4.width() == 5);
        test(C4.to_string() == string(" ### \n")
                                    + "#   #\n"
                                    + "#####\n"
                                    + "#   #\n"
                                    + "#   #\n");
        Canvas C5('B');
        test(C5.width() == 5);
        test(C5.to_string() == string("#### \n")
                                    + "#   #\n"
                                    + "#### \n"
                                    + "#   #\n"
                                    + "#### \n");
        Canvas C6('C');
        test(C6.width() == 5);
        test(C6.to_string() == string(" ####\n")
                                    + "#    \n"
                                    + "#    \n"
                                    + "#    \n"
                                    + " ####\n");
        Canvas C7('D');
        test(C7.width() == 5);
        test(C7.to_string() == string("#### \n")
                                    + "#   #\n"
                                    + "#   #\n"
                                    + "#   #\n"
                                    + "#### \n");
        Canvas C8('E');
        test(C8.width() == 5);
        test(C8.to_string() == string("     \n")
                                    + "     \n"
                                    + "     \n"
                                    + "     \n"
                                    + "     \n");


        // Test replace()
        C5.replace('#', '@');
        test(C5.width() == 5);
        test(C5.to_string() == string("@@@@ \n")
                                    + "@   @\n"
                                    + "@@@@ \n"
                                    + "@   @\n"
                                    + "@@@@ \n");
        C5.replace(' ', '-');
        test(C5.width() == 5);
        test(C5.to_string() == string("@@@@-\n")
                                    + "@---@\n"
                                    + "@@@@-\n"
                                    + "@---@\n"
                                    + "@@@@-\n");
        C5.replace('-', '@');
        test(C5.width() == 5);
        test(C5.to_string() == string("@@@@@\n")
                                    + "@@@@@\n"
                                    + "@@@@@\n"
                                    + "@@@@@\n"
                                    + "@@@@@\n");
        C5.replace('@', '$');
        test(C5.width() == 5);
        test(C5.to_string() == string("$$$$$\n")
                                    + "$$$$$\n"
                                    + "$$$$$\n"
                                    + "$$$$$\n"
                                    + "$$$$$\n");
        C6.replace(' ', '*');
        test(C6.width() == 5);
        test(C6.to_string() == string("*####\n")
                                    + "#****\n"
                                    + "#****\n"
                                    + "#****\n"
                                    + "*####\n");
        C7.replace('#', '*');
        test(C7.width() == 5);
        test(C7.to_string() == string("**** \n")
                                    + "*   *\n"
                                    + "*   *\n"
                                    + "*   *\n"
                                    + "**** \n");
        C8.replace('#', ' ');
        test(C8.width() == 5);
        test(C8.to_string() == string("     \n")
                                    + "     \n"
                                    + "     \n"
                                    + "     \n"
                                    + "     \n");


        // Test add()   
        Canvas C9('A');
        C9.add('C');
        test(C9.width() == 12); 
        test(C9.to_string() == string(" ###    ####\n")
                                    + "#   #  #    \n"
                                    + "#####  #    \n"
                                    + "#   #  #    \n"
                                    + "#   #   ####\n");
        C9.add('B');
        test(C9.width() == 19); 
        test(C9.to_string() == string(" ###    ####  #### \n")
                                    + "#   #  #      #   #\n"
                                    + "#####  #      #### \n"
                                    + "#   #  #      #   #\n"
                                    + "#   #   ####  #### \n");
        C9.add('D');
        test(C9.width() == 26); 
        test(C9.to_string() == string(" ###    ####  ####   #### \n")
                                    + "#   #  #      #   #  #   #\n"
                                    + "#####  #      ####   #   #\n"
                                    + "#   #  #      #   #  #   #\n"
                                    + "#   #   ####  ####   #### \n");
        C9.add('!');
        test(C9.width() == 33); 
        test(C9.to_string() == string(" ###    ####  ####   ####        \n")
                                    + "#   #  #      #   #  #   #       \n"
                                    + "#####  #      ####   #   #       \n"
                                    + "#   #  #      #   #  #   #       \n"
                                    + "#   #   ####  ####   ####        \n");
        C9.add('C');
        test(C9.width() == 40); 
        test(C9.to_string() == string(" ###    ####  ####   ####           ####\n")
                                    + "#   #  #      #   #  #   #         #    \n"
                                    + "#####  #      ####   #   #         #    \n"
                                    + "#   #  #      #   #  #   #         #    \n"
                                    + "#   #   ####  ####   ####           ####\n");

        
        // Test Canvas(string)
        Canvas C10("ADD");
        test(C10.width() == 19);        
        test(C10.to_string() == string(" ###   ####   #### \n")
                                     + "#   #  #   #  #   #\n"
                                     + "#####  #   #  #   #\n"
                                     + "#   #  #   #  #   #\n"
                                     + "#   #  ####   #### \n");
        Canvas C11("VBAD!");
        test(C11.width() == 33);        
        test(C11.to_string() == string("       ####    ###   ####        \n")
                                     + "       #   #  #   #  #   #       \n"
                                     + "       ####   #####  #   #       \n"
                                     + "       #   #  #   #  #   #       \n"
                                     + "       ####   #   #  ####        \n");
        Canvas C12("DAD CAB");
        test(C12.width() == 47);        
        test(C12.to_string() == string("####    ###   ####           ####   ###   #### \n")
                                     + "#   #  #   #  #   #         #      #   #  #   #\n"
                                     + "#   #  #####  #   #         #      #####  #### \n"
                                     + "#   #  #   #  #   #         #      #   #  #   #\n"
                                     + "####   #   #  ####           ####  #   #  #### \n");


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



-----------------------------------------------------------------------
0 0
Add a comment Improve this question Transcribed image text
Answer #1

As code is pretty standared I haven't commented. If you find any problem I am avaible in comments.

#include <string>

#include <cstdlib>

#include <iostream>

#include "canvas.h"

void Canvas::Canvas(int width, int height)

{
char *temp;

temp = (char *)malloc(sizeof(char) * width);

C = (char **)malloc(sizeof(temp) * height);
}

void Canvas::Canvas(char x)

{
char *temp;

int i;

temp = (char *)malloc(sizeof(char) * 5);

C = (char **)malloc(sizeof(temp) * 5);

width = 5;

height = 5;

string s1, s2, s3, s4, s5;

if (x == 'A')

{
s1 + = " ### ";

s2 + = " # # ";

s3 + = "#####";

s4 + = " # # ";

s5 + = " # # ";
}

else if (x == 'B')

{
s1 + = "#### ";

s2 + = "# #";

s3 + = "#####";

s4 + = "# #";

s5 + = "#### ";
}

else if (x == 'C')

{
s1 + = " ####";

s2 + = "# ";

s3 + = " ";

s4 + = "# ";

s5 + = " ####";
}

else if (x == 'D')

{
s1 + = "#### ";

s2 + = "# #";

s3 + = "# #";

s4 + = "# #";

s5 + = "#### ";
}

for (i = 0; i < 5; i++)

C[0][i] = s1[i];

for (i = 0; i < 5; i++)

C[1][i] = s2[i];

for (i = 0; i < 5; i++)

C[2][i] = s3[i];

for (i = 0; i < 5; i++)

C[3][i] = s4[i];

for (i = 0; i < 5; i++)

C[4][i] = s5[i];
}

void Canvas::Canvas(string S)

{
int len = S.length();

char *temp;

int j = 0;

temp = (char *)malloc(sizeof(char) * 5 * len);

C = (char **)malloc(sizeof(temp) * 5);

string s1, s2, s3, s4, s5;

width = 5 * len;

height = 5;

for (int i = 0; i < len; i++, j = j + 5)

{
char x = S[i];

if (x == 'A')

{
s1 + = " ### ";

s2 + = " # # ";

s3 + = "#####";

s4 + = " # # ";

s5 + = " # # ";
}

else if (x == 'B')

{
s1 + = "#### ";

s2 + = "# #";

s3 + = "#####";

s4 + = "# #";

s5 + = "#### ";
}

else if (x == 'C')

{
s1 + = " ####";

s2 + = "# ";

s3 + = " ";

s4 + = "# ";

s5 + = " ####";
}

else if (x == 'D')

{
s1 + = "#### ";

s2 + = "# #";

s3 + = "# #";

s4 + = "# #";

s5 + = "#### ";
}

else

{
s1 + = " ";

s2 + = " ";

s3 + = " ";

s4 + = " ";

s5 + = " ";
}
}

for (j = 0; j < len * 5; j++)

C[0][j] = s1[j];

for (j = 0; j < len * 5; j++)

C[1][j] = s2[j];

for (j = 0; j < len * 5; j++)

C[2][j] = s3[j];

for (j = 0; j < len * 5; j++)

C[3][j] = s4[j];

for (j = 0; j < len * 5; j++)

C[4][j] = s5[j];
}

int Canvas::width()

{
return width;
}

int Canvas::height()

{
return height;
}

string Canvas::to_string()

{
int row = 0, col;

string ans;

while (row < height)

{
col = 0;

while (col < width)

{
if (C[row + 3][col + 1] == '#')

ans += 'A';

else if (C[row + 2][col + 2] == '#')

ans += 'B';

else if (C[row + 2][col] == '#')

ans += 'D';

else

ans += 'C';

col = col + 5;
}

ans += '\n';

row = row + 5;
}

return ans;
}

void Canvas::replace(char old_char, char new_char)

{
int i, j;

for (i = 0; i < width; i++)

{
for (j = 0; j < height; j++)

{
if (C[i][j] == old_char)

C[i][j] = new_char;
}
}
}

Canvas::~Canvas()

{
int i;

for (i = height - 1; i >= 0; i--)

delete (C[i]);

width = 0;

height = 0;
}

Thank you.

Add a comment
Know the answer?
Add Answer to:
help implementing canvas.cpp but with only #include "canvas.h", class header file doesnt have height ----------------------------------------------------------------------------------------------------------------------- canvas.h...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std;...

    Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public:    Server();    Server(string, int);    ~Server();    string getPiece(int); private:    string *ascii;    mutex access; }; #endif -------------------------------------------------------------------------------------------------------------------------- Server.cpp #include "Server.h" #include #include Server::Server(){} Server::Server(string filename, int threads) {    vector loaded;    ascii = new string[threads];    ifstream in;    string line;    in.open(filename);    if (!in.is_open())    {        cout << "Could not open file...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...

    In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete the requested function. You may not use ANY library functions or include any headers, except for <cstddef> for size_t. */ #include <cstddef> // size_t for sizes and indexes ///////////////// WRITE YOUR FUNCTION BELOW THIS LINE /////////////////////// ///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE /////////////////////// // These are OK after...

  • Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n...

    Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • *****Complete void example 4, 7, 8, 9, 10 #include <iostream> #include <fstream> #include <vector> #include <string>...

    *****Complete void example 4, 7, 8, 9, 10 #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; void rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") { str.erase(str.find_last_not_of(chars) + 1); } vector<string> *get_words() { vector<string> *words = new vector<string>(); fstream infile; string word; infile.open("words.txt"); getline(infile, word); while(infile) { rtrim(word);     words->push_back(word);     getline(infile, word); } return words; } void example_1(vector<string> *words) { // find words that contain the substring 'pet' and 'cat' // HINT: use find(str, p) method....

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT