• <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>
            華劍緣
            一切都在這個過程中獲得,將那些目標(biāo)埋藏于心中
            posts - 19,comments - 20,trackbacks - 0

            實驗 3 對象數(shù)組與對象指針

            1 .實驗?zāi)康?/span>

            (1) 掌握數(shù)組與指針的定義與使用方法。

            (2) 理解數(shù)組與指針的存儲分配與表示。

            (3) 學(xué)習(xí)向函數(shù)傳遞數(shù)組的方法。

            (4) 學(xué)習(xí)用指針和引用向函數(shù)傳遞參數(shù)。

            (5) 學(xué)習(xí)靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的使用。

            (6) 理解友元與友元函數(shù)的作用與使用方法。

            2 .實驗基本要求

            (1) 設(shè)計一個矩陣類 Matrix( 矩陣由二維數(shù)組實現(xiàn) ) ,有分配空間和對矩陣賦值的功能。

            (2) 練習(xí)將這個矩陣類的對象作為參數(shù)傳送到函數(shù) Mul() ,用普通、指針和引用三種方法實現(xiàn),并要注意這三種方式的區(qū)別。

            ??? ①直接傳送: Mul(Matrix a Matrix b) 。實際上只是傳送值,在函數(shù)中針對對象的任何修改均不影響該對象本身。

            ??? ②指針傳送: Mul(Matrix *pa Matrix *pb) 。要注意指針的級數(shù)。

            ??? ③引用傳送: Mul(Matrix & a Matrix & b) 。這種調(diào)用將影響參數(shù)的實際值。

            (3) Mul() 函數(shù)實現(xiàn):完成對傳送的兩個 Matrix 對象的相乘運算。下面給出矩陣相乘的算法:

            ??? ①矩陣 a[i][j] 與矩陣 b[x][y] 相乘,條件是 j==x

            ??? ②乘積是一個新的矩陣 c[i][y] ,其中 c[i][y] 的值是∑ (a[i][k]* b[k][y]) ,其中 K 0,l,…,j

            (4) Matrix 類中定義一個靜態(tài)數(shù)據(jù)成員,記錄當(dāng)前的所有 Matrix 對象的數(shù)量。

            (5) 定義一個友元函數(shù)實現(xiàn)轉(zhuǎn)置的功能。轉(zhuǎn)置是指將數(shù)組中 a[i][j] a[j] [i] 的值對調(diào)。

            3 .實驗基本步驟

            (1) 建立一個工程。在工程中定義一個 Matrix 類,在構(gòu)造函數(shù)中根據(jù)參數(shù)創(chuàng)建數(shù)據(jù)成員:一個二維數(shù)組。提示:用構(gòu)造函數(shù)記錄二維數(shù)組的大小 (unsigned int x unsigned int y) 類中實際定義的二維數(shù)組的數(shù)據(jù)成員是一個指針 ( 二級指針 ) int **pMatrix 在構(gòu)造函數(shù)中根據(jù)傳送的參數(shù)為這個二維數(shù)組分配空間: pMatrix newint[x][y]

            (2) 設(shè)計成員函數(shù),完成對數(shù)組的賦值的功能。本例中定義的成員函數(shù)為 SetValue(unsigned int x unsigned int y int value)

            (3) 參考以上的說明,以常用三種方式實現(xiàn)向 Mul() 函數(shù)傳送參數(shù),并返回矩陣相乘的結(jié)果。

            (4) 在類中定義一個靜態(tài)的數(shù)據(jù)成員 ObjectAliveNo ,記錄當(dāng)前共有幾個 Matrix 類的對象。實現(xiàn)方法:可以在對象的構(gòu)造函數(shù)中向該數(shù)據(jù)成員報告 ( 使靜態(tài)數(shù)據(jù)成員加 1) ;在析構(gòu)函數(shù)中也向該數(shù)據(jù)成員報告 ( 使靜態(tài)數(shù)據(jù)成員減 1) 。并要注意,在程序開始時,給這個靜態(tài)數(shù)據(jù)成員賦初值。

            (5) Matrix 類中定義一個友元函數(shù),使其具有對 Matrix 類的對象內(nèi)的數(shù)組進(jìn)行轉(zhuǎn)置的功能。




            ?

            #include? < iostream >
            using ? namespace ?std;
            class ?Matrix
            {
            private :
            ????
            int ?rows,columns;
            public :
            int ? ** pMatrix;
            Matrix(?
            int ?rows, int ?columns);
            Matrix(Matrix
            & ?);
            ~ Matrix();
            int ?GetRows();
            int ?GetColumns();
            void ?SetValue();
            void ?Mul(Matrix? & ?a,Matrix? & ?b);


            }
            ;

            int ?Matrix::GetRows() { return ?rows;} ;
            int ?Matrix::GetColumns() { return ?columns;} ;

            // 構(gòu)造函數(shù)
            Matrix::Matrix( int ?x, int ?y)
            {
            rows
            = x;
            columns
            = y;
            pMatrix
            = new ? int * ?[x];
            for ( int ?i = 0 ;?i < x;?i ++ )
            ??pMatrix[i]
            = new ? int ?[y];
            }


            // 析構(gòu)函數(shù)
            Matrix:: ~ Matrix()
            {
            for ( int ?i = 0 ;i < columns;i ++ )
            ??delete?[]pMatrix[i];
            ????delete[]?pMatrix;
            }


            // 賦值函數(shù)
            void ?Matrix::SetValue()
            {
            ????
            int ?i,j,value;
            ????
            for (?i = 0 ;?i < rows;?i ++ )
            {
            ??
            for (?j = 0 ;?j < columns;?j ++ )
            ??
            {
            ???cout
            << " " << i << " " ;
            ???cout
            << " " << j << " 列: " ;
            ???cin
            >> value;
            ???cout
            << endl;
            ???pMatrix[i][j]
            = value;
            ??}

            }

            }


            // 拷貝構(gòu)造函數(shù)
            Matrix::Matrix(Matrix & ?M)
            {
            for ( int ?i = 0 ;?i < M.rows;?i ++ )
            ??
            for ( int ?j = 0 ;?j < M.columns;?j ++ )
            ???pMatrix[i][j]
            = M.pMatrix[i][j];????? /// 這樣對么?
            }


            void ?Matrix::Mul(?Matrix? & ?a,?Matrix? & ?b)
            {
            Matrix?c(a.GetRows(),b.GetColumns());
            int ?temp = 0 ;
            ????
            for ( int ?ai = 0 ;ai < a.GetRows();ai ++ )
            {

            ??
            for ( int ?bj = 0 ;bj < b.GetColumns();bj ++ )
            ??
            {
            ???
            for ( int ?aj = 0 ;aj < a.GetColumns();aj ++ )
            ????temp
            = temp + a.pMatrix[ai][aj] * b.pMatrix[aj][bj];
            ???c.pMatrix[ai][bj]
            = temp;
            ???temp
            = 0 ;
            ??}

            }

            for ( int ?i = 0 ;i < c.GetRows();i ++ ) { // 輸出相乘后的矩陣
            ???cout << ' \n ' ;
            ??
            for ( int ?j = 0 ;j < c.GetColumns();j ++ )
            ???cout
            << c.pMatrix[i][j] << " ?? " ;
            ?}

            }

            // 主函數(shù)
            int ?main()
            {
            Matrix?Ma(
            2 , 2 ),Mb( 2 , 2 );
            Ma.SetValue();
            Mb.SetValue();

            for ( int ?i = 0 ;i < Ma.GetRows();i ++ )
            {
            ????cout
            << ' \n ' ;
            ??
            for ( int ?j = 0 ;j < Ma.GetColumns();j ++ )
            ???cout
            << Ma.pMatrix[i][j] << " ? " ;
            }

            cout
            << endl;
            Matrix?Mc(
            2 , 2 );
            Mc.Mul(Ma,Mb);
            /*
            for(int?i=0;i<Mc.GetRows();i++)
            {
            ????cout<<'\n';
            ??for(int?j=0;j<Mc.GetColumns();j++)
            ???cout<<Mc.pMatrix[i][j]<<"?";
            }
            */

            return ? 0 ;
            }

            posted on 2006-04-03 16:21 華劍緣 閱讀(1368) 評論(0)  編輯 收藏 引用

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


            精品国产综合区久久久久久| 四虎久久影院| 久久se精品一区二区影院| 日批日出水久久亚洲精品tv| 新狼窝色AV性久久久久久| 99久久精品国产一区二区蜜芽| 伊人精品久久久久7777| 久久亚洲精品成人AV| 久久久噜噜噜久久中文字幕色伊伊| 久久综合亚洲色HEZYO社区| 久久精品一区二区国产| 久久久久久综合网天天| 国产精品久久久久一区二区三区| 精品久久亚洲中文无码| 久久综合亚洲色HEZYO国产| 久久本道伊人久久| 久久久久亚洲av无码专区导航| 无码8090精品久久一区| 国产精品免费久久| 久久国产精品77777| 亚洲国产美女精品久久久久∴| 久久影院久久香蕉国产线看观看| 99久久精品国产免看国产一区| 伊人久久大香线蕉亚洲五月天| 人妻系列无码专区久久五月天| 亚洲伊人久久大香线蕉苏妲己 | 亚洲国产精品成人久久蜜臀| 欧美一区二区精品久久| 国产精品青草久久久久婷婷| 色妞色综合久久夜夜| 亚洲日本va中文字幕久久| 精品国产青草久久久久福利| 久久丫忘忧草产品| 国内高清久久久久久| 99久久精品免费看国产一区二区三区 | 亚洲国产高清精品线久久 | 久久精品人人做人人爽97| 久久婷婷五月综合97色| 久久久久久亚洲AV无码专区| 人人狠狠综合久久88成人| 久久综合噜噜激激的五月天|