CODE#include
#include
#include
#include
#include
using namespace std;
class CharacterPicture
{
ÂÂÂÂprivate:
ÂÂÂÂÂÂÂÂ// Character used when printing the frame
ÂÂÂÂÂÂÂÂstring frameCharacter;
ÂÂÂÂÂÂÂÂ// Vector used to store all strings in the object
ÂÂÂÂÂÂÂÂvector pictureStrings;
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ// Returns the longest string contained in the object
ÂÂÂÂÂÂÂÂ// *** Not required by spec, but makes life a lot easier during
ÂÂÂÂÂÂÂÂ//ÂÂÂÂ framing and concatenation by determining how far to frame or pad
ÂÂÂÂÂÂÂÂint longestString();
ÂÂÂÂpublic:
ÂÂÂÂÂÂÂÂ// Create a new CharacterPicture object
ÂÂÂÂÂÂÂÂCharacterPicture();
ÂÂÂÂÂÂÂÂ// Create a new CharacterPicture object as a copy of another
ÂÂÂÂÂÂÂÂCharacterPicture(CharacterPicture& pic);
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ// Returns the number of discrete strings in this object
ÂÂÂÂÂÂÂÂint getSize();
ÂÂÂÂÂÂÂÂ// Returns a string from this object by index
ÂÂÂÂÂÂÂÂstring getString(int index);
ÂÂÂÂÂÂÂÂ// Returns the character used for the frame
ÂÂÂÂÂÂÂÂstring getFrame();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ// Change the frame character used in output, the default is '*'
ÂÂÂÂÂÂÂÂvoid setFrame(string frame);
ÂÂÂÂÂÂÂÂ// Add a new string to the object
ÂÂÂÂÂÂÂÂ// *** Not required by spec, but simplifies adding with the other two
ÂÂÂÂÂÂÂÂ//ÂÂÂÂ methods. Also has the side effect of making programmatic
ÂÂÂÂÂÂÂÂ//ÂÂÂÂ additions a bit more elegant.
ÂÂÂÂÂÂÂÂvoid addString(string str);
ÂÂÂÂÂÂÂÂ// Add a new string to the object by console input
ÂÂÂÂÂÂÂÂvoid inputString();
ÂÂÂÂÂÂÂÂ// Load strings from a file
ÂÂÂÂÂÂÂÂvoid loadFile(string filename);
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ// Concatenate another CharacterPicture object horizontally
ÂÂÂÂÂÂÂÂvoid concatHorizontal(CharacterPicture pic);
ÂÂÂÂÂÂÂÂ// Concatenate another CharacterPicture object vertically
ÂÂÂÂÂÂÂÂvoid concatVertical(CharacterPicture pic);
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ// Print the character picture to stdout, takes an optional boolean to
ÂÂÂÂÂÂÂÂ// determine if the frame should be used
ÂÂÂÂÂÂÂÂvoid output(bool frame = false);
ÂÂÂÂÂÂÂÂ// Print the character picture to the filename specified, takes an
ÂÂÂÂÂÂÂÂ// optional boolean to determine if the frame should be used
ÂÂÂÂÂÂÂÂvoid output(string filename, bool frame = false);
};
int CharacterPicture::longestString()
{
ÂÂÂÂint maxlen = 0;
ÂÂÂÂfor(int i = 0; i < getSize(); i++)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂif(pictureStrings.length() > maxlen)
ÂÂÂÂÂÂÂÂ{
ÂÂÂÂÂÂÂÂÂÂÂÂmaxlen = pictureStrings.length();
ÂÂÂÂÂÂÂÂ}
ÂÂÂÂ}
ÂÂÂÂreturn maxlen;
}
CharacterPicture::CharacterPicture()
{
ÂÂÂÂsetFrame("*");
}
CharacterPicture::CharacterPicture(CharacterPicture& pic)
{
ÂÂÂÂfor(int i = 0; i < pic.getSize(); i++)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂaddString(pic.getString(i));
ÂÂÂÂ}
ÂÂÂÂsetFrame(pic.getFrame());
}
int CharacterPicture::getSize()
{
ÂÂÂÂreturn pictureStrings.size();
}
string CharacterPicture::getString(int index)
{
ÂÂÂÂif(index >= 0 && index < getSize())
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂreturn pictureStrings[index];
ÂÂÂÂ}
ÂÂÂÂelse
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂreturn string("");
ÂÂÂÂ}
}
string CharacterPicture::getFrame()
{
ÂÂÂÂreturn frameCharacter;
}
void CharacterPicture::setFrame(string frame)
{
ÂÂÂÂframeCharacter = frame;
}
void CharacterPicture::addString(string str)
{
ÂÂÂÂpictureStrings.push_back(str);
}
void CharacterPicture::inputString()
{
ÂÂÂÂchar str[256];
ÂÂÂÂcin.getline(str, 256);
ÂÂÂÂaddString(string(str));
}
void CharacterPicture::loadFile(string filename)
{
ÂÂÂÂstring line;
ÂÂÂÂifstream stringfile(filename.c_str());
ÂÂÂÂif(stringfile.is_open())
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ while(!stringfile.eof())
ÂÂÂÂÂÂÂÂ {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ getline(stringfile, line);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ addString(line);
ÂÂÂÂÂÂÂÂ }
ÂÂÂÂÂÂÂÂ stringfile.close();
ÂÂÂÂ}
}
void CharacterPicture::concatHorizontal(CharacterPicture pic)
{
ÂÂÂÂint length = longestString() + 1;
ÂÂÂÂfor(int i = 0; i < pic.getSize(); i++)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂif(i < getSize())
ÂÂÂÂÂÂÂÂ{
ÂÂÂÂÂÂÂÂÂÂÂÂstring str;
ÂÂÂÂÂÂÂÂÂÂÂÂstr = pictureStrings;
ÂÂÂÂÂÂÂÂÂÂÂÂstr.resize(length, ' ');
ÂÂÂÂÂÂÂÂÂÂÂÂstr += pic.getString(i);
ÂÂÂÂÂÂÂÂÂÂÂÂpictureStrings = str;
ÂÂÂÂÂÂÂÂ}
ÂÂÂÂÂÂÂÂelse
ÂÂÂÂÂÂÂÂ{
ÂÂÂÂÂÂÂÂÂÂÂÂstring str;
ÂÂÂÂÂÂÂÂÂÂÂÂstr.resize(length, ' ');
ÂÂÂÂÂÂÂÂÂÂÂÂpictureStrings.push_back(str + pic.getString(i));
ÂÂÂÂÂÂÂÂ}
ÂÂÂÂ}
}
void CharacterPicture::concatVertical(CharacterPicture pic)
{
ÂÂÂÂfor(int i = 0; i < pic.getSize(); i++)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂpictureStrings.push_back(pic.getString(i));
ÂÂÂÂ}
}
void CharacterPicture::output(bool frame)
{
ÂÂÂÂif(frame)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂfor(int i = 0; i < (longestString() + 4); i++)
ÂÂÂÂÂÂÂÂ{
ÂÂÂÂÂÂÂÂÂÂÂÂcout