Lenguaje de programación Borland Delphi
Obtiene el tipo de tarjeta de crédito según el número introducido, si no es válido mostrará un aviso
Un listado del programa completo:
unit Unit1;
interface
uses
Windows, Controls, Forms, Dialogs, StdCtrls, Classes;
type
TForm1 = class(TForm)
Button1: TButton;
txtNumeroTarjeta: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
tipoTarjeta : array [0..4] of string = ('No válida', 'Amex', 'Visa', 'Mastercard', 'Discover');
implementation
{$R *.dfm}
function comprobarTarjeta(C: string) : Integer;
var
Card : string[21];
VCard : array [0..21] of Byte absolute Card;
XCard : Integer;
Cstr : string[21];
y,
x : Integer;
begin
Cstr := '';
FillChar(VCard, 22, #0);
Card := C;
for x := 1 to 20 do
if (VCard[x] in [48..57]) then
Cstr := Cstr+Chr(VCard[x]);
Card := '';
Card := Cstr;
XCard := 0;
if not odd(Length(Card)) then
for x := (Length(Card)-1) downto 1 do
begin
if odd(x) then
y := ((VCard[x]-48)*2)
else
y := (VCard[x]-48);
if (y>=10) then
y := ((y-10)+1);
XCard := (XCard+y)
end
else
for x := (Length(Card)-1) downto 1 do
begin
if odd(x) then
y := (VCard[x]-48)
else
y := ((VCard[x]-48)*2);
if (y>=10) then
y := ((y-10)+1);
XCard := (XCard+y)
end;
x := (10-(XCard mod 10));
if (x=10) then
x := 0;
if (x=(VCard[Length(Card)]-48)) then
comprobarTarjeta := Ord(Cstr[1])-Ord('2')
else
comprobarTarjeta := 0
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(tipoTarjeta[comprobarTarjeta(txtNumeroTarjeta.Text)]);
end;
NOTA: la función devolverá el tipo de tarjeta cuando el número introducido en el cuadro de texto sea correcto. Cuando el número no sea correcto mostrará: "No válida".
Publicado el: 2003-09-19