• <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 閱讀(305) 評論(0)  編輯 收藏 引用 所屬分類: C++

            久久久久AV综合网成人| 超级碰久久免费公开视频| 欧美日韩精品久久久免费观看| 欧美国产成人久久精品| 久久强奷乱码老熟女网站 | 精品无码久久久久久国产| 免费精品久久久久久中文字幕| 伊人久久大香线蕉av不卡| 国产精品美女久久久免费| 久久久亚洲裙底偷窥综合| 91久久精品国产成人久久| 国产精品久久久久久久人人看| 久久精品国产免费一区| 精品国产青草久久久久福利| 9999国产精品欧美久久久久久| 亚洲午夜久久久影院伊人| 狠狠色丁香婷婷综合久久来来去| 色狠狠久久AV五月综合| 久久综合精品国产一区二区三区| 97久久香蕉国产线看观看| 色播久久人人爽人人爽人人片AV| 国产精品久久久天天影视香蕉 | www.久久精品| 久久久久av无码免费网| 久久强奷乱码老熟女| 99久久无码一区人妻| 久久精品www人人爽人人| 日韩人妻无码一区二区三区久久99| 亚洲成人精品久久| 国产亚洲欧美成人久久片| 亚洲AV日韩精品久久久久| 久久综合久久综合亚洲| 少妇被又大又粗又爽毛片久久黑人| 国产精品热久久无码av| 精品久久国产一区二区三区香蕉 | 精品久久香蕉国产线看观看亚洲| 77777亚洲午夜久久多喷| 亚洲中文久久精品无码ww16| 久久青青草视频| 色婷婷久久综合中文久久蜜桃av| 成人久久免费网站|