miércoles, 26 de octubre de 2011

Este programa es de archivos

class Program{//METODO PARA MANEJAR ARCHIVOS
{
public static void Escribirarchivo(string nomarch)//SE INICIALIZAN A NULL
FileStream fs = null;BinaryWriter bw = null;try{//LA TRANSMISION DE DATOS ES DE ESCRITURAfs =
new FileStream(nomarch, FileMode.Create, FileAccess.Write);//FS ES DONDE SE ENCUENTRA EL ARCHIVO SI ES DE UNA USB O DE EL DISCO DURObw =


new BinaryWriter(fs);Random r = new Random();int n = 5, i = 0;int val;do{
val = r.Next(10, 100);

bw.Write(val);
i++;
}

}

{

}
Console.WriteLine("Dato insertado en el archivo: ");while (i < n);catch (IOException ex)Console.WriteLine("ERROR " + ex.Message);finally{

{
fs.Close();
bw.Close();
}
}
}

{
if (bw != null)public static void LeerArchivo(string nomarch)//alternativa sin crear el objeto filestream;//files fs=null;
BinaryReader br = null;try{

{
br =
if (File.Exists(nomarch))new BinaryReader(new FileStream(nomarch, FileMode.Open, FileAccess.Read));int val;do{
val = br.ReadInt32();

}
}
Console.WriteLine("Dato : " + val);while (true);else{

}
}

{

}
Console.WriteLine("El archivo no existe");catch (EndOfStreamException)Console.WriteLine("Fin del archivo");finally{

{
br.Close();
}
}
}

{
if (br != null)public static void Agregar_a_archivo(string nomarch)//SE INICIALIZAN A NULL
FileStream fs = null;BinaryWriter bw = null;try{//LA TRANSMISION DE DATOS ES DE ESCRITURAfs = new FileStream(nomarch, FileMode.Append, FileAccess.Write);//FS ES DONDE SE ENCUENTRA EL ARCHIVO SI ES DE UNA USB O DE EL DISCO DURObw =


new BinaryWriter(fs);Random r = new Random();int n = 5, i = 0;int val;do{
val = r.Next(10, 100);

bw.Write(val);
i++;
}

}

{

}
Console.WriteLine("Dato insertado en el archivo: "+ val);while (i < n);catch (IOException ex)Console.WriteLine("ERROR " + ex.Message);finally{

{
fs.Close();
bw.Close();
}
}
}

{










}

{



Escribirarchivo(archivo);

LeerArchivo(archivo);
Agregar_a_archivo(archivo);

LeerArchivo(archivo);



}
}
if (bw != null)public static int Menu()Console.Clear();Console.WriteLine("1. Nuevo");Console.WriteLine("2. Abrir");Console.WriteLine("3. Agregar");Console.WriteLine("4. Cerrar");Console.WriteLine("5. Salir");int menu = int.Parse(Console.ReadLine());if (menu < 0)Console.Write("No es valido");return menu;static void Main(string[] args)int m = Menu();string archivo = "E:\\Estructura\\leerarchivos\\archivitO.ar";Console.ReadKey();

domingo, 23 de octubre de 2011

calculo de promedios diagonal en consola

class Program
{
public int Menu()
{

Console.Clear();
Console.WriteLine("Matriz Cuadrada");
Console.WriteLine("1)Promedio Diagonal Principal");
Console.WriteLine("2)Promedio Diagonal Invertida");
Console.WriteLine("3)Salir..");
int resp = Convert.ToInt16(Console.ReadLine());
return resp;
}
static public double promPrincipal(int[,] x)
{
double suma = 0;
double Prom = 0;
double cont = 0;
for(int r = 0; r < x.GetLength(0); r++)
for (int c = 0; c < x.GetLength(1); c++)
{
if (r == c)
{
suma += x[r,c];
cont++;
}
}
Prom = suma / cont;
return Prom;
}
static public double promInversa(int[,] x, int n)
{
double suma = 0;
double Prom = 0;
double cont = 0;
for(int r = 0; r < x.GetLength(0); r++)
for (int c = 0; c < x.GetLength(1); c++)
{
if ((r+c) == (n-1))
{
suma += x[r,c];
cont++;
}
}
Prom = suma / cont;
return Prom;
}
static void Main(string[] args)
{
int r;
int c;
int n;
Program prog = new Program();
do
{
n = prog.Menu();
switch (n)
{
case 1:
{
do
{

Console.WriteLine("Numero de del Tamaño de la matiz cuadrada (Numero de 2 - 10) ");
r = c = Convert.ToInt16(Console.ReadLine());
if (r < 2 || r > 10)
{
Console.WriteLine("Solo numero entre 2 y 10");
Console.Clear();
}

}
while (r < 2 || r > 10);
int x = 10; int y = 10;

Random alt = new Random();
int[,] mat = new int [r,c];
int alto = y + mat.GetLength(0);
for(r =0; r < mat.GetLength(0); r++ )
for (c = 0; c < mat.GetLength(1); c++)
{
mat[r, c] = alt.Next(1,10);
Console.SetCursorPosition(x, y);
Console.Write(mat[r,c]);
y++;
if (y >= alto)
{
x += 5;
y = 10;
}

}

Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Promedio diagonal principal : ");
Console.WriteLine(promPrincipal(mat));
Console.WriteLine("Enter para ir a Menu.........");
Console.ReadKey();
}
break;
case 2:
{
do
{

Console.WriteLine("Numero de del Tamaño de la matiz cuadrada (Numero de 2 - 10) ");
r = c = Convert.ToInt16(Console.ReadLine());
if (r < 2 || r > 10)
{
Console.WriteLine("Solo numero entre 2 y 10");
Console.Clear();
}

}
while (r < 2 || r > 10);
int x = 10; int y = 10;

Random alt = new Random();
int[,] mat = new int[r, c];
int alto = y + mat.GetLength(0);
for (r = 0; r < mat.GetLength(0); r++)
for (c = 0; c < mat.GetLength(1); c++)
{
mat[r, c] = alt.Next(1, 10);
Console.SetCursorPosition(x, y);
Console.Write(mat[r, c]);
y++;
if (y >= alto)
{
x += 5;
y = 10;
}

}

Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Promedio diagonal inversa : ");
Console.WriteLine(promInversa(mat, c));
Console.WriteLine("Enter para ir a Menu.........");
Console.ReadKey();
}
break;
case 3: Console.WriteLine();
break;


}
}
while(n < 3);
}
}
}

lunes, 17 de octubre de 2011

aplicacion susando cadenas de caracteres

static void Main(string[] args)
{


string cad = "Septiembre 12 DEL 2011";
int may = 0, min = 0, vocal = 0;


for (int i = 0; i < cad.Length; i++)
{
if (cad[i] >= 65 && cad[i] <= 90)
{
may++;
}
else
if(cad[i] >= 97 && cad[i] <= 122)
{
min++;
}
else
if(cad[i] == 'a' || cad[i] == 'e' || cad[i] == 'i' || cad[i] == 'o' || cad[i]=='u')
{
vocal++;
}
}


Console.WriteLine("Hay " + may + " numero de mayusculas");
Console.WriteLine("Hay " + min + " numero de minusculas");
Console.WriteLine("Hay " + vocal + " numero de vocales");
Console.ReadKey();

martes, 11 de octubre de 2011

apicacion consola usando arreglos

static void Main(string[] args)
{
int num;
string a;
do
{
Console.Write("Tamaño del arreglo: ");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("--------------------------");

if (num >= 5 && num <= 10)
{
Random x = new Random();
int[] datos = new int[num];
for (int i = 0; i < datos.Length; i++)
{
datos[i] = x.Next(1, 20);
Console.WriteLine("DATO[" + (i + 1) + "]= " + datos[i]);

}
Console.WriteLine("--------------------------");
int may = 0;
for (int i = 0; i < datos.Length; i++)
{
if (datos[i] >= may)
may = datos[i];

}
Console.WriteLine("El num. mayor es = " + may);
int menor = datos[0];
for (int i = 1; i < datos.Length; i++)
{
if (datos[i] <= menor)
menor = datos[i];

}
Console.WriteLine("El num. menor es = " + menor);


}
else
{
Console.WriteLine("Numero no validoo = " + num);
Console.WriteLine("--------------------------");

}
Console.Write("Realizar de nuevo (s/n) =");
a = Console.ReadLine();
if (a == "s")
Console.Clear();
}
while (a == "s");
//Console.ReadKey();
}