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

            liyuxia713

            蹣跚前行者

            常用鏈接

            統計

            Algorithms

            C++

            最新評論

            矩陣類

            自己編寫的一個矩陣類,個人感覺從文件中讀取矩陣和將矩陣寫入文件這兩個函數作用大些。
            收獲:1. 對類的static成員函數的作用有所了解。
                       2. 對文件的讀寫操作熟練了一些。clear,seekg等
                       3. 對異常處理的初級應用。
            下面把代碼貼出來吧

            //Matrix.h
            //矩陣類定義

            #ifndef MATRIX_H
            #define MATRIX_H
            #include 
            <iostream>
            #include 
            <string>
            //using namespace std; 一般頭文件中使用完全限定域名字,而不是包含命名空間,防止重復包含頭文件時造成的資源浪費

            class Matrix 
            {
                
            //從流中讀入矩陣
                friend std::istream& operator >> (std::istream& is, Matrix A);
                
            //輸出矩陣
                friend std::ostream& operator << (std::ostream& os, const Matrix& A);
                
            //將矩陣輸出到名為str的文件中
                friend void print_file(const Matrix&A,const char* str);
            public:
                
            //定義空矩陣
                Matrix() {elems = NULL; row = 0; col = 0;};
                
            //定義m*n零矩陣
                Matrix(int m, int n);
                
            //定義m*n矩陣,由a初始化
                Matrix(int m, int n, double *a, int size = 0);    
                
            //復制構造函數
                Matrix(const Matrix& B);
                
            //從文件str中讀取矩陣
                Matrix(const char* str);

                
            ~Matrix() {delete[]elems; row = 0; col = 0;    };
                
                
            //重載算數操作符
                Matrix& operator = (Matrix& B);
                Matrix 
            operator +(const Matrix&B)const;
                Matrix 
            operator -(const Matrix&B)const;
                Matrix 
            operator *(const Matrix&B)const;

                
            //返回矩陣第i行第j列元素
                double& operator()(int i,int j)const;
                
                
            double get_row()const {return row;};
                
            double get_col()const {return col;};
                
                
            //矩陣轉置
                Matrix& trans()const;
                
            protected:
            private:
                
            double* elems;
                
            int row, col;
            }
            ;

            #endif

              1//Matrix.cpp
              2//函數實現
              3
              4#include "Matrix.h"
              5#include <iostream>
              6#include <fstream>
              7#include <sstream>
              8#include <string>
              9#include <stdexcept>
             10
             11using namespace std;
             12
             13//重載下標操作符,返回A[i,j]
             14double& Matrix::operator()(int i,int j)const
             15{
             16    if(i<0 || i >= row || j < 0 || j >= col)
             17        throw out_of_range("The suffix is out of range");
             18    
             19    return elems[i*col+j];
             20}

             21
             22//從輸入流中讀入矩陣
             23istream& operator >>(istream& is, Matrix&A)
             24{
             25    for(int i = 0; i != A.get_row(); ++i)
             26        for(int j = 0; j != A.get_col(); ++j)
             27            is >> A(i,j);
             28    return is;
             29}

             30
             31//輸出矩陣
             32ostream& operator <<(ostream& os, const Matrix& A)
             33{
             34    for(int i = 0; i != A.get_row(); ++i)
             35    {
             36        for(int j = 0; j != A.get_col(); ++j)
             37            os << A(i,j) << " ";
             38        cout <<endl;
             39    }

             40    cout << "------------------------" <<endl;
             41
             42    return os;
             43    
             44}
            ;
             45
             46//將矩陣A輸出到文件str中
             47void print_file(const Matrix&A,const char* str)
             48{
             49    ofstream outfile("Matrix_out.txt",ios::app);
             50    if(!outfile)
             51        throw domain_error("Cannot open this file.");
             52    
             53    for(int i = 0; i != A.row; ++i)
             54    {
             55        for(int j = 0; j!= A.col; ++j)
             56            outfile << A(i,j);
             57        outfile << endl;
             58    }

             59    outfile << "----------------------"<<endl;            
             60
             61    outfile.clear();
             62    outfile.close();
             63}

             64
             65//構造m*n零矩陣
             66Matrix::Matrix(int m, int n):row(m),col(n)
             67{
             68    if(m <1 || n <1)
             69        throw out_of_range("The row or column number should be larger than 0.");
             70    elems = new double[m*n];
             71    for(int i = 0; i != m*n; ++i)
             72        elems[i] = 0;
             73}

             74
             75//構造m*n矩陣,從數組a中讀入數據存儲到矩陣中
             76Matrix::Matrix(int m, int n,double* a, int size):row(m),col(n)
             77{
             78    if(m <0 || n<0 || size < m*n)
             79        throw out_of_range("The suffix or size are out of range");
             80
             81    elems = new double[m*n];    
             82    for(int i = 0; i != m*n; ++i)
             83        elems[i] = a[i];
             84
             85}
            ;
             86
             87//從文件中讀入矩陣
             88Matrix::Matrix(const char* str)
             89{
             90    //忘了剛開始的行列初始化,導致錯誤,尋找了半天。
             91    row = 0;
             92    col = 0;
             93    
             94    ifstream infile(str,ios::in);
             95    if(!infile)
             96    {
             97        throw domain_error("Cannot find this file.");
             98    }

             99
            100    char ch = ' ';
            101    //計算列數
            102    while(infile.get(ch) && ch != '\n')
            103    {
            104        if(ch == ' ')  ++col;        
            105    }

            106    ++col;    
            107    
            108    //計算行數
            109    infile.clear(); //在這里這個語句不必要
            110    infile.seekg(0,ios::beg);//千萬不能忘了重定位到文件頭
            111    while(infile.get(ch))
            112    {
            113        if(ch == '\n'++row;
            114    }

            115    ++row;
            116
            117    infile.clear();//已經讀到文件尾時想重新定位到文件頭必須有這條語句
            118    infile.seekg(0,ios::beg);
            119    
            120    elems = new double[row*col];
            121    int i = 0;
            122    while(i != row*col)
            123        infile >> elems[i++];
            124
            125    infile.clear();
            126    infile.close();
            127    
            128}

            129
            130//矩陣復制構造函數
            131Matrix::Matrix(const Matrix& B):row(B.row),col(B.col)
            132{
            133    if((row != B.row) || (col != B.col)) 
            134        throw invalid_argument("The Matrix should be matched.");
            135    
            136    elems = new double[row*col];
            137    for(int i = 0; i != row*col; ++i)
            138        elems[i] = B.elems[i];
            139    
            140}
            ;
            141
            142//重載矩陣賦值操作符    
            143Matrix& Matrix::operator = (Matrix& B)
            144{
            145    
            146    if((row != B.row) || (col != B.col)) 
            147        throw invalid_argument("The matrix should be matched.");
            148    row = B.row;
            149    col = B.col;
            150    elems = new double[row*col];
            151    for(int i = 0; i != row*col; ++i)
            152        elems[i] = B.elems[i];
            153
            154    return *this;
            155}
            ;
            156
            157//重載矩陣相加操作符
            158Matrix Matrix::operator+(const Matrix& B)const
            159{
            160    if((row != B.row) || (col != B.col)) 
            161        throw invalid_argument("The matrix should be matched");    
            162
            163    Matrix& T = * new Matrix;
            164    T.row = row;
            165    T.col = col;
            166    T.elems = new double[row*col];
            167
            168    for(int i = 0; i != row*col; ++i)
            169        T.elems[i] = elems[i] + B.elems[i];
            170    return T;
            171
            172}
            ;
            173
            174//重載矩陣相減操作符
            175Matrix Matrix::operator-(const Matrix& B)const
            176{
            177    if((row != B.row) || (col != B.col)) 
            178        throw invalid_argument("The matrix should be matched");    
            179
            180    Matrix& T = * new Matrix;
            181    T.row = row;
            182    T.col = col;
            183    T.elems = new double[row*col];
            184
            185    for(int i = 0; i != row*col; ++i)
            186        T.elems[i] = elems[i] - B.elems[i];
            187    return T;
            188
            189}
            ;
            190
            191//重載矩陣相乘操作符
            192Matrix Matrix::operator *(const Matrix& B)const
            193{
            194    if( col != B.row)
            195        throw invalid_argument("The matrix should be matched.");    
            196
            197    Matrix& T = *new Matrix;
            198    T.row = row;
            199    T.col = B.col;
            200    T.elems = new double[T.row * T.col];
            201
            202    for(int i = 0; i != T.row; ++i)
            203        for(int j = 0; j != T.col; ++j)
            204        {
            205            T.elems[i * T.col + j] = 0;
            206            for(int k = 0; k != col; ++k)
            207                T.elems[i * T.col + j] += elems[i * col + k] * B.elems[k*B.col + j];
            208        }

            209
            210    return T;
            211}
            ;
            212    
            213//轉置矩陣
            214Matrix& Matrix::trans()const
            215{
            216    Matrix& T = *new Matrix; //new 返回的是指針,需要解引用
            217    T.row = col;
            218    T.col = row;
            219    T.elems = new double[row*col];
            220    for(int i = 0; i != T.row; ++i)
            221        for(int j = 0; j != T.col; ++j)
            222            T.elems[i*T.col + j] = elems[j*col + i];
            223    return T;
            224}

             1//Mainfun.cpp
             2//測試編寫的矩陣類
             3#include "Matrix.h"
             4#include <iostream>
             5#include <string>
             6#include <fstream>
             7#include <cstdlib> 
             8#include <stdexcept>
             9
            10using namespace std;
            11
            12int main()
            13{
            14    double d[12= {1,2,3,4,5,6,7,8,1,2,3,4};
            15    double d2[12= {1,2,3,4,1,2,3,4,5,6,7,8};
            16    Matrix A(3,4,d,12);
            17    Matrix B(3,4,d2,12);
            18    Matrix C= B.trans();
            19    
            20    cout << "A = \n" << A << "B = \n" << B << "C = \n" <<C<<endl;
            21    cout << "A + B \n" << A + B << "B*C \n" << B*<<endl;
            22
            23    //將矩陣輸出到文件Matrix_out.txt中
            24    print_file(A,"Matrix_out.txt");
            25    
            26    //從文件"Matrix_in.txt"中讀取矩陣
            27    Matrix D("Matrix_in.txt");
            28        cout << D <<endl;    
            29
            30    //異常處理的寫起來太繁瑣了,只示例一個,其他省略了。
            31    try
            32    {
            33        Matrix D(0,3);
            34    }

            35    catch(out_of_range& err)
            36    {
            37        cerr << err.what() <<endl;
            38    }
                
            39
            40    system("pause");
            41    return 0;
            42}

            43

            posted on 2009-04-16 19:49 幸運草 閱讀(3612) 評論(4)  編輯 收藏 引用 所屬分類: Data Structure

            評論

            # re: 矩陣類 2011-03-23 17:15 ss

            您好 我使用您的從文件中讀取矩陣數據,在判斷行列數的時候,當矩陣數據形式為科學計數法例如1.341e+002這樣形式,判斷結果會出錯?請問是什么原因呢?謝謝 盼回復  回復  更多評論   

            # re: 矩陣類 2011-04-05 21:19 幸運草

            @ss
            謝謝!
            如果只是判斷行列數的話,應該沒有問題吧。
            當然代碼也有缺陷,如果空格不小心連續輸入了兩次,就會多算一列。  回復  更多評論   

            # re: 矩陣類 2011-04-17 11:51 ss

            @幸運草
            沒問題 是我不小心弄多了個空格 汗 thankyou  回復  更多評論   

            天天综合久久一二三区| 国产激情久久久久影院| 久久福利青草精品资源站免费| 国内精品久久国产| 久久毛片一区二区| 亚洲人AV永久一区二区三区久久 | 中文字幕无码久久人妻| 狠狠精品干练久久久无码中文字幕| 久久天天躁狠狠躁夜夜96流白浆| 国产69精品久久久久观看软件| 久久精品成人一区二区三区| 26uuu久久五月天| 国产精品无码久久久久| 精品久久久久久国产三级| 久久精品夜色噜噜亚洲A∨| 精品视频久久久久| 久久亚洲精品无码播放| 亚洲欧洲精品成人久久奇米网| 无码国内精品久久综合88 | 久久精品国产亚洲av麻豆色欲| 伊人久久大香线蕉AV色婷婷色| 久久综合狠狠综合久久综合88| 久久亚洲精品国产精品| 久久九九全国免费| 久久强奷乱码老熟女网站| 无码国内精品久久综合88| 国产Av激情久久无码天堂| 欧美777精品久久久久网| 久久亚洲精品无码播放| 亚洲AV无码1区2区久久| 国产精品久久影院| 亚洲精品高清一二区久久| 久久人人爽人人爽人人AV东京热| 久久亚洲高清观看| 久久精品国产亚洲av麻豆图片| 亚洲精品国精品久久99热一| 国内精品久久久久影院优| 日本久久中文字幕| 久久99精品国产麻豆宅宅 | 久久免费小视频| 久久人人爽人人爽人人片AV东京热|