我曾经用C#编码,我现在在UOW学习C ++。 有这个任务,我应该验证信用卡是否有效通过模数10(LUHN公式) 但是,我没有设法得到讲师显示/要求的正确答案。 以下是我为这项任务所做的C ++工作。建议,干杯。
问题1 - 循环错误/无限 问题2 - 我得到的4个令牌(数字)的总和是不正确的我相信。 问题3 - 任何改进这种编码的建议(不要改变太多) - 只是为了提高效率。
#include <iostream>
using namespace std;
// Get Reverse of an int
int getReverse(int);
// Double 2nd and 4th digit
int newDigits (int);
// Sum Token up
int sumToken (int);
// Modulus 10 Verification
bool verifyToken (int);
int main()
{
int card,card1, card2, card3, card4;
int cardReverseD, cardReverseC, cardReverseB, cardReverseA;
int reverseA, reverseB, reverseC, reverseD;
int newNumber4, newNumber3, newNumber2, newNumber1;
int sumToken4, sumToken3, sumToken2, sumToken1;
int sumTokenFinal; // Used for data verification via modulus 10
char again; // For loop
cout << "Welcome to Marcus Moo Legal Licensed Money Lender Card Company" << endl;
cout << endl;
// Enter Credit Card Number
do
{
cout << "Enter credit card number: ";
// Fourth (4th) Card Number
cin >> card4;
cin.ignore(1,'-');
cardReverseD = getReverse(card4);
newNumber4 = newDigits(cardReverseD); // new int after double 2nd and 4th digit
sumToken4 = sumToken(newNumber4); // sum of token of sector 4
// Third(3rd) Card Number
cin >> card3;
cin.ignore(1,'-');
cardReverseC = getReverse(card3);
newNumber3 = newDigits(cardReverseC); // new int after double 2nd and 4th digit
sumToken3 = sumToken(newNumber3); // sum of token of sector 3
// Second(2nd) Card Number
cin >> card2;
cin.ignore(1,'-');
cardReverseB = getReverse(card2);
newNumber2 = newDigits(cardReverseB); // new int after double 2nd and 4th digit
sumToken2 = sumToken(newNumber2); // sum of token of sector 2
// First(1st) Card Number
cin >> card1;
cardReverseA = getReverse(card1);
newNumber1 = newDigits(cardReverseA); // new int after double 2nd and 4th digit
sumToken1 = sumToken(newNumber1); // sum of token of sector 1
// Addition of 1-4 tokens NEW & Verification & COUT
sumTokenFinal = sumToken1 + sumToken2 + sumToken3 + sumToken4;
cout << "The special sum is " << sumTokenFinal << endl;
if (verifyToken(sumTokenFinal)==true)
{
cout << card1 << "-" << card2 << "-" << card3 << "-" << card4
<< " is a valid credit card number" << endl;
}
else
{
cout << card1 << "-" << card2 << "-" << card3 << "-" << card4
<< " is not a valid credit card number" << endl;
}
// Let loop go again?
cout << endl;
cout << "Another Card (y/Y/n/N): ";
cin.get(again);
cout << endl;
cout << "------------------------------------------------" << endl;
} while ((again='y')||(again='Y')&&(again!='n')&&(again!='N'));
}
// Get Reverse of an int
int getReverse(int x)
{
int reverse=0;
int lastDigit;
while (x>0)
{
lastDigit = x%10;
reverse = (reverse*10)+lastDigit;
x = x/10;
}
return reverse;
}
// Double 2nd and 4th digit
int newDigits (int x)
{
int newNumber, length;
int secondDigit, fourthDigit;
secondDigit = x%1000/100*2;
fourthDigit = x%10*2; // Edited as per advised
if ((secondDigit<10)&&(fourthDigit<10))
{
newNumber = (x%10000/1000*1000)+(secondDigit*100)+(x%100/10*10)+(fourthDigit);
}
else if ((secondDigit>10)&&(fourthDigit<10))
{
newNumber = (x%10000/1000*10000)+(secondDigit*100)+(x%100/10*10)+(fourthDigit);
}
else if ((secondDigit<10)&&(fourthDigit>10))
{
newNumber = (x%10000/1000*10000)+(secondDigit*1000)+(x%100/10*100)+(fourthDigit);
}
else if ((secondDigit>10)&&(fourthDigit>10))
{
newNumber = (x%10000/1000*100000)+(secondDigit*1000)+(x%100/10*100)+(fourthDigit);
}
return newNumber;
}
// Sum Token up
int sumToken (int x)
{
int lastDigit;
int sumDigits=0;
while (x>0)
{
lastDigit = x % 10;
sumDigits += lastDigit;
x /= 10;
}
return sumDigits;
}
// Modulus 10 Verification
bool verifyToken (int x)
{
bool verify = false;
if (x%10==0)
{
verify = true;
}
return verify;
}