Documentos de Académico
Documentos de Profesional
Documentos de Cultura
I. Recursiones numéricas
1. Mover el digito mayor al final
2. Generar la sumatoria de una serie
II. Recursiones cadenas
1. Verificar que un carácter pertenezca a un conjunto
AnsiString cad=InputBox(“”””)
Char *x=cad.c_str();
Luego: cad tiene la cadena con los caracteres validos
String car=InputBox(“”);
char letra=car[1];
Luego puede usar strchr(x,letra)
2. Contar las vocales
3. Encontrar la palabra mas larga de un conjunto
III. Ejercicios
1. Factorial de un numero
{
Grid1->Visible=false;
int num=StrToInt(InputBox("Factorial","Introduzca Nro. a convertir","1"));
String prod=factorial(num);
MessageDlg(prod,mtConfirmation, TMsgDlgButtons() << mbOK,0);
}
int factorial(int nro){
//int res=1;
if(nro==0){
return 1;
}else{
//res= nro*factorial(nro-1);
return nro*factorial(nro-1);
}
//return res;
}
2. Contar dígitos
void __fastcall TForm1::ContarDig1Click(TObject *Sender)
{ int tot=0;
String cad=InputBox("CONTAR DIGITOS UN NUMERO","Introduzca el numero ","0");
ContarDig(StrToInt(cad),tot);
ShowMessage(tot);
}
void ContarDig(int nro, int&tot){
if (nro<10) {tot=0;}
else{
ContarDig(nro/10,tot);}
tot++;
}
3. Partir Pi
void __fastcall TForm1::Par1Click(TObject *Sender)
{
String cad=InputBox("PARTIR NUMERO","Introduzca el numero ","0");
unsigned int res=StrToInt(cad);
unsigned int num=PartirPi(res);
ShowMessage(num);
unsigned int PartirPi(unsigned int nro){
if (nro<10)
return nro ;
else{
unsigned int res=PartirPi(nro/10);
unsigned int dig=nro%10;
if (dig%2==0) {dig=res*10+dig;}
else{
int tot;
ContarDig(res,tot);
dig=dig*Potencia(10,tot)+res;
}
return dig;
}
}
4. Invertir una cadena
void __fastcall TForm1::InvMed1Click(TObject *Sender)
{
String cad=InputBox("INVERTIR CADENA","Introduzca cadena a Invertir","0");
String inv=InvCadMed(cad,1,cad.Length());
ShowMessage(inv);
}
String InvCadMed(String cad,int ini,int fin){
char x[1] ;
if(ini>=fin){
return cad;
}else{
x[1]=cad[fin] ;
cad[fin]=cad[ini];
cad[ini]=x[1];
return InvCadMed(cad,ini+1,fin-1);
}
}
5. Elimine un carácter de una cadena
void __fastcall TForm1::Elimina1Click(TObject *Sender)
{
String cad=InputBox("ELIMINAR UN CARACTER DE LA CADENA","Introduzca cadena ","0");
String car =InputBox("ELIMINAR UN CARACTER DE LA CADENA","Introduzca caracter a Eliminar ","0");
//String elim=EliCad(cad,car,1);
String elim=EliCad2(cad,car,1);
ShowMessage(elim);
}
String EliCad(String cad,String car,int pos){
String res;
if(pos>cad.Length()){
return cad;
}else{
if(cad[pos]==car[1]){
cad=cad.Delete(pos,1);
pos=pos-1;
}
//else res=cad;
//res=cad;
res=EliCad(cad,car,pos+1);
return res;
}
}