20
Facultad: Ciencias de la computación y telecomunicación CARRERA: Ingeniería sistemas PROGRAMACIÓN I” NOMBRE : SILVIA CARVAJAL MENDEZ DOCENTE : LIC. EDWIN VARGAS YAPURA MATERIA : PROGRAMACIÓN I SIGLA : INF 120 GRUPO : # 14

LISTAS

Embed Size (px)

DESCRIPTION

programacion

Citation preview

  • Facultad: Ciencias de la computacin y

    telecomunicacin

    CARRERA: Ingeniera sistemas

    PROGRAMACIN I

    NOMBRE : SILVIA CARVAJAL MENDEZ

    DOCENTE : LIC. EDWIN VARGAS YAPURA MATERIA : PROGRAMACIN I SIGLA : INF 120 GRUPO : # 14

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    1

    Ejercicios de listas en java Y VISUAL STUDIO

    JAVA

    public class Lista {

    private int cantElem;

    private int max;

    private int elem[];

    public Lista() {

    this.max = 100;

    this.cantElem = 0;

    this.elem = new int[max];

    }

    public void insertar(int x) {

    this.elem[this.cantElem] = x;

    this.cantElem = this.cantElem + 1;

    }

    public String toString() {

    String s1 = "[";

    int i = 0;

    while (i < this.cantElem) {

    if (i == this.cantElem - 1) {

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    2

    s1 = s1 + this.elem[i];

    } else {

    s1 = s1 + this.elem[i] + " ";

    }

    i++;

    }

    s1 = s1 + "]";

    return s1;

    }

    public int mayor() {

    int may = this.elem[0], i = 0;

    while (i < this.cantElem) {

    if (this.elem[i] > may) {

    may = this.elem[i];

    }

    i = i + 1;

    }

    return may;

    }

    public int menor() {

    int men = this.elem[0], i = 0;

    while (i < this.cantElem) {

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    3

    if (this.elem[i] < men) {

    men = this.elem[i];

    }

    i = i + 1;

    }

    return men;

    }

    public int dif(int x, int y) {

    if (x > y) {

    return x - y;

    } else {

    return y - x;

    }

    }

    public int frecuencia(int x) {

    int i = 0;

    int f = 0;

    while (i < this.cantElem) {

    if (this.elem[i] == x) {

    f++;

    }

    i++;

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    4

    }

    return f;

    }

    public boolean seEncuentra(int x) {

    return this.frecuencia(x) > 0;

    }

    public boolean diferentes() {

    int i = 0;

    while (i < this.cantElem) {

    if (this.frecuencia(this.elem[i]) > 1) {

    return false;

    }

    i++;

    }

    return true;

    }

    public boolean iguales() {

    return this.frecuencia(this.elem[0]) == this.cantElem - 1;

    }

    public int indexOf(int x) {

    int i = 0;

    while (i < this.cantElem) {

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    5

    if (this.elem[i] == x) {

    return i;

    }

    i++;

    }

    return -1;

    }

    public int indexOf(int x, int i) {

    while (i < this.cantElem) {

    if (this.elem[i] == x) {

    return i;

    }

    i++;

    }

    return -1;

    }

    public int suma() {

    int i = 0;

    int sum = 0;

    while (i < this.cantElem) {

    sum = sum + this.elem[i];

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    6

    i++;

    }

    return sum;

    }

    public boolean ascendente() {

    int i = 0;

    while (i < this.cantElem - 1) {

    if (this.elem[i] > this.cantElem + 1) {

    return false;

    }

    i++;

    }

    return true;

    }

    public boolean descendente() {

    int i = 0;

    while (i < this.cantElem - 1) {

    if (this.elem[i] < this.cantElem + 1) {

    return false;

    }

    i++;

    }

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    7

    return true;

    }

    public boolean ordenado() {

    return this.ascendente() || this.descendente();

    }

    public int promedio() {

    return this.suma() / this.cantElem;

    }

    public boolean par(int x) {

    return x % 2 == 0;

    }

    public boolean impar(int x) {

    return !par(x);

    }

    public boolean existePar() {

    int i = 0;

    while (i < this.cantElem) {

    if (par(this.elem[i])) {

    return true;

    }

    i++;

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    8

    }

    return false;

    }

    public boolean existeImpar() {

    int i = 0;

    while (i < this.cantElem) {

    if (impar(this.elem[i])) {

    return true;

    }

    i++;

    }

    return false;

    }

    public boolean todoPares() {

    int i = 0;

    while (i < this.cantElem) {

    if (!par(this.elem[i])) {

    return false;

    }

    i++;

    }

    return true;

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    9

    }

    public boolean todoImpares() {

    int i = 0;

    while (i < this.cantElem) {

    if (!impar(this.elem[i])) {

    return false;

    }

    i++;

    }

    return true;

    }

    public boolean existeParImpar() {

    return this.existePar() && this.existeImpar();

    }

    public boolean primo(int x) {

    int i = 1;

    int sum = 0;

    while (i

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    10

    }

    return sum == 2;

    }

    public boolean alterno() {

    int i = 0;

    while (i < this.cantElem - 1) {

    if (!((par(this.elem[i]) && impar(this.elem[i + 1])) || (par(this.elem[i + 1]) && impar(this.elem[i])))) {

    return false;

    }

    i++;

    }

    return true;

    }

    public boolean existePrimo() {

    int i = 0;

    while (i < this.cantElem) {

    if (primo(this.elem[i])) {

    return true;

    }

    i++;

    }

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    11

    return false;

    }

    public boolean todosPrimo() {

    int i = 0;

    while (i < this.cantElem) {

    if (!primo(this.elem[i])) {

    return false;

    }

    i++;

    }

    return true;

    }

    public int aleatoria (int a, int b)

    {

    return a + (int)((b-a) * Math.random());

    }

    public void generar_random (int a, int b, int n ){

    int i=1;

    while (i

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    12

    }

    }

    }

    public class Practico{

    public static boolean primo(int x) {

    int i = 1;

    int sum = 0;

    while (i

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    13

    Lista L1 = new Lista();

    L1.insertar(18);

    L1.insertar(19);

    L1.insertar(22);

    L1.insertar(14);

    System.out.println(L1.menor());

    System.out.println(L1);

    }

    }

    VISUAL

    Module Module1 Private Property L1 As Lista1 Sub Main() L1 = New Lista1 L1.insertarElemento(1) L1.insertarElemento(2) L1.insertarElemento(30) L1.insertarElemento(4) L1.insertarElemento(19) L1.insertarElemento(1) L1.insertarElemento(20) Console.WriteLine(L1.Descargar()) Console.WriteLine("menor diferencia de x") Console.WriteLine(L1.menor_dif(20)) Console.WriteLine("mayor diferencia de x") Console.WriteLine(L1.mayor_dif(4)) Console.ReadLine()

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    14

    End Sub Public Class Lista Private max As Integer Private cantElem As Integer Private elem() As Integer Public Sub New(ByVal max As Integer) Me.max = max Me.cantElem = 0 ReDim Me.elem(max) End Sub Public Sub New() Me.max = 20 Me.cantElem = 0 ReDim Me.elem(20) End Sub Public Overrides Function ToString() As String Dim s As String = "[" Dim i As Integer = 0 While i < Me.cantElem If i = Me.cantElem - 1 Then s = s & Me.elem(i) Else s = s & Me.elem(i) & ", " End If i = i + 1 End While s = s & "]" Return s End Function Public Sub insertarIesimo(ByVal x As Integer, ByVal i As Integer) Dim k As Integer = Me.cantElem - 1 While k >= i Me.elem(k + 1) = Me.elem(k) k = k - 1 End While Me.elem(i) = x Me.cantElem = Me.cantElem + 1 End Sub Public Sub insertarPrim(ByVal x As Integer) Me.insertarIesimo(x, 0) End Sub Public Sub insertarUlt(ByVal x As Integer) Me.insertarIesimo(x, Me.cantElem) End Sub Public Sub eliminarIesima(ByVal i As Integer) Dim k As Integer = i + 1 While k < Me.cantElem Me.elem(k - 1) = Me.elem(k) k = k + 1 End While Me.cantElem = Me.cantElem + 1 End Sub Public Sub eliminarPrim() Me.eliminarIesima(0) End Sub

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    15

    Public Sub eliminarUlt() Me.eliminarIesima(Me.cantElem - 1) End Sub Public Function mayorElem() As Integer Dim i = 0 Dim m = Me.elem(i) While i < Me.cantElem If Me.elem(i) > m Then m = Me.elem(i) End If i = i + 1 End While Return m End Function Public Function menorElem() As Integer Dim i = 0 Dim m = Me.elem(i) While i < Me.cantElem If Me.elem(i) < m Then m = Me.elem(i) End If i = i + 1 End While Return m End Function Public Function iguales() As Boolean Dim i = 0 While i < Me.cantElem - 1 If Me.elem(i) Me.elem(i + 1) Then Return False End If i = i + 1 End While Return True End Function Public Function diferentes() As Boolean Dim i = 0 While i < Me.cantElem If Me.frecuencia(Me.elem(i) > 1) Then Return False End If i = i + 1 End While Return True End Function Public Function frecuencia(ByVal x As Integer) As Integer Dim i = 0 Dim c = 0 While i < Me.cantElem If Me.elem(i) = x Then c = c + 1 End If i = i + 1 End While Return c End Function Public Function ordenado() As Boolean Return Me.asc() Or Me.des

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    16

    End Function Public Function asc() As Boolean Dim i = 0 While i < Me.cantElem - 1 If Me.elem(i) > Me.elem(i + 1) Then Return False End If i = i + 1 End While Return True End Function Public Function des() As Boolean Dim i = 0 While i < Me.cantElem - 1 If Me.elem(i) < Me.elem(i + 1) Then Return False End If i = i + 1 End While Return True End Function Public Sub insertarLugarAsc(ByVal x As Integer) Dim i As Integer = Me.cantElem - 1 While i >= 0 AndAlso x < Me.elem(i) Me.elem(i + 1) = Me.elem(i) i = i - 1 End While Me.elem(i + 1) = x Me.cantElem = Me.cantElem + 1 End Sub Public Sub insertarLugarDes(ByVal x As Integer) Dim i = Me.cantElem - 1 While i >= 0 AndAlso x > Me.elem(i) Me.elem(i + 1) = Me.elem(i) i = i - 1 End While Me.elem(i + 1) = x Me.cantElem = Me.cantElem + 1 End Sub Public Sub cargarAsc(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer) Dim i = 1 While i

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    17

    End While End Sub Public Sub cargarDif(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer) Dim i = 0 Dim j = 0 While i < n j = aleatorio(a, b) If Me.frecuencia(j) = 0 Then Me.cantElem = Me.cantElem + 1 Me.elem(i) = j i = i + 1 End If End While End Sub Public Sub cargarIguales(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer) Dim i = 0 Dim j = 0 Dim k = aleatorio(a, b) While i < n j = aleatorio(a, b) If j = k Then Me.cantElem = Me.cantElem + 1 Me.elem(i) = j i = i + 1 End If End While End Sub Public Function aleatorio(ByVal a As Integer, ByVal b As Integer) As Integer Return a + Rnd() * b End Function Public Function indexOf(ByVal x As Integer, ByVal i As Integer) As Integer While i < Me.cantElem If Me.elem(i) = x Then Return i End If i = i + 1 End While Return -1 End Function Public Function indexOf(ByVal x As Integer) Return Me.indexOf(x, 0) End Function Public Function lastIndexOf(ByVal x As Integer, ByVal i As Integer) As Integer Dim k = Me.cantElem - 1 While k >= i If Me.elem(k) = x Then Return k End If k = k - 1 End While Return -1 End Function Public Sub reemplazar(ByVal x As Integer, ByVal y As Integer) Dim i = 0 While i < Me.cantElem If Me.indexOf(x) > 0 Then Me.elem(i + 1) = y End If

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    18

    i = i + 1 End While End Sub Public Function seEncuentra(ByVal x As Integer) As Boolean Return Me.indexOf(x) >= 0 End Function Public Function exiteFrec(ByVal n As Integer) As Boolean Return Me.frecuencia(n) = n End Function Public Function par(ByVal i As Integer) As Boolean Return Me.elem(i) Mod 2 = 0 End Function Public Function impar(ByVal i As Integer) As Boolean Return Not Me.par(i) End Function Public Function pares() As Boolean Dim i = 0 While i < Me.cantElem If Me.impar(i) Then Return False End If i = i + 1 End While Return True End Function Public Function cantidadPar() As Integer Dim i = 0 Dim c = 0 While i < Me.cantElem If (Me.par(i)) Then c = c + 1 End If i = i + 1 End While Return c End Function Public Function parImpar() As Boolean Dim i = 0 While i < Me.cantElem If (Me.par(i)) AndAlso (Me.impar(i + 1)) Or (Me.impar(i)) AndAlso (Me.par(i + 1)) Then Return True End If i = i + 1 End While Return False End Function End Class End Module

  • Silvia Carvajal Mendez Santa Cruz Noviembre 2013

    19