Sie sind auf Seite 1von 3

ALGORITMOS Y ESTRUCTURAS DE DATOS

LABORATORIO N 01
MTODOS PARA ELEMENTOS DE UN VECTOR LINEAL
OPERACIONES A IMPLEMENTAR
a) Creacin y carga de datos del vector dinmico.
b) Clculo del nmero de elementos con dgitos pares; de un vector dinmico.
c) Clculo de la suma de elementos que tienen un nmero impar de dgitos de un
vector dinmico.
PROGRAMA CON MTODOS
// OperarVectores-1.cpp: define el punto de entrada de la aplicacin de consola.
//
#include "stdafx.h"
#include "iostream" // Librera para operaciones de entrada/salida por teclado
#include "conio.h"
// Librera para sensibilizar el teclado
using namespace std;
class OperaVectores
{
public:
int *vec;
// definicin de la variable puntero para el vector
int t;
// objeto para definir el tamao del vector
public:
int tamanho();
// Mtodo para establecefr el tamao del vector
int menu();
// Mtodo para mostrar el men de opciones
void CargarDatos(int[], int);
// Mtodo para Ingresar los datos al
vector
bool VerDigitoPar(int);
// Mtodo para ver si hay
dgitos pares en un elemento
int CuentaElementosDigitoPar(int[], int); // Contar el n elementos
ISO del vector
void MuestraElementos(int[], int); // Mtodo para imprimir el vector
bool VerElementoImparDigitos(int); // Mtodo para verificar si un
nmero tiene dgitos ISO
int SumarElementosImparDigitos(int[], int);// Mtodo para la suma de
elementos Primos
};
int OperaVectores::tamanho()
{
int n;
cout<<"Ingresa el tamanho del vector .."; cin>>n;
if(n>0)
return(n);
else
return(tamanho());
}
bool OperaVectores::VerDigitoPar(int n)
{
bool Salir=false;

MG. LUIS BOY CHAVIL

Pgina 1

ALGORITMOS Y ESTRUCTURAS DE DATOS


while(!Salir&&n>0)
{
if((n%10)%2==0)
Salir=true;
else
n/=10;
}
if (Salir==true)
return(true);
else
return(false);
}
void OperaVectores::CargarDatos(int v[], int z)
{
for(int k=0; k<z; k++)
{
cout<<"Ingresar dato .."<<k<<" : "; cin>>v[k];
}
}
void OperaVectores::MuestraElementos(int v[], int z)
{
cout<<"Elementos del vector"<<endl;
for(int k=0; k<z; k++)
{
cout<<v[k]<<" - ";
}
}
int OperaVectores::CuentaElementosDigitoPar(int z[], int tope)
{
int cuenta=0;
for(int k=0; k<tope; k++)
cuenta+=((VerDigitoPar(z[k]))?1:0);
return(cuenta);
}
bool OperaVectores::VerElementoImparDigitos(int n)
{
int cuenta=0;
while(n>0)
{
cuenta++;
n/=10;
}
if(cuenta%2==0)
return(false);
else
return(true);
}
int OperaVectores::SumarElementosImparDigitos(int z[], int tope)
{
int suma=0;
for(int k=0; k<tope; k++)
suma+=((VerElementoImparDigitos(z[k]))? z[k]: 0);
return(suma);

MG. LUIS BOY CHAVIL

Pgina 2

ALGORITMOS Y ESTRUCTURAS DE DATOS


}
int OperaVectores::menu()
{
int opcion;
int ingreso=0;
while (ingreso==0)
{
system("cls");
cout<<"1. Ingresa datos al vector"<<endl;
cout<<"2. Numero de elementos con por lo menos un digito PAR en el
vector"<<endl;
cout<<"3. Sumar elementos con un numero IMPAR de digitos en el
vector"<<endl;
cout<<"0. Ingresa una opcion"<<endl;
cin>>opcion;
if(opcion<0||opcion>3)
ingreso=0;
else
ingreso=1;
}
return(opcion);
}
int _tmain(int argc, _TCHAR* argv[])
{
OperaVectores OP;
bool salir=false;
OP.t=OP.tamanho();
while(!salir)
{
int opcion=OP.menu();
switch(opcion)
{
case 1:
{
OP.vec=new int[OP.t];
OP.CargarDatos(OP.vec, OP.t);
break;
}
case 2:
{
OP.MuestraElementos(OP.vec, OP.t);
cout<<"Cuenta de elementos con digito PAR:
"<<OP.CuentaElementosDigitoPar(OP.vec, OP.t)<<endl;
char zz=getch();
break;
}
case 3:
{
OP.MuestraElementos(OP.vec, OP.t);
cout<<"Suma de elementos con IMPAR de dgitos:
"<<OP.SumarElementosImparDigitos(OP.vec, OP.t)<<endl;
char zz=getch();
break;
}
default:
{
salir=true;
break;
}
}
}
return 0;
}

MG. LUIS BOY CHAVIL

Pgina 3

Das könnte Ihnen auch gefallen