funtion that counts coloumn.

Pages: 12
Some more ideas.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;


class ProcessFile {
    string  mType;
    bool    mHaveDimensions = false;
    int     mRows = 0;
    int     mCols = 0;
    int     mCount = 0;
    enum {
        INTS,
        REALS,
        PATTERN
    }       mTypeID;

    void processHeaderOrComment(const string &line) {
        if ( mType == "" ) {
            if ( line.find("%%MatrixMarket") != std::string::npos ) {
                stringstream is(line);
                string ident, matrix, coordinate, general;
                is >> ident >> matrix >> coordinate >> mType >> general;
                if ( mType == "integer" ) mTypeID = INTS;
                if ( mType == "real" ) mTypeID = REALS;
                if ( mType == "pattern" ) mTypeID = PATTERN;
            }
        }
    }

    void processCoordData(const string &line) {
        int x, y, iVal;
        double rVal;
        stringstream is(line);
        switch ( mTypeID ) {
            case INTS:
                if ( is >> x >> y >> iVal ) {
                }
                break;
            case REALS:
                if ( is >> x >> y >> rVal ) {
                }
                break;
            case PATTERN:
                if ( is >> x >> y ) {
                    cout << "Pattern " << x << "," << y << endl;
                }
                break;
        }
    }

    void processLine(const string &line) {
        if ( line[0] == '%' ) {
            processHeaderOrComment(line);
        } else if ( !mHaveDimensions ) {
            stringstream is(line);
            if ( is >> mRows >> mCols >> mCount ) {
                mHaveDimensions = true;
            }
        } else {
            processCoordData(line);
        }
    }

public:
    void processFile(const char *filename)
    {
        ifstream inf(filename);
        if ( inf.is_open() ) {
            string line;
            while ( getline(inf,line) ) {
                processLine(line);
            }
        } else {
            cout << "Sorry, but the file don't exist!\n";
        }
    }
};

int main(int argc, char *argv[])
{
    if (argc==2) {
        cout <<"Filename: " << argv[1] << endl;
        ProcessFile f;
        f.processFile(argv[1]);
    } else {
        cout << "give a filename\n";
    }
}

$ g++ -std=c++11 foo.cpp
$ cat sparse01.mtx 
%%MatrixMarket matrix coordinate pattern general
9 9 50
1 1
2 1
$ ./a.out sparse01.mtx 
Filename: sparse01.mtx
Pattern 1,1
Pattern 2,1

Topic archived. No new replies allowed.
Pages: 12