C++ (Qt)#include <QString>#include <QDebug>#include <QFile>#include <QTextStream>#include <QVector>#include <QPoint>#include <QRegExp>#include <QList> bool extractData(const QString & str, int & x, int & y){ static const QString prefixX = "+X"; static const QString prefixY = "-Y"; static const QChar space = ' '; if (!str.startsWith(prefixY)) { qDebug() << "1 ;("; return false; } int n = prefixY.size(); if (str[n] != space) { qDebug() << "2 ;("; return false; } if (++n == str.size()) { qDebug() << "3 ;("; return false; } int m = str.indexOf(space, n); if (m == -1) { qDebug() << "4 ;("; return false; } bool ok = false; y = str.mid(n, m-n).toInt(&ok); if (!ok) { qDebug() << "5 ;("; return false; } if (str[m] != space) { qDebug() << "6 ;("; return false; } n = str.indexOf(prefixX, ++m); if (n != m) { qDebug() << "7 ;("; return false; } n += prefixX.size(); if (n == str.size()) { qDebug() << "8 ;("; return false; } x = str.mid(++n, str.size()).toInt(&ok); if (!ok) { qDebug() << "9 ;("; return false; } return true; }
C++ (Qt)template <class Container>void readData_QRegExManner(const QString & fileName, Container & v){ QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Error opening file"; return; } QTextStream stream(&file); QString str = stream.readAll(); QRegExp regex("\\-Y (\\d+) \\+X (\\d+)"); regex.setMinimal(true); int pos = 0; while ((pos = regex.indexIn(str, pos)) != -1) { int y = regex.cap(1).toInt(); int x = regex.cap(2).toInt(); v.append(QPoint(x, y)); pos += regex.matchedLength(); } }
C++ (Qt)bool extractData(const QString & str, int & x, int & y){ static const QString prefixX = "+X"; static const QString prefixY = "-Y"; static const QChar space = ' '; if (!str.startsWith(prefixY)) { qDebug() << "1 ;("; return false; } int n = prefixY.size(); if (str[n] != space) { qDebug() << "2 ;("; return false; } if (++n == str.size()) { qDebug() << "3 ;("; return false; } int m = str.indexOf(space, n); if (m == -1) { qDebug() << "4 ;("; return false; } bool ok = false; y = str.mid(n, m-n).toInt(&ok); if (!ok) { qDebug() << "5 ;("; return false; } if (str[m] != space) { qDebug() << "6 ;("; return false; } n = str.indexOf(prefixX, ++m); if (n != m) { qDebug() << "7 ;("; return false; } n += prefixX.size(); if (n == str.size()) { qDebug() << "8 ;("; return false; } x = str.mid(++n, str.size()).toInt(&ok); if (!ok) { qDebug() << "9 ;("; return false; } return true; }