Попав под "вики-эффект" наткнулся на статью про
этот алгоритм и захотелось сделать
#ifndef LUHNALGORITHM_H
#define LUHNALGORITHM_H
#include <QString>
#include <QVector>
class LuhnAlgorithm
{
public:
LuhnAlgorithm( const QString & n = "" );
LuhnAlgorithm & execute();
LuhnAlgorithm & execute(const QString & number );
int getSum() const;
bool isValid() const;
void setNumberCard( const QString & number );
QVector < qint16 > getSequence();
static QVector < qint16 > getSequence(const QString & numberCard );
private:
QString numberCard;
int sum;
QVector < qint16 > digits;
bool valid;
};
#endif // LUHNALGORITHM_H
#include "luhnalgorithm.h"
const int getSumSequence( const QVector < qint16 > & arr )
{
int sum = 0;
foreach ( qint16 n, arr )
sum += n;
return sum;
}
LuhnAlgorithm::LuhnAlgorithm( const QString & n )
: sum(0), valid( false )
{
setNumberCard( n );
}
LuhnAlgorithm &LuhnAlgorithm::execute()
{
digits = getSequence( numberCard );
sum = getSumSequence( digits );
valid = sum % 10 == 0; // Если сумма кратна 10, тогда номер правильный
return *this;
}
LuhnAlgorithm & LuhnAlgorithm::execute( const QString & number )
{
setNumberCard( number );
return execute();
}
int LuhnAlgorithm::getSum() const
{
return sum;
}
bool LuhnAlgorithm::isValid() const
{
return valid;
}
void LuhnAlgorithm::setNumberCard(const QString & number )
{
numberCard = number;
numberCard = numberCard.remove( ' ' );
}
QVector < qint16 > LuhnAlgorithm::getSequence()
{
return digits;
}
QVector < qint16 > LuhnAlgorithm::getSequence( const QString & numberCard )
{
// Алгоритм Луна
// http://ru.wikipedia.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%9B%D1%83%D0%BD%D0%B0
//
QVector < qint16 > array;
const qint32 length = numberCard.length();
for( qint32 i = 0; i < length; i++ )
{
qint16 digit = numberCard.at(i).digitValue();
if ( i % 2 == 0 )
{
digit *= 2;
digit = (digit > 9 ? digit - 9 : digit);
}
array << digit;
}
return array;
}
Использование:
QString num = "4561 2612 1234 5467";
LuhnAlgorithm & alg = LuhnAlgorithm( num ).execute();
qDebug() << num << "sum =" << alg.getSum() << "valid =" << alg.isValid();
num = "4561 2612 1234 5464";
alg = LuhnAlgorithm().execute( num );
qDebug() << num << "sum =" << alg.getSum() << "valid =" << alg.isValid();
qDebug() << "sequence =" << LuhnAlgorithm( "4561 2612 1234 5467" ).execute().getSequence();
"4561 2612 1234 5467" sum = 60 valid = true
"4561 2612 1234 5464" sum = 57 valid = false
sequence = QVector(8, 5, 3, 1, 4, 6, 2, 2, 2, 2, 6, 4, 1, 4, 3, 7)
Исходники с гуи