//////////////////////////////////////////////
// Resizeable ASCII Text Generator Example //
// Alexander Karagulamos //
// 2nd of May, 2012 //
//////////////////////////////////////////////
// To Peace, 300L CSC, UNIBEN, for her idea //
// and contributions. //
//////////////////////////////////////////////
#include <iostream>
using std::cerr; using std::clog;
using std::cout; using std::cin;
using std::endl;
#include <stdexcept>
using std::domain_error;
using std::invalid_argument;
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <cctype>
using std::toupper;
class ASCII_Char{
vector<string> ret;
void make_A();
// void make_...
void make_F();
void make_Z();
protected:
int font_sz; // Font size
// Create ascii character art
ASCII_Char(int fnt_sz, int vec_sz)
: font_sz(fnt_sz), ret(vec_sz)
{}
void make_ascii_char(char c); // Character art
const vector<string>& get() { return ret; }
};
class ASCII_Art : ASCII_Char{
string word;
void make_ascii_word(const string&); // Word art
public:
// Create ASCII text art from word in s
// Use default size if non specified
ASCII_Art(const string& s, int size = 8)
: ASCII_Char(size, size >> 1)
{ make_ascii_word(s); }
// get word art
const vector<string>& getArt() { return get(); }
};
int main(){
cout << " Enter word contain the ASCII_Chars[A-Z]: ";
string word;
cin >> word;
cout << " Enter ASCII font size[8-25]: ";
int font_sz;
cin >> font_sz;
try{
ASCII_Art myWord(word, font_sz);
cout << endl;
cout << " Result:" << endl;
cout << " =======" << endl;
// Note: C++11 range-based for loop
// Display ASCII word art
for(const string& s : myWord.getArt()){
cout << s << endl;
}
}catch(std::exception& e){
cerr << e.what() << endl;
}
return 0;
}
void ASCII_Art::make_ascii_word(const string& word){
if(font_sz < 8 || font_sz > 25)
throw domain_error(
"\n Invalid Font Size."
" Please run program again!"
);
// Note: C++11 range-based for loop
// For each char in word & create ascii art
for(const char &c : word){
make_ascii_char(c);
}
}
void ASCII_Char::make_ascii_char(char c){
switch(toupper(c)){
case 'A': make_A(); break;
// ....
case 'F': make_F(); break;
case 'Z': make_Z(); break;
default:
throw invalid_argument(
"\n Word Contains Illegal Character(s)."
" Please run the program again!"
);
}
}
void ASCII_Char::make_A(){ // make art for 'A'
const int mcols = font_sz >> 1;
const int mid = mcols >> 1; // draw middle line
for(int r = 0; r < mcols; ++r){
int c = 1;
int l_edge = mcols - r; // draw left edge
int r_edge = mcols + r; // draw right edge
while(c != font_sz){
if(c == l_edge || c == r_edge || r == mid
&& c > l_edge
&& c < r_edge){
ret[r].push_back('*');
}else{
ret[r].push_back(' ');
}
++c;
}
ret[r].push_back(' ');
}
}
void ASCII_Char::make_F(){ // make art for 'F'
int size = font_sz >> 1;
int mid = size >> 1;
for(int r = 0; r < size; ++r){
int c = 0;
while(c != size){
if(r == 0 || r == mid || c == 0){
ret[r].push_back('*');
}else{
ret[r].push_back(' ');
}
++c;
}
ret[r].push_back(' ');
}
}
void ASCII_Char::make_Z(){ // make art for 'Z'
const int n = font_sz >> 1;
for(int r = 0; r < n; ++r){
int c = 0;
int diagon = n - r - 1; // draw diagonal
while(c != n){
if(r == 0 || r == n-1 || c == diagon){
ret[r].push_back('*');
}else{
ret[r].push_back(' ');
}
++c;
}
ret[r].push_back(' ');
}
}