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
| #include "mainwindow.h"
#include <iostream>
#include <fstream>
#include <C:/Users/MPA/Desktop/eigen-3.3.7/eigen-3.3.7/Eigen/Dense>
#include <C:/Users/MPA/Desktop/eigen-3.3.7/eigen-3.3.7/Eigen/Lu>
#include <QApplication>
#include "c:/qt/essaicsv.csv"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
using Eigen::MatrixXd;
using namespace std;
int csvRead(MatrixXd& outputMatrix, const string& fileName, const streamsize dPrec) {
ifstream inputData;
inputData.open(fileName);
cout.precision(dPrec);
if (!inputData)
return -1;
string fileline, filecell;
unsigned int prevNoOfCols = 0, noOfRows = 0, noOfCols = 0;
while (getline(inputData, fileline)) {
noOfCols = 0;
stringstream linestream(fileline);
while (getline(linestream, filecell, ',')) {
try {
stod(filecell);
}
catch (...) {
return -1;
}
noOfCols++;
}
if (noOfRows++ == 0)
prevNoOfCols = noOfCols;
if (prevNoOfCols != noOfCols)
return -1;
}
inputData.close();
outputMatrix.resize(noOfRows, noOfCols);
inputData.open(fileName);
noOfRows = 0;
while (getline(inputData, fileline)) {
noOfCols = 0;
stringstream linestream(fileline);
while (getline(linestream, filecell, ',')) {
outputMatrix(noOfRows, noOfCols++) = stod(filecell);
}
noOfRows++;
}
return 0;
}
int main()
{
int error;
MatrixXd A;
error = csvRead(A, "c:/qt/essaicsv.csv",20);
if (error == 0) {
cout << "Matrix (" << A.rows() << "x" << A.cols() << "):" << endl;
cout << A << endl;
getchar();
}
return 0;
} |
Partager