본문 바로가기
C C++ - STL

STL - 5 Text Picture

by violetoz 2014. 2. 13.
[STL-5]예제하나 보고 다음으로 가죠. - text picture|STL을 배우자
2004.05.19 20:14

출처C언어를 배우자 | 라온

원문http://cafe.naver.com/cafec/1042

알고리즘과 container 들어가기 전에 샘플 프로그램 하나 보고 넘어가죠.

아래 프로그램은 vector<string> p 를 가지고 frame, vertical , horizontal로

텍스트 그림을 그리는 것입니다.

 

처음 vector<string> p 에 push_back()으로 요소를 넣어 줍니다.

frame은 Text의 최대 길이를 구하여 외곽으로 Frame을 '*' 로 해줍니다.

vcat은 두개의 vector<string>을 받아 종으로 붙은 vector<string>을 만듭니다.

hcat은 두개의 vector<string>을 받아 횡으로 붙은 vector<string>을 만듭니다.

vcat은 간다하게 붙이면 되지만 hcat의 경우 한 쪽의 요소가 적을 경우 빈곳을

whitespace를 넣어 채워 줍니다.

 

gcc 3.2 , ms visual c++ 6.0 sp5 이상에서 compile 하시는 것이 좋습니다.

 

첨부한 프로그램을 실행해 보시고 source를 보세요.

C로 구현해 봐도 재미있을 것 같네요..

그럼 다음 연재에서 generic algorithm 보도록 하죠  ...  ^^/

 

ps)gcc에는 min/max를 지원하지만 vc sp5에서는 지원이 안되서 template으로 따로 만들었습니다.

 

  1  #include <iostream>
  2  #include <iterator>
  3  #include <algorithm>
  4  #include <string>
  5  #include <vector>
  6
  7  using namespace std;
  8
  9  void title(string);                             //title printer
 10  void PrnCnt(string , const vector<string> );    //vector picture print
 11  void nextfn();                                  //to continue ...
 12
 13  //max length return in vector<string>
 14  string::size_type width(const vector<string>& v);
 15  //frame text picture inserts vector
 16  vector<string>    frame(const vector<string>& v);
 17  //vertical text picture inserts vector<string>
 18  vector<string>    vcat(const vector<string>& top,
 19                         const vector<string>& bottom);
 20  //horizontal text picture inserts vector<string>
 21  vector<string>    hcat(const vector<string>& left,
 22                         const vector<string>& right);
 23
 24  #ifdef _MSC_VER
 25  template <class T> T max(const T& a, const T& b) {return (a > b)?a:b;}
 26  template <class T> T min(const T& a, const T& b) {return (a < b)?a:b;}
 27  #endif
 28
 29  int main()
 30  {
 31      vector<string> p;
 32      p.push_back("this is an");
 33      p.push_back("example");
 34      p.push_back("to");
 35      p.push_back("illustrate");
 36      p.push_back("framing");
 37
 38  #ifdef _MSC_VER
 39      system("cls");
 40  #else
 41      system("clear");
 42  #endif
 43
 44      //Original vector<string> p prints to standard out
 45      PrnCnt("Original vector<string> p",p);
 46      //Change From Original string to Frame
 47      PrnCnt("Frame text picture",frame(p));
 48      //Vertical text picture vector<string> v prints to standard out
 49      PrnCnt("Vertical text picture(p, frame(p))",vcat(p, frame(p)));
 50      //Vertical text picture vector<string> v1 prints to standard out
 51      PrnCnt("Vertical text picture(frame(p), p)",vcat(frame(p),p));
 52      //Horizontal text picture vector<string> h prints to standard out
 53      PrnCnt("Horizontal text picture(p, frame(p))",hcat(p,frame(p)));
 54      //Horizontal text picture vector<string> h1 prints to standard out
 55      PrnCnt("Horizontal text picture(frame(p), p)",hcat(frame(p),p));
 56
 57      return 0;
 58  }
 59
 60  void title(string t_name)
 61  {
 62      string line(t_name.size()+2,'-');
 63      cout << line << endl;
 64      cout << ' ' << t_name << endl;
 65      cout << line << endl;
 66  }
 67
 68  void PrnCnt(string s, const vector<string> d)
 69  {
 70      title(s);
 71      ostream_iterator<string> ofile(cout, "\n");
 72      copy(d.begin(), d.end(), ofile);
 73      cout << endl;
 74      nextfn();
 75  }
 76
 77  void nextfn()
 78  {
 79      cin.clear();
 80      cout << "Press any key to continue...";
 81      cin.get();
 82  #ifdef _MSC_VER
 83      system("cls");
 84  #else
 85      system("clear");
 86  #endif
 87  }
 88
 89  string::size_type width(const vector<string>& v)
 90  {
 91      string::size_type maxlen = 0;
 92      for(vector<string>::size_type i = 0; i != v.size(); ++i)
 93          maxlen = max(maxlen, v[i].size());
 94      return maxlen;
 95  }
 96
 97  vector<string> frame(const vector<string>& v)
 98  {
 99      vector<string> ret;
100      string::size_type maxlen = width(v);
101      string border(maxlen + 4, '*');
102      // write the top border
103      ret.push_back(border);
104      // write each interior row, bordered by an asterisk and a space
105      for (vector<string>::size_type i = 0; i != v.size(); ++i) {
106          ret.push_back("* "+v[i]+string(maxlen-v[i].size(),' ')+" *");
107      }
108      // write the bottom border
109      ret.push_back(border);
110      return ret;
111  }
112
113  vector<string> vcat(const vector<string>& top, const vector<string>& bottom)
114  {
115      // copy the `top' picture
116      vector<string> ret = top;
117      // copy entire `bottom' picture
118      for (vector<string>::const_iterator it = bottom.begin();it != bottom.end(); ++it)
119          ret.push_back(*it);
120
121      return ret;
122  }
123
124  vector<string> hcat(const vector<string>& left, const vector<string>& right)
125  {
126      vector<string> ret;
127
128      // add 1 to leave a space between pictures
129      string::size_type width1 = width(left) + 1;
130      // indices to look at elements from `left' and `right' respectively
131      vector<string>::size_type i = 0, j = 0;
132      // continue until we've seen all rows from both pictures
133      while (i != left.size() || j != right.size()) {
134          // construct new `string' to hold characters from both pictures
135          string s;
136          // copy a row from the left-hand side, if there is one
137          if (i != left.size())
138              s = left[i++];
139          // pad to full width
140          s += string(width1 - s.size(), ' ');
141          // copy a row from the right-hand side, if there is one
142          if (j != right.size())
143              s += right[j++];
144          // add `s' to the picture we're creating
145          ret.push_back(s);
146      }
147      return ret;
148  }
149


'C C++ - STL' 카테고리의 다른 글

STL - 8 Vector 라이브러리  (0) 2014.02.13
STL - 7 Sequence Container  (0) 2014.02.13
STL - 6 알고리즘  (0) 2014.02.13
STL - 4 Iterator 반복자  (0) 2014.02.13
STL - 3 템플릿에 대하여  (0) 2014.02.13