• <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++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

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

            我們將模板模式應(yīng)用到泡冒排序算法中,將算法中的位置交換及大小比較獨(dú)立出來作為模板使用,使算法在不改變主程序骨架的情況下支持多種數(shù)據(jù)類型,結(jié)構(gòu)圖如下:


            實(shí)現(xiàn)代碼:
            //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排序后的結(jié)果:" << intBubble << endl;

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

            最后輸出為:
            int排序后的結(jié)果:9 10 13 14 16 18 20
            double排序后的結(jié)果:9.60 10.70 13.80 18.50 28.50
            posted on 2009-02-12 17:32 emptysoul 閱讀(420) 評(píng)論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲国产成人精品久久久国产成人一区二区三区综 | 熟妇人妻久久中文字幕| 久久精品久久久久观看99水蜜桃| 久久精品国产亚洲AV久| 蜜桃麻豆www久久| 99久久国产亚洲综合精品| 久久国产乱子伦免费精品| 久久久久国产精品三级网| 色欲av伊人久久大香线蕉影院| 美女写真久久影院| A级毛片无码久久精品免费| 99久久亚洲综合精品网站| 午夜天堂精品久久久久| 国内精品久久久久久中文字幕| 伊人久久大香线蕉av一区| 久久精品国产WWW456C0M| 国产精品久久免费| 亚洲日本va中文字幕久久| 久久久精品人妻无码专区不卡| 国产精品久久久久久福利漫画| 国产69精品久久久久APP下载| 国产成人精品久久亚洲| 久久精品国产亚洲AV无码麻豆 | 99久久精品费精品国产| 亚洲AV日韩精品久久久久久| 久久精品无码一区二区日韩AV | 久久婷婷激情综合色综合俺也去| 日韩欧美亚洲国产精品字幕久久久| 97久久精品无码一区二区天美| 国产精品99久久久精品无码| 久久成人18免费网站| 久久99精品久久久久久野外| 蜜桃麻豆www久久| 国产精品99久久精品爆乳| 国产精品青草久久久久婷婷| 国产亚洲精久久久久久无码| 天天爽天天狠久久久综合麻豆| 亚洲va中文字幕无码久久不卡| 伊人久久大香线蕉亚洲五月天| 亚洲AV日韩AV天堂久久| 久久久久国产精品熟女影院|