Wednesday, December 10, 2008

EJEMPLOS DE FICHEROS Y FLUJOS

EJEMPLO 1

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public class CEscribirBytes
{

public static void Main(string[] args)
{
FileStream fs = null;
byte[] buffer = new byte[81];
int nbytes = 0, car;

try
{
//Crear un flujo hacia el fichero texto.txt
fs = new FileStream("C:\\Datos\\texto.txt", FileMode.Create, FileAccess.Write);
Console.WriteLine("Escriba el texto que desea almacenar en el fichero:");
while ((car = Console.Read()) != '\r' && nbytes < buffer.Length)
{
buffer[nbytes] = (byte)car;
nbytes++;
}

//Escribir la linea de texto en el fichero
fs.Write(buffer, 0, nbytes);
}
catch (IOException e)
{
Console.WriteLine("Error: " + e.Message);

}
Console.ReadLine();
}
}
}
}




EJEMPLO 2

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
public class CLeerBytes
{
public static void Main(string[] args)
{
FileStream fe = null;
char[] cBuffer = new char[81];
byte[] bBuffer = new byte[81];
int nbytes;
try
{
// Crear un flujo desde el fichero texto.text
fe = new FileStream("C:\\Datos\\texto.txt", FileMode.Open, FileAccess.Read);
//Leer del fichero un linea de texto
nbytes = fe.Read(bBuffer, 0, 81);
//Crear un objeto string con el texto leido
Array.Copy(bBuffer, cBuffer, bBuffer.Length);
String str = new String(cBuffer, 0, nbytes);
// Mostrar el texto leido
Console.WriteLine(str);
}
catch (IOException e)
{
Console.WriteLine("Error: " + e.Message);
}
finally
{
//Cerrar el fichero
if (fe != null) fe.Close();
}
Console.ReadLine();
}
}
}


EJEMPLO 3

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace ConsoleApplication1
{
public class CEscribirCars
{
static void Main(string[] args)
{
StreamWriter sw = null;
string str;
try
{
//Crar un flujo hacia el fichero doc.txt
sw = new StreamWriter("C:\\Datos\\doc.txt");
Console.WriteLine("Escriba las lineas de texto a almacenar en el fichero.\n" + "Finalice cada linea pulsando la tecla .\n" + "Para finalizar solo pulse la tecla .\n");
//Leer la linea de la entrada estandar
str = Console.ReadLine();
while (str.Length != 0)
{
//Escribir la linea leida en el fichero
sw.WriteLine(str);
//Leer la linea siguiente
str = Console.ReadLine();
}
}
catch (IOException e)
{
Console.WriteLine("Error:" + e.Message);
}
finally
{
if (sw != null) sw.Close();
}




}
}
}


EJEMPLO 4

using System;
using System.IO;

public class CLeerCars
{
public static void Main(string[] args)
{
StreamReader sr = null;
String str;

try{
//Crear un flujo desde el fichero doc.txt
sr = new StreamReader("C:\\Datos\\doc.txt");
// Leer del fichero una linea de texto
str = sr.ReadLine();
while (str != null)
{
// Mostrar la linea leida
Console.WriteLine(str);
// Leer la linea siguiente
str = sr.ReadLine();
}
}
catch(IOException e)
{
Console.WriteLine("Error: " + e.Message);
}
finally
{
// cerrar el fichero
if (sr != null) sr.Close();
}
Console.ReadLine();
}
}

CLASES FILE, DIRECTORY Y PATH

ESTAS CLASES SOPORTAN MANIPULACION DEL NOMBRE DE UN FICHERO O DE UN DIRECTORI QUE PUEDE EXISTIR EN EL SISTEMA DE FICHEROS DE LA MAQUINA, POR LO TANTO, SUS METODOS PERMITIRAN INTERROGAR AL SISTEMA SOBRE TODAS LAS CARACTERISTICAS DE ESE FICHERO O DIRECTORIO. TODOS LOS METODOS DE ESTAS CLASES SON STATIC PARA QUE PUEDAN SER INVOCADOS SIN NECESIDAD DE QUE EXISTA UN OBJETO DE ELLAS.

PARA REFEIRNOS A UN FICHERO O UN DIRECTORIO, LO MAS SNCILLO ES FORMAR UN STRING A PARTIR DE SU NOMBRE AL QUE PODEMOS AÑADIR OPCIONALMENTE SU RUTA DE ACCESO.

FLUJOS

LOS FLUJOS SON OBJETOS INTERMEDIARIOS ENTRE EL PROGRAMA Y EL ORIGEN DE LA INFORMACION, DE TAL FORMA EL PROGRAMA LEERE O ESCRIBIRA SOBRE EL FLUJO SIN SABER DONDE PUEDE IR LA INFORMACION. PARA PODER REALIZAR ESTAS ACCIONES SE EMPLEAN CLASES YA DISEÑADAS POR LA BIBLIOTECA.NET PARA PODER LEER O ESCRIBIR POR EJEMPLO EXISTE FILESTREAM,STREAMWRITER,BINARYREADER Y BINARYWRITER.


LOS FLUJOS PUEDEN SER ESCRITOS O LEIDOS DE UN FICHERO CARACTER A CARACTER EN UN FORMATO PORTABLE UTILIZANDO FLUJOS DE LAS CLASES STREAMWRITER Y STREAMREADER
EN UN

Practica 11 VISUAL VARIAS FORMAS




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

namespace chaviuxprograma
{
class circulo
{double radio;
public circulo(double rad)
{
radio = rad;
}

public circulo()
{
radio = 1;
}
public double Radio
{
get{return radio ;}
set{radio = value ;}
}

public double area()
{
return Math.PI * Math.Pow(radio,2);
}

public double perimetro()
{
return 2 * Math.PI * radio;
}


}
}
class rectangulo
{
double ancho, largo;

public rectangulo (double anc, double lar)
{
ancho = anc;
largo = lar;
}

public rectangulo()
{
ancho = 1;
largo = 1;
}

public double Largo
{
get{return largo;}
set{largo = value;}
}
public double Ancho
{
get{return ancho;}
set{ancho = value;}

}

public double area()
{
return largo * ancho;
}

public double perimetro()
{
return 2 * (largo + ancho);
}

}
class triangulo
{
double lado1, lado2, lado3;

public triangulo (double lad1,double lad2,double lad3)
{
lado1 = lad1;
lado2 = lad2;
lado3 = lad3;
}

public triangulo()
{
lado1 = 1;
lado2 = 1;
lado3 = 1;
}

public double Lado1
{
get {return lado1;}
set{lado1 = value;}
}

public double Lado2
{
get {return lado2;}
set{lado2 = value;}
}

public double Lado3
{
get {return lado3;}
set{lado3 = value;}
}

public double area()
{
return Math.Sqrt(((lado1 + lado2 + lado3) / 2) * (((lado1 + lado2 + lado3) / 2) - lado1) * (((lado1 + lado2 + lado3) / 2) - lado2) * (((lado1 + lado2 + lado3) / 2) - lado3));
}
public double perimetro()
{
return lado1 + lado2 + lado3;
}
public double Altura()
{
return (Lado1 + lado2 + lado3) / 3;
}


}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace chaviuxprograma
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form f2 = new Form2();
f2.Show();
}

private void button2_Click(object sender, EventArgs e)
{
Form f3 = new Form3();
f3.Show();
}

private void button3_Click(object sender, EventArgs e)
{
Form f4 = new Form4();
f4.Show();
}

private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace chaviuxprograma
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
label1.Text = "Circulo";

}

private void button1_Click(object sender, EventArgs e)
{
circulo ci = new circulo();

ci.Radio = double.Parse(textBox1.Text);
listBox1.Items.Add("Dato ingresado:" + ci.Radio);
listBox1.Items.Add("\n");
listBox1.Items.Add("\nArea =" + ci.area().ToString());
listBox1.Items.Add("\n");
listBox1.Items.Add("\nPerimetro = " + ci.perimetro().ToString());
textBox1.Focus();
textBox1.Clear();



}

private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}

Tuesday, December 2, 2008

examen 3 era unidad

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

namespace EXAMEN_3ERA_UNIDAD
{

public class PUNTO
{
public int x1, x2, y1, y2;
public PUNTO() { }

public PUNTO(int x1, int x2, int y1, int y2)
{
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public virtual double area()
{
return 0;

}
public virtual double distancia()
{

return 0;

}
public class CIRCULO : PUNTO
{
public int X1, X2, Y1, Y2;
public double radio;
public CIRCULO()
{
}



public CIRCULO(int x1, int x2, int y1, int y2)
: base(x1, x2, y1, y2)
{
X1 = x1;
X2 = x2;
Y1 = y1;
Y2 = y2;
}
public void Radio(double radio)
{
this.radio = radio;
}


public override double distancia()
{
return Math.Sqrt((Math.Pow((X2 - X1), 2)) + (Math.Pow((Y2 - Y1), 2)));
//Math.Sqrt((X2 - X1)* 2 + (Y2 -Y1)* 2);
}


public override double area()
{

return Math.PI * Math.Pow(radio, 2);
}



}

}
}

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

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

double radio;


Console.Write("Introduce x1: ");
int x1=Int32.Parse(Console.ReadLine());

Console.Write("Introduce x2: ");
int x2=Int32.Parse(Console.ReadLine());

Console.Write("Introduce y1: ");
int y1=Int32.Parse(Console.ReadLine());

Console.Write("Introduce y2: ");
int y2=Int32.Parse(Console.ReadLine());
PUNTO p = new PUNTO(x1, x2, y1, y2);
CIRCULO circ2 = new CIRCULO();
CIRCULO circ = new CIRCULO();

Console.Write("Captura Radio x1y1: ");
double radio1 = Double.Parse(Console.ReadLine());
circ2.Radio(radio1);
Console.Write("Captura Radio x2y2: ");
radio = Double.Parse(Console.ReadLine());
circ.Radio(radio);

Console.WriteLine("\nel area del circulo x1y1 : {0}", circ.area());
Console.WriteLine("\nel area del circulo x1y1 : {0}", circ2.area());
Console.WriteLine("\nla distancia de estos circulos por las rectas x1y1 y y1y2 es : {0}", circ.distancia());
Console.ReadLine();

}
}
}