Advertisement
AntonioVillanueva

Verificando tarjetas de credito

Mar 30th, 2020
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. //Test tarjetas de credito Antonio Villanueva
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. #define DIGITO ((tarjeta[index])-'0')
  10.  
  11. //**********************************************************************
  12. //De derecha a izquierda ,cada posicion par se multiplica por 2 , si el resultado es mayor que 9 restamos 9
  13. bool test(string &tarjeta){
  14.     int n(0);
  15.    
  16.     for (int index= (tarjeta.size()-1); (index>-1);--index){
  17.         if (index%2==0){// Suma indices pares , multiplicados *2 y si es mayor de 9 se resta 9     
  18.             n+=DIGITO*2 >9 ? abs (DIGITO*2-9) :DIGITO*2;
  19.         }else{//indice impar
  20.             n+=DIGITO;
  21.         }
  22.     }
  23.     return ( n%10 ? false:true);//Si el resultado es divisible por 10 es correcto
  24. }
  25.  
  26. //**********************************************************************
  27. string tarjetaOk(string &tarjeta){
  28.     //Elimina espacios en blanco
  29.     tarjeta.erase(remove(tarjeta.begin(),tarjeta.end(),' '),tarjeta.end());
  30.    
  31.     if (tarjeta.size()!=16){return "NO";}// test 16 digitos
  32.  
  33.     return test(tarjeta) ? "YES":"NO";
  34. }
  35.  
  36. //**********************************************************************
  37. int main()
  38. {
  39.     string card;//Tarjeta de credito
  40.     int n;//NĀ° de tarjetas a verificar
  41.     cin >> n; cin.ignore();
  42.    
  43.     for (int i = 0; i < n; i++) {//Cadena de caracteres tarjetas        
  44.         getline(cin, card);
  45.         cout <<tarjetaOk(card)<<endl;//"YES or NO"        
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement