fork download
  1. //////////////////////////////////////////////
  2. // Resizeable ASCII Text Generator Example //
  3. // Alexander Karagulamos //
  4. // 2nd of May, 2012 //
  5. //////////////////////////////////////////////
  6. // To Peace, 300L CSC, UNIBEN, for her idea //
  7. // and contributions. //
  8. //////////////////////////////////////////////
  9.  
  10. #include <iostream>
  11. using std::cerr; using std::clog;
  12. using std::cout; using std::cin;
  13. using std::endl;
  14.  
  15. #include <stdexcept>
  16. using std::domain_error;
  17. using std::invalid_argument;
  18.  
  19. #include <vector>
  20. using std::vector;
  21.  
  22. #include <string>
  23. using std::string;
  24.  
  25. #include <cctype>
  26. using std::toupper;
  27.  
  28. class ASCII_Char{
  29. vector<string> ret;
  30.  
  31. void make_A();
  32. // void make_...
  33. void make_F();
  34. void make_Z();
  35.  
  36. protected:
  37. int font_sz; // Font size
  38.  
  39. // Create ascii character art
  40. ASCII_Char(int fnt_sz, int vec_sz)
  41. : font_sz(fnt_sz), ret(vec_sz)
  42. {}
  43.  
  44. void make_ascii_char(char c); // Character art
  45. const vector<string>& get() { return ret; }
  46. };
  47.  
  48. class ASCII_Art : ASCII_Char{
  49. string word;
  50. void make_ascii_word(const string&); // Word art
  51.  
  52. public:
  53. // Create ASCII text art from word in s
  54. // Use default size if non specified
  55. ASCII_Art(const string& s, int size = 8)
  56. : ASCII_Char(size, size >> 1)
  57. { make_ascii_word(s); }
  58.  
  59. // get word art
  60. const vector<string>& getArt() { return get(); }
  61. };
  62.  
  63. int main(){
  64. cout << " Enter word contain the ASCII_Chars[A-Z]: ";
  65. string word;
  66. cin >> word;
  67.  
  68. cout << " Enter ASCII font size[8-25]: ";
  69. int font_sz;
  70. cin >> font_sz;
  71.  
  72. try{
  73. ASCII_Art myWord(word, font_sz);
  74.  
  75. cout << endl;
  76. cout << " Result:" << endl;
  77. cout << " =======" << endl;
  78.  
  79. // Note: C++11 range-based for loop
  80. // Display ASCII word art
  81. for(const string& s : myWord.getArt()){
  82. cout << s << endl;
  83. }
  84.  
  85. }catch(std::exception& e){
  86. cerr << e.what() << endl;
  87. }
  88. return 0;
  89. }
  90.  
  91. void ASCII_Art::make_ascii_word(const string& word){
  92. if(font_sz < 8 || font_sz > 25)
  93. throw domain_error(
  94. "\n Invalid Font Size."
  95. " Please run program again!"
  96. );
  97.  
  98. // Note: C++11 range-based for loop
  99. // For each char in word & create ascii art
  100. for(const char &c : word){
  101. make_ascii_char(c);
  102. }
  103. }
  104.  
  105. void ASCII_Char::make_ascii_char(char c){
  106. switch(toupper(c)){
  107. case 'A': make_A(); break;
  108. // ....
  109. case 'F': make_F(); break;
  110. case 'Z': make_Z(); break;
  111. default:
  112. throw invalid_argument(
  113. "\n Word Contains Illegal Character(s)."
  114. " Please run the program again!"
  115. );
  116. }
  117. }
  118. void ASCII_Char::make_A(){ // make art for 'A'
  119. const int mcols = font_sz >> 1;
  120. const int mid = mcols >> 1; // draw middle line
  121.  
  122. for(int r = 0; r < mcols; ++r){
  123. int c = 1;
  124. int l_edge = mcols - r; // draw left edge
  125. int r_edge = mcols + r; // draw right edge
  126.  
  127. while(c != font_sz){
  128. if(c == l_edge || c == r_edge || r == mid
  129. && c > l_edge
  130. && c < r_edge){
  131. ret[r].push_back('*');
  132. }else{
  133. ret[r].push_back(' ');
  134. }
  135. ++c;
  136. }
  137. ret[r].push_back(' ');
  138. }
  139. }
  140.  
  141. void ASCII_Char::make_F(){ // make art for 'F'
  142. int size = font_sz >> 1;
  143. int mid = size >> 1;
  144.  
  145. for(int r = 0; r < size; ++r){
  146. int c = 0;
  147.  
  148. while(c != size){
  149. if(r == 0 || r == mid || c == 0){
  150. ret[r].push_back('*');
  151. }else{
  152. ret[r].push_back(' ');
  153. }
  154. ++c;
  155. }
  156. ret[r].push_back(' ');
  157. }
  158. }
  159.  
  160. void ASCII_Char::make_Z(){ // make art for 'Z'
  161. const int n = font_sz >> 1;
  162.  
  163. for(int r = 0; r < n; ++r){
  164. int c = 0;
  165. int diagon = n - r - 1; // draw diagonal
  166.  
  167. while(c != n){
  168. if(r == 0 || r == n-1 || c == diagon){
  169. ret[r].push_back('*');
  170. }else{
  171. ret[r].push_back(' ');
  172. }
  173. ++c;
  174. }
  175. ret[r].push_back(' ');
  176. }
  177. }
Success #stdin #stdout #stderr 0.01s 5284KB
stdin
Standard input is empty
stdout
 Enter word contain the ASCII_Chars[A-Z]:  Enter ASCII font size[8-25]: 
stderr
 Invalid Font Size. Please run program again!