-- 作者:z7c9
-- 发布时间:2013/8/5 15:54:39
-- [原创]Text,Binary,Database
#include <QCoreApplication> #include <QDateTime> #include <QDebug> #include <QDataStream> #include <QFile> #include <QList> #include <QStringList> #include <QTime> #include <QtSql/QSqlDatabase> #include <QtSql/QSqlQuery>
int size = 999;
class Bar { private: double open; double high; double low; double close; public: Bar(double open=0,double high=0,double low=0,double close=0); friend QDataStream& operator <<(QDataStream &out,Bar &bar); friend QDataStream& operator >>(QDataStream &in,Bar &bar); friend QTextStream& operator <<(QTextStream &out,Bar &bar); friend QTextStream& operator >>(QTextStream &in,Bar &bar); friend void writeDatabase(); friend void readBinary(); void print(); };
Bar::Bar(double open, double high, double low, double close):open(open),high(high),low(low),close(close) { }
QDataStream& operator <<(QDataStream &out,Bar &bar) { out << bar.open << bar.high << bar.low << bar.close; return out; }
QDataStream& operator >>(QDataStream &in,Bar &bar) { in >> bar.open >> bar.high >> bar.low >> bar.close; return in; }
QTextStream& operator <<(QTextStream &out,Bar &bar) { out << bar.open << "," << bar.high << "," << bar.low << "," << bar.close << endl; return out; }
QTextStream& operator >>(QTextStream &in,Bar &bar) { QStringList line= in.readLine().split(","); bar.open = line.at(0).toDouble(); bar.high = line.at(1).toDouble(); bar.low = line.at(2).toDouble(); bar.close = line.at(3).toDouble(); return in; }
void Bar::print() { qDebug() << open << high << low << close; }
void writeText() { QFile file("E:\\\\mytest.csv"); file.open(QIODevice::WriteOnly); QTextStream out(&file);
for(int i=0;i<size;++i) { Bar bar(i,i+1,i+2,i+3); out << bar; }
file.close(); }
void writeBinary() { QFile file("E:\\\\mytest.dat"); file.open(QIODevice::WriteOnly); QDataStream out(&file);
for(int i=0;i<size;++i) { Bar bar(i,i+1,i+2,i+3); out << bar; }
file.close(); }
void writeDatabase() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("E:\\\\mytest.db"); db.open();
QSqlQuery query; db.transaction(); query.exec("create table bars(open double,high double,low double,close double)");
for(int i=0;i<size;++i) { Bar bar(i,i+1,i+2,i+3);
query.prepare("insert into bars(open,high,low,close) values(:open,:high,:low,:close)"); query.bindValue(":open",bar.open); query.bindValue(":high",bar.high); query.bindValue(":low",bar.low); query.bindValue(":close",bar.close);
query.exec(); } db.commit(); }
void readText() { QFile file("E:\\\\mytest.csv"); file.open(QIODevice::ReadOnly); QTextStream in(&file);
while(!in.atEnd()) { Bar bar; in >> bar; // bar.print(); } }
void readBinary() { QFile file("E:\\\\mytest.dat"); file.open(QIODevice::ReadOnly); QDataStream in(&file);
while(!in.atEnd()) { Bar bar; in >> bar; // bar.print(); } }
void readDatabase() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("E:\\\\mytest.db"); db.open();
QSqlQuery query; query.exec("select open,high,low,close from bars");
while(query.next()) { Bar bar(query.value(0).toDouble(),query.value(1).toDouble(),query.value(2).toDouble(),query.value(3).toDouble()); // bar.print(); } }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv);
QTime time; time.start();
// writeText(); // writeBinary(); // writeDatabase();
// readText(); // readBinary(); readDatabase();
qDebug() << time.elapsed(); return a.exec(); }
[此贴子已经被作者于2013/8/5 15:55:10编辑过]
|