Upload
jhordano12
View
206
Download
0
Embed Size (px)
Citation preview
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011
CLASE VECTORES using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace vectores
{
class Jvector
{
Clase_natural n = new Clase_natural();
public int c = 1;
int[] vector = new int[100];
int dim;
public int pdim
{
get { return dim; }
set { dim = value; }
}
public void cargar_vector(int pos, int x)
{
vector[pos] = x;
}
public int mostrarvector(int pos)
{
return vector[pos];
}
public int sumar_elemento()
{
int c = 1;
int suma = 0;
while (c <= dim)
{
suma = suma + vector[c];
c++;
}
return suma;
}
public int resta_elemento()
{
int c = 1;
int suma = 0;
while (c <= dim)
{
suma = suma + vector[c];
c++;
}
return suma;
}
public int contar_pares()
{
int c = 1;
int par = 0;
while (c <= dim)
{
if ((vector[c] % 2) == 0)
{
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 par = par + 1;
}
c++;
}
return par;
}
public int contar_impares()
{
int c = 1;
int impar = 0;
while (c <= dim)
{
if ((vector[c] % 2) != 0)
{
impar = impar + 1;
}
c++;
}
return impar;
}
public float promedio()
{
return sumar_elemento() / dim;
}
public void ordenar()
{
int c = 1;
int x;
int aux = 0;
while (c < dim)
{
x = c + 1;
while (x <= dim)
{
if (vector[x] < vector[c])
{
aux = vector[x];
vector[x] = vector[c];
vector[c] = aux;
}
x++;
}
c++;
}
}
public void adjuntar(Jvector par, int p, Jvector impar, int i)
{
int c = 1;
while (c <= p)
{
cargar_vector(c, par.mostrarvector(c));
c++;
}
while (c <= i)
{
cargar_vector(c, impar.mostrarvector(c));
c++;
}
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 }
public void ordenar_par_impar()
{
Jvector par = new Jvector();
Jvector impar = new Jvector();
ordenar();
int c = 1;
while (c <= dim)
{
int p = 1;
int i = 1;
if ((vector[c] % 2) == 0)
{
par.cargar_vector(p, vector[c]);
p++;
par.dim = p;
}
else
{
impar.cargar_vector(i, vector[c]);
i++;
impar.dim = i;
}
c++;
}
adjuntar(par, par.dim, impar, impar.dim);
}
public int contar_rep(int dato)
{
int c = 1;
int rep = 0;
while (c <= dim)
{
if (vector[c] == dato)
{
rep++;
}
c++;
}
return rep;
}
public int encontrar_el_elemen_repetido()
{
int c = 1;
int rep = vector[1];
while (c <= dim)
{
if (contar_rep(vector[c]) > contar_rep(rep))
{
rep = vector[c];
}
c++;
}
return rep;
}
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 public Jvector elimiar_elemento_X(int x)
{
int c = 1;
Jvector aux = new Jvector();
int c2 = 1;
while (c <= dim)
{
if (vector[c] != x)
{
aux.cargar_vector(c2, vector[c]);
aux.pdim = c2;
c2++;
}
c++;
}
return aux;
}
public Jvector elimiar_el_elemento_mas_repetido()
{
int c = 1;
Jvector Aux = new Jvector();
int m = encontrar_el_elemen_repetido();
int y = 1;
while (c <= dim)
{
if (vector[c] != m)
{
Aux.cargar_vector(y, vector[c]);
Aux.pdim = y;
y++;
}
c++;
}
return Aux;
}
public int Encontrar_el_promedio_de_los_numero_capicua()
//para hacer esto se deve crear una instancia "n" de la clase natural
// Y TAMBIEN NECESITAMOS TODA NUESTRA CLASE NATURAL
{
int c = 1;
int suma = 0;
int CC = 0;
while (c <= dim)
{
n.pnumero = vector[c];
if (n.NumeroCapicua())
{
suma = suma + vector[c];
CC++;
}
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 c++;
}
return (suma / CC);
}
public bool encotrar_si_los_numeros_del_vector_son_multiplos_de_x(int z)
{
int c = 1;
bool sw = true;
while (c <= dim)
{
n.pnumero = vector[c];
if (n.MultiplosDeDigitos(z) && (sw = true))
{
sw = true;
}
else
{
sw = false;
}
c++;
}
return sw;
}
public int contar_elementos_multiplos_de_X(int x)
{
int c = 1;
int cm = 0;
while (c <= dim)
{
if ((vector[c] % x) == 0)
{
cm++;
}
c++;
}
return cm;
}
public int Buscar_mayor_multiplo_de_X(int z)
{
int c = 1;
int MayorM = 0;
while (c <= dim)
{
if ((vector[c] % z) == 0)
{
if (vector[c] > MayorM)
{
MayorM = vector[c];
}
}
c++;
}
return MayorM;
}
}
}
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011
NOTA: SI ESTAN TRABAJANDO EN UN NUEVO FORMULARIO
NECESITAN TAMBIEN LA CLASE NATURAL ASI QUE TAMBIEN
ESTA AQUI
CLASE NATURAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace vectores
{
class Clase_natural
{
int numero;
public int pnumero
{
get { return numero; }
set { numero = value; }
}
public bool EsPar()
{
return ((numero % 2) == 0);
}
public int potencia(int b)
{
int pot = 1;
while (b > 0)
{
pot = pot * numero;
b = b - 1;
}
return pot;
}
public int mayor(int numero2)
{
if (numero > numero2)
{
return numero;
}
else
{
return numero2;
}
}
public int DigitoMayor()
{
int r1, r2;
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 r1 = (numero % 10);
numero = numero / 10;
while (numero > 0)
{
r2 = (numero % 10);
numero = numero / 10;
if (r2 > r1)
{
r1 = r2;
}
}
return r1;
}
public int SumarDigitos()
{
int s, a;
s = 0;
while (numero > 0)
{
a = (numero % 10);
numero = (numero / 10);
s = s + a;
}
return s;
}
public string Primo()
{
int c = 1;
int cd = 0;
while (c <= numero)
{
if ((numero % c) == 0)
{
cd++;
}
c++;
}
if (cd > 2)
{
return ("No Es Primo");
}
else
{
return ("Es Primo");
}
}
public int Invertir()
{
int aux = 0;
while (numero > 0)
{
aux = (aux * 10) + numero % 10;
numero = numero / 10;
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 }
return aux;
}
#region fecha 6 de abril 2011
public int ParImpar()
{
int par, impar;
par = 0;
impar = 0;
while (numero > 0)
{
if (((numero % 10) % 2) == 0)
{
par = (par * 10) + numero % 10;
numero = numero / 10;
}
else
{
impar = (impar * 10) + numero % 10;
numero = numero / 10;
}
}
while (impar > 0)
{
par = (par * 10) + (impar % 10);
impar = impar / 10;
}
numero = par;
return numero;
}
public int EliminarPares()
{
int dig;
int impares = 0;
while (numero > 0)
{
dig = numero % 10;
numero = numero / 10;
if ((dig % 2) == 1)
{
impares = (impares * 10) + dig;
}
}
numero = impares;
return Invertir();
}
public int ContarRepeticiones(int d)
{
int dig;
int cr = 0;
while (numero > 0)
{
dig = numero % 10;
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 numero = numero / 10;
if (dig == d)
{
cr++;
}
}
return cr;
}
public bool NumeroCapicua()
{
if (numero == Invertir())
{
return true;
}
else
{
return false;
}
}
public bool MultiplosDeDigitos(int d1)
{
bool sw = true;
int dig;
while (numero > 0)
{
dig = numero % 10;
numero = numero / 10;
if ((dig % d1) == 0 && (sw == true))
{
sw = true;
}
else
{
sw = false;
}
}
return sw;
}
#endregion
#region 27 de abril 2011
public int el_digito_mayor_menor_a_x()
{
int s = 7;
int a, mayor;
mayor = 0;
while (numero > 0)
{
a = numero % 10;
numero = numero / 10;
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 if (a > mayor)
{
if (a < s)
{
mayor = a;
}
}
}
numero = mayor;
return numero;
}
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011
BOTONES
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace vectores
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Jvector vec = new Jvector();
public int c = 1;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{ //boton para dar la dimension
vec.pdim = int.Parse(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{ // boton para cargar
if (c <= vec.pdim)
{
vec.cargar_vector(c, int.Parse(textBox2.Text));
c++;
textBox3.Text = textBox3.Text + textBox2.Text + ',';
}
}
private void button3_Click(object sender, EventArgs e)
{ // suma todos los elementos del vector
MessageBox.Show(vec.sumar_elemento().ToString());
}
private void button4_Click(object sender, EventArgs e)
{ // saca el promedio de todos los numeros del vector
MessageBox.Show(vec.promedio().ToString());
}
private void button5_Click(object sender, EventArgs e)
{ //elimina el elemto que deaseamos X
int s = Convert.ToInt32(textBox4.Text);
Jvector v = new Jvector();
v = vec.elimiar_elemento_X(s);
int x = 1;
while (x <= v.pdim)
{
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011 textBox5.Text = textBox5.Text + ',' +
Convert.ToString(v.mostrarvector(x));
x++;
}
}
private void button6_Click(object sender, EventArgs e)
{
Jvector v = new Jvector();
v = vec.elimiar_el_elemento_mas_repetido();
int x = 1;
while (x <= v.pdim)
{
textBox6.Text = textBox6.Text + ',' +
Convert.ToString(v.mostrarvector(x));
x++;
}
}
private void button7_Click(object sender, EventArgs e)
{
MessageBox.Show(vec.Encontrar_el_promedio_de_los_numero_capicua().ToString());
}
private void button8_Click(object sender, EventArgs e)
{
int s = Convert.ToInt32(textBox4.Text);
bool d = vec.encotrar_si_los_numeros_del_vector_son_multiplos_de_x(s);
if (d == true)
{
MessageBox.Show("El vector es Multiplo de " + s);
}
else
{
MessageBox.Show("El vector No es Multiplo de " + s);
}
}
private void button9_Click(object sender, EventArgs e)
{
MessageBox.Show(vec.encontrar_el_elemen_repetido().ToString());
}
private void button10_Click(object sender, EventArgs e)
{
int w = Convert.ToInt32(textBox4.Text);
MessageBox.Show(vec.contar_elementos_multiplos_de_X(w).ToString());
}
private void button11_Click(object sender, EventArgs e)
{
int s = Convert.ToInt32(textBox4.Text);
MessageBox.Show(vec.Buscar_mayor_multiplo_de_X(s).ToString());
}
}
}
UDABOL – LABORARIO II – ING. DE SISTEMAS 2011
FORMULARIO