lunes, 15 de abril de 2013

C# Demonstrate the fail-soft array


class FSDemo {
  static void Main() {
    FailSoftArray fs = new FailSoftArray(5, -1);
    int x;

    // Show quiet failures.
    Console.WriteLine("Fail quietly.");
    for(int i=0; i < (fs.Length * 2); i++)
      fs.Put(i, i*10);

    for(int i=0; i < (fs.Length * 2); i++) {
      x = fs.Get(i);
      if(x != -1) Console.Write(x + " ");
    }
    Console.WriteLine("");

    // Now, handle failures.
    Console.WriteLine("\nFail with error reports.");
    for(int i=0; i < (fs.Length * 2); i++)
      if(!fs.Put(i, i*10))
        Console.WriteLine("Index " + i + " out-of-bounds");

    for(int i=0; i < (fs.Length * 2); i++) {
      x = fs.Get(i);
      if(x != -1) Console.Write(x + " ");
      else
        Console.WriteLine("Index " + i + " out-of-bounds");
    }
  }
}

C# Public vs private access.


using System;

class MyClass {
  private int alpha; // private access explicitly specified
  int beta;          // private access by default
  public int gamma;  // public access

  /* Methods to access alpha and beta.  It is OK for a member
     of a class to access a private member of the same class. */

  public void SetAlpha(int a) {
    alpha = a;
  }

  public int GetAlpha() {
    return alpha;
  }

  public void SetBeta(int a) {
    beta = a;
  }

  public int GetBeta() {
    return beta;
  }
}
 
class AccessDemo {
  static void Main() {
    MyClass ob = new MyClass();
 
    // Access to alpha and beta is allowed only through methods.
    ob.SetAlpha(-99);
    ob.SetBeta(19);
    Console.WriteLine("ob.alpha is " + ob.GetAlpha());
    Console.WriteLine("ob.beta is " + ob.GetBeta());

    // You cannot access alpha or beta like this:
//  ob.alpha = 10; // Wrong! alpha is private!
//  ob.beta = 9;   // Wrong! beta is private!

    // It is OK to directly access gamma because it is public.
    ob.gamma = 99;
   }
}

Program C# Compute your weight on the moon.


using System;

class Moon {  
  static void Main() {  
    double earthweight; // weight on earth
    double moonweight;  // weight on moon  
 
    earthweight = 165.0;  
 
    moonweight = earthweight * 0.17;
 
    Console.WriteLine(earthweight + " earth-pounds is equivalent to " +
                      moonweight + " moon-pounds.");  
 
  }  
}

Numeros primos C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace numerosprimos
{
    class Program
    {
        static void Main(string[] args)
        {
            int numero = 2;
            for (numero = 2; numero < 100; numero++)
            {
                Console.WriteLine("Numeros primos del 1 al 100"+numero);
            }

            Console.ReadLine();

        }
    }
}

Tabla de la verdad C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TablaDeLaVerdad
{
    class Program
    {
        static void Main(string[] args)
        {

            //encuentra los numeros primos entre 2 y 100
            int i, j;
            bool esPrimo;

            for (i = 2; i < 100;i++ )
            {
                esPrimo = true;
                //prueba si un numero uniformemente es divisible

               for(j=2;j<=i/j;j++)
               
             
                if((i%j) == 0)
               
                esPrimo=false;
                if(esPrimo)

                    Console.WriteLine(i+" es primo");
               
                Console.ReadLine();
            }

        }
    }
}

Tablero Ajedrez C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TableroAjedres
{
    class Program
    {
        static void Main(string[] args)
        {
            int falfil, calfil; // posición inicial del alfil
            int fila, columna;  // posición actual del alfil

            Console.WriteLine("Posición del alfil:");
            Console.Write("  fila    ");
            falfil = Convert.ToInt32(Console.ReadLine());
            Console.Write("  columna ");
            calfil = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(); // dejar una línea en blanco

            // Pintar el tablero de ajedrez
            for (fila = 1; fila <= 8; fila++)
            {
                for (columna = 1; columna <= 8; columna++)
                {
                    if ((fila + columna == falfil + calfil) ||
                       (fila - columna == falfil - calfil))
                        Console.Write("* ");
                    else if ((fila + columna) % 2 == 0)
                        Console.Write("B ");
                    else
                        Console.Write("N ");
                }
                Console.WriteLine(); // cambiar de fila

            }
            Console.ReadLine();
        }
    }
}

Teorema Newton C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TeoremaDeNewton
{
    class Program
    {
        static void Main(string[] args)
        {
            double n; //numero
            double aprox; //aproximacion de la raiz cuadrada
            double antaprox; //anterior aproximacion a la raiz cuadrada
            double epsilon; //coeficiente de error

            do
            {
                Console.Write("Número: ");
                n = Convert.ToDouble(Console.Read());
            }

            while (n < 0);
            do
            {
                Console.WriteLine("Raiz cuadrada aproximada: ");
                aprox = Convert.ToDouble(Console.Read());
            }

            while (aprox <= 0);
            do
            {
                Console.WriteLine("Coeficiente de error: ");
                epsilon = Convert.ToDouble(Console.Read());


            }

            while (epsilon <= 0);
            do
            {
                antaprox = aprox;
                aprox = (n/antaprox + antaprox) / 2;
            }

            while (Math.Abs(aprox - antaprox) >= epsilon);
            Console.WriteLine("La raiz cuadrada de {0:F2} es {1:F2}", n, aprox);

       

        }

    }
   
}

Factorial recursivo C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecursionFact
{
    class Recursivo
    {
         static int Factorial(int n)
        {
            if (n == 0)
            {
                return (1);
            }

               
            else
                return (n * Factorial(n - 1));
        }



        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("Introduce un numero: ");
            n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("El facotrial de: "+n+" es "+Factorial(n));
            Console.ReadLine();
            //Console.WriteLine("el factorial es: "+Factorial(5));
       
        }
    }
}

Factorial Java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package factorial;
import java.util.Scanner;

public class Factorial {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
        Scanner leer=new Scanner(System.in);
       
        double numero;
        int i=0;
   
        double factorial=1;
       
        System.out.print("Introduce un numero: ");
        numero=leer.nextInt();
       
        for(i=1;i<numero+1;i++)
       
        {
            factorial=factorial*i;
     
        }
        System.out.println("El factorial es:"+factorial);
    }
}

Menus paralelos C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MENUPARALELOS
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] NOMBRE = new string[5];
            int[] CALIFICACION1 = new int[5];
            int[] CALIFICACION2 = new int[5];

            int j = 0, w = 0;
            string  b = "", nom = "", x;
            int i = 0, cali1, cali2;
            string Opcion = "";

            Random objeto = new Random();
            float numero = objeto.Next(5, 10);


            while (Opcion != "X" )
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.BackgroundColor = ConsoleColor.Cyan;
       


                Console.WriteLine();
                Console.WriteLine("****___MENU DE ARREGLOS PARALELOS____****:");
                Console.WriteLine();
                Console.WriteLine("A) !!-INSERTAR");
                Console.WriteLine("B) !!-MODIFICAR");
                Console.WriteLine("C) !!-ELIMINAR");
                Console.WriteLine("X) !!-SALIR");
                Console.Write("SELECCIONA UNA OPCION:");
                Opcion = Console.ReadLine();

                switch (Opcion)
                {
                    case "A":

                        do
                        {

                            if (j < 5)
                            {

                                Console.Write("Introduzca Un Nombre:");
                                NOMBRE[j] = Console.ReadLine();

                               // Console.Write("ingrese calificacion 1:");
                                //cal1[j] = int.Parse(Console.ReadLine());
                                CALIFICACION1[j] = objeto.Next(5, 10);
                                Console.WriteLine("CALIFICACION 1: " + CALIFICACION1[j]);
                                CALIFICACION2[j] = objeto.Next(5, 10);
                                Console.WriteLine("CALIFICACION 1: "+CALIFICACION2[j]);
                                //Console.Write("ingrese calificacion 2:");
                                //cal2[j] = int.Parse(Console.ReadLine());
                                Console.WriteLine();

                                j++;

                            }
                            else
                                Console.WriteLine("!!!!!!----------Los Arreglos Están Llenos  ^__^------!!!!!");


                            Console.WriteLine("Desea Ingresar Otro Dato (SI/NO)");
                            b = Console.ReadLine();
                        } while (b == "SI");

                        break;




                    case "B":

                        Console.Write("Alumno Que Desea Modificar: ");
                        x = Console.ReadLine();

                        for (i = 0; i < 5; i++)
                            if (NOMBRE[i] == x)
                            {
                                Console.WriteLine();

                                Console.WriteLine("-------!!!!!...NUEVOS DATOS!!!!!!! -------- ");

                                Console.WriteLine("Ingrese Nombre:");
                                nom = Console.ReadLine();
                                NOMBRE[i] = nom;

                                //Console.WriteLine("ingrese calificacion 1:");
                                CALIFICACION1[i] = objeto.Next(5, 10);
                           
                                //cali1 = int.Parse(Console.ReadLine());
                                //cal1[i] = cali1;

                                //Console.WriteLine("ingrese calificacion 2: ");
                                //cali2 = int.Parse(Console.ReadLine());
                                //al2[i] = cali2;
                                    CALIFICACION2[i] = objeto.Next(5, 10);

                                Console.WriteLine();


                                for (i = 0; i < 5; i++)
                                {
                                    Console.WriteLine();

                                    Console.WriteLine(NOMBRE[i]);
                                    Console.WriteLine(CALIFICACION1[i]);
                                    Console.WriteLine(CALIFICACION2[i]);
                                }

                            }

                            else
                                Console.WriteLine("El Alumno No Existe");
                        break;



                    case "C":
                        Console.WriteLine();

                        Console.WriteLine("Elemento Que Desea Borrar");
                        x = Console.ReadLine();

                        for (i = 0; i < j; i++)
                            if (NOMBRE[i] == x)
                            {

                                for (i = 0; i < 5 - 1; i++)
                                {
                                    NOMBRE[i] = NOMBRE[i + 1];
                                    CALIFICACION1[i] = CALIFICACION1[i + 1];
                                    CALIFICACION2[i] = CALIFICACION2[i + 1];
                                }
                                j = j - 1;
                            }

                        for (i = 0; i < 5; i++)
                        {
                            Console.WriteLine(NOMBRE[i]);
                            Console.WriteLine(CALIFICACION1[i]);
                            Console.WriteLine(CALIFICACION2[i]);
                        }
                        break;
                }
            }

        }


        }
    }