Recursividad

Debo hacer un programa en Pascal, utilizando recursividad, que lea un número telefónico y lo transforme en todas las palabras posibles( según las letras que se corresponden con los números del celular. Es recomendable utilizar números de tres o dos dígitos.

1 respuesta

Respuesta
1
Espero te sirva
{$M 65520,0,0}
PROGRAM CELULAR;
USES CRT, PRINTER;
CONST
MaxCol=7;
A1='ABC';
A2='DEF';
A3='GHI';
A4='JKL';
A5='MNO';
A6='PQRS';
A7='TUV';
A8='WXYZ';
Type
Reg=Record
Posi:Byte;
Cadena:String[4];
End;
Matriz=Array [1..MaxCol] of Reg;
Var
Arch:Text;
FUNCTION POTENCIA(Base:INTEGER; Expo:Integer):INTEGER;
Begin
IF Expo=0
THEN POTENCIA:=1
ELSE POTENCIA:=Base*POTENCIA(Base, Expo-1)
End;
Procedure Inicializar(Var m:Matriz);
Var
i:Byte;
j:Byte;
Begin
For i:=1 to MaxCol Do
Begin
m.Posi:=0;
m.Cadena:='-----';
End;
End;
Procedure ImprimirCombinatoria(m:Matriz);
Var
i:Byte;
Cad:String[5];
NoSale:Boolean;
Fin:Boolean;
Begin
Cad:='';
Fin:=True;
For i:=1 to MaxCol Do
If m.Posi <> 0
Then Begin
Cad:=Cad + m.Cadena[m.Posi];
Fin:=Fin and (m.Posi = Length(m.Cadena))
End;
If Fin
Then Begin
WriteLn(Cad);
WriteLn(Arch,Cad);
End
Else Begin
WriteLn(Cad);
WriteLn(Arch,Cad);
i:=1;
Nosale:=True;
While NoSale Do
Begin
If m.Posi < Length(m.Cadena)
Then Begin
m.Posi:=m.Posi + 1;
NoSale:=False;
End
Else Begin
m.Posi:=1;
i:=i+1;
End;
End;
ImprimirCombinatoria(m);
End;
End;
Var
m:Matriz;
Cad,Cad3:STRING[10];
Resp,Cad1:CHAR;
Num,I,N,A,B,C:INTEGER;
Numero:LONGINT;
Begin
Inicializar(m);
Repeat
ClrScr;
Write('INGRESE UN NUMERO (EL 0 Y EL 1 NO TIENE LETRAS ASOCIADAS): ');
ReadLn(Numero);
WriteLn;
WriteLn('LAS LETRAS CORRESPONDIENTES A ESE NUMERO SON: ');
Str(Numero,Cad3);
Cad3:=Copy(Cad3,1, 10);
A:=0;
B:=0;
For n:=1 TO Length(Cad3) Do
Begin
Case Cad3[n] Of
'0':Cad:='';
'1':Cad:='';
'2':Cad:=A1;
'3':Cad:=A2;
'4':Cad:=A3;
'5':Cad:=A4;
'6':Cad:=A5;
'7':Cad:=A6;
'8':Cad:=A7;
'9':Cad:=A8;
End;
For i:=1 TO Length(Cad) Do
Begin
Cad1:=Cad;
Write(Cad1);
End;
m[n].Posi:=1;
m[n].Cadena:=Cad;
IF Length(Cad)=3
Then A:=A+1
Else If Length(Cad)=4
Then B:=B+1;
WriteLn;
END;
WriteLn;
WriteLn;
WriteLn('SE TIENEN: ',A,' TECLAS DE TRES CARACTERES');
WriteLn('SE TIENEN: ',B,' TECLAS DE CUATRO CARACTERES');
WriteLn;
IF A+B <= 1
Then WriteLn('CON ESTE NUMERO NO SE NINGUNA PALABRA')
Else Begin
If a=0
Then a:=1
Else a:=Potencia(3,a);
If b=0
Then b:=1
Else b:=Potencia(4,b);
WriteLn('CON ESTE NUMERO SE PUEDEN FORMAR: ',A*B,' PALABRAS');
End;
Assign(arch,'c:\salida.txt');
ReWrite(Arch);
ImprimirCombinatoria(m);
Close(Arch);
WriteLn;
WriteLn;
Write('DESEA CONTINUAR (S/N): ');
ReadLn(RESP);
Until Resp in ['N','n'];
END.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas