• <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>

            emptysoul

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            模板模式(Template)定義一個操作中算法的骨架,將一些步驟的執行延遲到其子類中。結構圖如下:
             

            我們將模板模式應用到泡冒排序算法中,將算法中的位置交換及大小比較獨立出來作為模板使用,使算法在不改變主程序骨架的情況下支持多種數據類型,結構圖如下:


            實現代碼:
            //Bubblesort.h
            class Bubblesort
            {
            public:
                
            virtual ~Bubblesort();

                
            void DoSort();
                
            virtual void Swap(int= 0;
                
            virtual bool IsNeedtoSwap(int= 0;
            protected:
                Bubblesort();
                
            int m_nLength;
            };

            //Bubblesort.cpp
            #include "stdafx.h"
            #include 
            "Bubblesort.h"

            Bubblesort::Bubblesort()
            {
                m_nLength 
            = 0;
            }

            Bubblesort::
            ~Bubblesort()
            {

            }

            void Bubblesort::DoSort()
            {
                
            for (int i = m_nLength - 2; i >= 0--i)
                {
                    
            for(int j = 0; j <= i; ++j)
                    {
                        
            if(IsNeedtoSwap(j))
                        {
                            Swap(j);
                        }
                    }
                }
            }

            //IntBubblesort.h
            #include <iostream>
            #include 
            "Bubblesort.h"

            class IntBubblesort : public Bubblesort
            {
            public:
                IntBubblesort();
                
            virtual ~IntBubblesort();

                
            void Sort(int*int);
                
            void Swap(int);
                
            bool IsNeedtoSwap(int);

                friend std::ostream
            & operator<<(std::ostream& os, IntBubblesort& bubble);
            private:
                
            int* m_pArray;
            };

            //IntBubblesort.cpp
            #include "stdafx.h"
            #include 
            "IntBubblesort.h"

            IntBubblesort::IntBubblesort()
            {
                
            }

            IntBubblesort::
            ~IntBubblesort()
            {

            }

            void IntBubblesort::Sort(int* pArray, int nLength)
            {
                
            this->m_pArray = pArray;
                
            this->m_nLength = nLength;
                
            this->DoSort();
            }

            void IntBubblesort::Swap(int nIndex)
            {
                
            int nTemp = m_pArray[nIndex];
                m_pArray[nIndex] 
            = m_pArray[nIndex + 1];
                m_pArray[nIndex 
            + 1= nTemp;
            }

            bool IntBubblesort::IsNeedtoSwap(int nIndex)
            {
                
            return m_pArray[nIndex] > m_pArray[nIndex + 1];
            }

            std::ostream
            & operator<<(std::ostream& os, IntBubblesort& bubble)
            {
                
            for(int i = 0; i < bubble.m_nLength; ++i)
                {
                    os 
            << bubble.m_pArray[i] << " ";
                }

                
            return os;
            }

            //DoubleBubblesort.h
            #include <iostream>
            #include 
            "Bubblesort.h"

            class DoubleBubblesort : public Bubblesort
            {
            public:
                DoubleBubblesort();
                
            virtual ~DoubleBubblesort();

                
            void Sort(double*int);
                
            void Swap(int);
                
            bool IsNeedtoSwap(int);

                friend std::ostream
            & operator<<(std::ostream& os, DoubleBubblesort& bubble);
            private:
                
            double* m_pArray;
            };

            //DoubleBubblesort.cpp
            #include "stdafx.h"
            #include 
            "DoubleBubblesort.h"
            #include 
            <iomanip>

            DoubleBubblesort::DoubleBubblesort()
            {

            }

            DoubleBubblesort::
            ~DoubleBubblesort()
            {

            }

            void DoubleBubblesort::Sort(double* pArray, int nLength)
            {
                
            this->m_pArray = pArray;
                
            this->m_nLength = nLength;
                
            this->DoSort();
            }

            void DoubleBubblesort::Swap(int nIndex)
            {
                
            int nTemp = m_pArray[nIndex];
                m_pArray[nIndex] 
            = m_pArray[nIndex + 1];
                m_pArray[nIndex 
            + 1= nTemp;
            }

            bool DoubleBubblesort::IsNeedtoSwap(int nIndex)
            {
                
            return m_pArray[nIndex] > m_pArray[nIndex + 1];
            }

            std::ostream
            & operator<<(std::ostream& os, DoubleBubblesort& bubble)
            {
                
            for(int i = 0; i < bubble.m_nLength; ++i)
                {
                    os 
            << std::fixed << std::setprecision(2<< bubble.m_pArray[i] << " ";
                }

                
            return os;
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "Bubblesort.h"
            #include 
            "IntBubblesort.h"
            #include 
            "DoubleBubblesort.h"
            #include 
            <iostream>

            using namespace std;

            int main(int argc, char* argv[])
            {
                
            int nArray[] = {2014169101318};
                
            int nLen = sizeof nArray / sizeof nArray[0];
                IntBubblesort intBubble;
                intBubble.Sort(nArray, nLen);
                cout 
            << "int排序后的結果:" << intBubble << endl;

                
            double dArray[] = {9.610.713.818.528.5};
                nLen 
            = sizeof dArray / sizeof dArray[0];
                DoubleBubblesort dlBubble;
                dlBubble.Sort(dArray, nLen);
                cout 
            << "double排序后的結果:" << dlBubble << endl;
                
                
            return 0;
            }

            最后輸出為:
            int排序后的結果:9 10 13 14 16 18 20
            double排序后的結果:9.60 10.70 13.80 18.50 28.50
            posted on 2009-02-12 17:32 emptysoul 閱讀(415) 評論(0)  編輯 收藏 引用
            久久人与动人物a级毛片| 久久久av波多野一区二区| 久久久精品国产Sm最大网站| 欧美成a人片免费看久久| 久久99这里只有精品国产| 久久狠狠高潮亚洲精品| 久久久久久久久久久免费精品| 思思久久精品在热线热| 精品午夜久久福利大片| 久久久久亚洲AV无码专区首JN | 国产免费久久精品99re丫y| 欧美精品久久久久久久自慰| 欧美精品一区二区精品久久| 国产综合免费精品久久久| 人妻无码久久一区二区三区免费| 国产日韩久久久精品影院首页| 无码伊人66久久大杳蕉网站谷歌 | 久久99热这里只有精品国产| 久久免费精品视频| 亚洲人成精品久久久久| 久久国产精品一区| 久久99久久99小草精品免视看| 久久人人爽人人爽人人片AV麻烦| 91麻精品国产91久久久久| 热re99久久6国产精品免费| 亚洲国产天堂久久综合| 国产精品久久久久久久午夜片| 久久久久无码精品国产不卡| 99久久精品国产一区二区| 综合久久一区二区三区| 色8激情欧美成人久久综合电| 人人狠狠综合久久亚洲婷婷| 国内精品久久久人妻中文字幕| 伊人久久精品无码av一区| 中文字幕无码av激情不卡久久| 超级碰久久免费公开视频| 婷婷综合久久中文字幕| 久久精品国产亚洲网站| 久久99精品久久只有精品| 国内精品伊人久久久久av一坑| 久久亚洲日韩精品一区二区三区|