• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 41, 文章 - 8, 評論 - 8, 引用 - 0
            數據加載中……

            [導入][C++]c++沉思錄第10章例子“字符圖像”

            雖然還不是很懂啊,但卻很有意思。

            下面把辛辛苦苦打的代碼貼出來,大家一起學習。

            下面是 Picture.h

            #include<iostream>
            using namespace std;
            class P_Node{
             friend class Picture;

            protected:
             P_Node();
             virtual ~P_Node();
             virtual int height() const=0;
             virtual int width() const=0;
             virtual void display
              (ostream&,int,int)const=0;
             //int max(int,int);
            private:
             int use;
            };

            class Picture{
             friend ostream& operator<<(ostream&,const Picture&);
             friend Picture frame(const Picture&);
             friend Picture operator&(const Picture&,const Picture&);
             friend Picture operator|(const Picture&,const Picture&);
             friend class String_Pic;
             friend class Frame_Pic;
             friend class Hcat_Pic;
             friend class VCat_Pic;

            public:
             Picture();
             Picture(const char* const*,int);
             Picture(const Picture&);
             ~Picture();
             
             Picture& operator=(const Picture&);
            private:
             Picture(P_Node*);
             int height() const;
             int width() const;
             void display(ostream&,int,int) const;
             P_Node* p;
            };


            class String_Pic:public P_Node{
             friend class Picture;
             String_Pic(const char* const*,int);
             ~String_Pic();
             int height() const;
             int width() const;
             void display(ostream&,int,int) const;
             char** data;
             int size;
            };

            class Frame_Pic:public P_Node{
             friend Picture frame(const Picture&);
             Frame_Pic(const Picture&);
             int height() const;
             int width() const;
             void display(ostream&,int,int) const;
             Picture p;
            };

            class VCat_Pic:public P_Node{
             friend Picture operator&
              (const Picture&,const Picture&);
             VCat_Pic(const Picture&,const Picture&);
             int height() const;
             int width() const;
             void display(ostream&,int,int) const;
             Picture top,bottom;
            };

            class Hcat_Pic:public P_Node{
             friend Picture operator|
              (const Picture&,const Picture&);
             Hcat_Pic(const Picture&,const Picture&);
             int height() const;
             int width() const;
             void display(ostream&,int,int) const;
             Picture left,right;
            };

            下面是 Picture.cpp

            #include"Picture.h"
            #include<iostream>
            #include<cstring>
            using namespace std;

            P_Node::~P_Node(){}
            P_Node::P_Node():use(1){}

            static void pad(ostream& os,int x,int y)
            {
             for(int i=x;i<y;i++)
              os<<" ";
            }
            int max(int x,int y)
            {
             return x>y?x:y;
            }

            Picture::Picture(const char* const* str,int n):
             p(new String_Pic(str,n)){}

            Picture::Picture(const Picture& orig):p(orig.p)
            {
             orig.p->use++;
            };

            Picture::Picture(P_Node* p_node):p(p_node){}

            Picture::~Picture()
            {
             if(--p->use==0)
              delete p;
            }

            Picture& Picture::operator=(const Picture& orig)
            {
             orig.p->use++;
             if(--p->use==0)
              delete p;
             p=orig.p;
             return *this;
            }

            int Picture::height() const
            {
             return p->height();
            }

            int Picture::width() const
            {
             return p->width();
            }

            void Picture::display(ostream& o,int x,int y)const
            {
             p->display(o,x,y);
            }

            ostream& operator<<(ostream& os,const Picture& picture)
            {
             int ht=picture.height();
             for(int i=0;i<ht;i++){
              picture.display(os,i,0);
              os<<endl;
             }
             return os;
            }

            String_Pic::String_Pic(const char* const* p,int n):
             data(new char* [n]),size(n)
            {
             for(int i=0;i<n;i++){
              data[i]=new char[strlen(p[i])+1];
              strcpy(data[i],p[i]);
             }
            }

            int String_Pic::height() const
            {
             return size;
            }

            int String_Pic::width() const
            {
             int n=0;
             for(int i=0;i<size;i++){
              n=max(n,strlen(data[i]));
             }
             return n;
            }

            void String_Pic::display(ostream& os,int row,int width)const
            {
             int start=0;
             if(row>=0&&row<height()){
              os<<data[row];
              start=strlen(data[row]);
             }
             pad(os,start,width);
            }

            String_Pic::~String_Pic()
            {
             for(int i=0;i<size;i++)
              delete[] data[i];
             delete[] data;
            }

            Frame_Pic::Frame_Pic(const Picture& pic):p(pic){}
            int Frame_Pic::height()const
            {
             return p.height()+2;
            }
            int Frame_Pic::width()const
            {
             return p.width()+2;
            }

            void Frame_Pic::display(ostream& os,int row,int wd)const
            {
             if(row<0||row>=height()){
              pad(os,0,wd);
             }else{
              if(row==0||row==height()-1){
               os<<"+";
               int i=p.width();
               while(--i>=0)
                os<<"-";
               os<<"+";
              }else{
               os<<"|";
               p.display(os,row-1,p.width());
               os<<"|";
              }
              pad(os,width(),wd);
             }
            }

            Picture frame(const Picture& pic)
            {
             return new Frame_Pic(pic);
            }

            VCat_Pic::VCat_Pic(const Picture& t,const Picture& b):
             top(t),bottom(b){}

            int VCat_Pic::height()const
            {
             return top.height()+bottom.height();
            }
            int VCat_Pic::width()const
            {
             return max(top.width(),bottom.width());
            }
            void VCat_Pic::display(ostream& os,int row,int wd)const
            {
             if(row>=0 && row< top.height())
              top.display(os,row,wd);
             else if(row <top.height()+bottom.height())
              bottom.display(os,row-top.height(),wd);
             else
              pad(os,0,wd);
            }
            Picture operator&(const Picture& t,const Picture& b)
            {
             return new VCat_Pic(t,b);
            }

            Hcat_Pic::Hcat_Pic(const Picture& l,const Picture& r):
             left(l),right(r){}

            int Hcat_Pic::height()const
            {
             return max(left.height(),right.height());
            }
            int Hcat_Pic::width()const
            {
             return left.width()+right.width();
            }
            void Hcat_Pic::display(ostream& os,int row,int wd)const
            {
             left.display(os,row,left.width());
             right.display(os,row,right.width());
             pad(os,width(),wd);
            }

            Picture operator|(const Picture& l,const Picture& r)
            {
             return new Hcat_Pic(l,r);
            }

             

            下面是 test.cpp

            #include<iostream>
            #include<cstring>
            #include"Picture.h"

            using namespace std;

            char *init[]={"Paris","in the","Spring"};
            int main()
            {
             Picture p(init,3);
             cout<<p<<endl;

             Picture q=frame(p);
             cout<<q<<endl;

             cout<<(q&(p|q))<<endl;

             cout<<frame(p|p)<<endl;

             cout<<frame(q|q)<<endl;

             cout<<(p&p)<<endl;

             cout<<(q&q)<<endl;

             cout<<frame(p&p)<<endl;
             cout<<frame(frame(q&q)|frame(p&p))<<endl;
             return 0;
            }

            閱讀全文
            類別:c++ 查看評論
            文章來源:http://hi.baidu.com/mirguest/blog/item/d91abdcd42caeb2ef8dc612e.html

            posted on 2011-02-02 12:01 mirguest 閱讀(301) 評論(0)  編輯 收藏 引用 所屬分類: C++

            色综合色天天久久婷婷基地| 久久久久久久综合狠狠综合| 国产欧美久久一区二区| 色成年激情久久综合| 久久国产成人午夜AV影院| 亚洲精品tv久久久久| 欧美丰满熟妇BBB久久久| 久久夜色撩人精品国产| 久久久久久久久久久久中文字幕| 欧美亚洲国产精品久久蜜芽| 精品国产乱码久久久久久呢 | 91性高湖久久久久| 久久久久亚洲国产| 久久99精品久久久久久野外| 中文字幕无码免费久久| 久久久久久极精品久久久| 国产91色综合久久免费| av色综合久久天堂av色综合在 | 色天使久久综合网天天| 国产成人99久久亚洲综合精品| 亚洲国产精品久久久天堂| 少妇无套内谢久久久久| 中文字幕久久精品| 欧美性大战久久久久久| 成人亚洲欧美久久久久| 精品亚洲综合久久中文字幕| 久久精品天天中文字幕人妻 | 国产精品久久久久…| 无码人妻久久一区二区三区 | 久久97久久97精品免视看| 亚洲天堂久久精品| 99久久777色| 久久婷婷综合中文字幕| 国产AⅤ精品一区二区三区久久| 久久永久免费人妻精品下载| 久久夜色精品国产网站| 亚洲色婷婷综合久久| 99久久国语露脸精品国产| 欧美久久综合性欧美| 久久免费视频一区| 婷婷久久五月天|