Prévia do material em texto
Cristal Aricel Sáenz Hernández 18131059 Practica 6 Hacer los programas indicados en C# A lunes 17 abril 2023 Ejercicio 1: Diseñe un formulario que contenga un TextBox en donde el usuario introduzca un numero decimal (double) y tres botones, la caratula del primer botón dirá binario, y al dar con clic en dicho botón, debajo de el una etiqueta mostrara la conversión del numero decimal en binario, lo mismo ocurrirá con otros dos botones los cuales tendrán en su caratula octal y hexadecimal respectivamente. Repita el ejercicio ahora con una lista desplegable en donde el usuario seleccione a que base quiere convertir el numero decimal introducido en el textBox. Código using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DECIMAL_CONVERSION { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void aceptar_Click(object sender, EventArgs e) { octal.Enabled = true; binario.Enabled = true; hexadecimal.Enabled = true; } private void octal_Click(object sender, EventArgs e) { int n; string s=" "; n = Convert.ToInt32(num.Value.ToString()); int v = 0; if (n == 0) { lab1.Text = Convert.ToString(v); } else { while (n > 0) { s = n % 8 + s; n = n / 8; } lab1.Text = s; } } private void label3_Click(object sender, EventArgs e) { } private void binario_Click(object sender, EventArgs e) { int n; string s = " "; n = Convert.ToInt32(num.Value.ToString()); int v = 0; if (n == 0) { lab2.Text = Convert.ToString(v); } else { while (n > 0) { s = n % 2 + s; n = n / 2; } lab2.Text =s; } } private void hexadecimal_Click(object sender, EventArgs e) { int n; string s = " "; n = Convert.ToInt32(num.Value.ToString()); int res; int v = 0; if (n == 0) { lab3.Text = Convert.ToString(v); } else { while (n>0) { res = n % 16; if (res == 10) { s = "A" + s; } else if(res == 11) { s = "B" + s; } else if(res == 12) { s = "C" + s; } else if (res == 13) { s = "D" + s; } else if (res == 14) { s = "E" + s; } else if (res == 15) { s = "F" + s; } else { s = n % 16 + s; } n = n / 16; } lab3.Text = s; } } private void reiniciar_Click(object sender, EventArgs e) { num.Value = 0; lab1.Text = " "; lab2.Text = " "; lab3.Text = " "; octal.Enabled = false; hexadecimal.Enabled = false; binario.Enabled = false; } private void salir_Click(object sender, EventArgs e) { Close(); } } } Evidencias Ejercicio 2: En este ejercicio se utilizará la propiedad Visible de un control, la cual puede establecerse como true o false. Por ejemplo el siguiente código hace invisible la etiqueta label1: label1.Visible = false; Escriba un programa con dos botones y una etiqueta . Al hacer clic en un botón, la etiqueta deberá hacerse invisible y al hacer clic en el otro botón deberá hacerse visible de nuevo. Código using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics.Eventing.Reader; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace dispensadora { public partial class dispensadora : Form { public dispensadora() { InitializeComponent(); } private void ca_ValueChanged(object sender, EventArgs e) { int ua; double ta; ua=Convert.ToInt32(ca.Value); ta = ua * 30; pa.Text = ta.ToString(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void d_Click(object sender, EventArgs e) { cd.Enabled = true; } private void button2_Click(object sender, EventArgs e) { Close(); } private void a_Click(object sender, EventArgs e) { ca.Enabled = true; } private void b_Click(object sender, EventArgs e) { cb.Enabled = true; } private void c_Click(object sender, EventArgs e) { cc.Enabled = true; } private void cb_ValueChanged(object sender, EventArgs e) { int ub; double tb; ub = Convert.ToInt32(cb.Value); tb = ub * 40; pb.Text = tb.ToString(); } private void cc_ValueChanged(object sender, EventArgs e) { int uc; double tc; uc = Convert.ToInt32(cc.Value); tc = uc * 55; pc.Text = tc.ToString(); } private void cd_ValueChanged(object sender, EventArgs e) { int ud; double td; ud = Convert.ToInt32(cd.Value); td = ud * 65; pd.Text = td.ToString(); } private void cal_Click(object sender, EventArgs e) { //primera parte int total; int ua; int ta; ua = Convert.ToInt32(ca.Value); ta = ua * 30; int ub; int tb; ub = Convert.ToInt32(cb.Value); tb = ub * 40; int uc; int tc; uc = Convert.ToInt32(cc.Value); tc = uc * 55; int ud; int td; ud = Convert.ToInt32(cd.Value); td = ud * 65; total = (ta + tb + tc + td); tot.Text = total.ToString(); //PARA DAR LA FERIA int euro; int a; a = Convert.ToInt32(prueba.SelectedIndex);if (a == 1) { euro = a; } else if (a==2) { euro = a; } else if (a == 3) { euro = a; } else if (a == 4) { euro = a; } else if (a == 5) { euro = a; } else if (a == 6) { euro = a; } else if (a == 7) { euro = a; } else if (a == 8) { euro = a; } else if (a == 9) { euro = a; } else if (a == 10) { euro = a; } else { MessageBox.Show ("No se ha ingresado un numero valido"); } //50 20 10 5 2 1 //FERIASAAASASASAS int monto; int cincuenta = 50; int veinte = 20; int cinco = 5; int dos = 2; int uno = 1; euro = a * 100; monto = euro - total; if (monto<0) { MessageBox.Show("No se ha ingresado la cantidad necesaria"); } else { MessageBox.Show("Te sobran: "+ monto.ToString()+" centavos"); int primero; int segundo; int tercero; int cuarto; int seg; int terc; int cuart; int quinto; int quint; primero = monto / cincuenta; seg = monto % cincuenta; segundo = seg / veinte; terc = seg % veinte; tercero = terc / cinco; cuart = terc % cinco; cuarto = cuart / dos; quint = cuart % dos; quinto = quint / uno; MessageBox.Show("Te sobran: " + primero.ToString() + " de 50 centavos"); MessageBox.Show("Te sobran: " + segundo.ToString() + " de 20 centavos"); MessageBox.Show("Te sobran: " + tercero.ToString() + " de 5 centavos"); MessageBox.Show("Te sobran: " + cuarto.ToString() + " de 2 centavos"); MessageBox.Show("Te sobran: " + quinto.ToString() + " de 1 centavos"); } } private void Form1_Load(object sender, EventArgs e) { } private void calcular_Click(object sender, EventArgs e) { int total; int ua; int ta; ua = Convert.ToInt32(ca.Value); ta = ua * 30; int ub; int tb; ub = Convert.ToInt32(cb.Value); tb = ub * 40; int uc; int tc; uc = Convert.ToInt32(cc.Value); tc = uc * 55; int ud; int td; ud = Convert.ToInt32(cd.Value); td = ud * 65; total = (ta + tb + tc + td); tot.Text = total.ToString(); } private void prueba_SelectedIndexChanged(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { ca.Enabled = false; cb.Enabled = false; cc.Enabled = false; cd.Enabled = false; ca.Value = 0; cb.Value = 0; cc.Value = 0; cd.Value = 0; tot.Text = ""; pa.Text = ""; pb.Text = ""; pc.Text = ""; pd.Text = ""; prueba.SelectedIndex =0; } } } Evidencia . Otros ejercicios: (propuestos en clase) Lluvia Código using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace lluvia { public partial class Form1 : Form { private Random númeroAleatorio = new Random(); private Random a = new Random(); private Graphics papel; public Form1() { InitializeComponent(); papel = pictureBox1.CreateGraphics(); etiquetaIntervalo.Text = Convert.ToString(trackBar1.Value); } private void label2_Click(object sender, EventArgs e) { } private void iniciar_Click(object sender, EventArgs e) { timer1.Start(); } private void detener_Click(object sender, EventArgs e) { timer1.Stop(); } private void borrar_Click(object sender, EventArgs e) { papel.Clear(Color.White); } private void trackBar1_Scroll(object sender, EventArgs e) { int intervaloTiempo = trackBar1.Value; etiquetaIntervalo.Text = Convert.ToString(intervaloTiempo); } private void timer1_Tick(object sender, EventArgs e) { int X, Y, TAMAÑO; Brush miPincel = new SolidBrush(Color.Red); X = númeroAleatorio.Next(0, pictureBox1.Width); Y = númeroAleatorio.Next(0, pictureBox1.Height); TAMAÑO = númeroAleatorio.Next(1, 20); papel.FillEllipse(miPincel, X, Y, TAMAÑO, TAMAÑO); int z, w, TAMAO; Brush miPincel1 = new SolidBrush(Color.Blue); z = a.Next(0, pictureBox1.Width); w = a.Next(0, pictureBox1.Height); TAMAO = a.Next(1, 20); papel.FillEllipse(miPincel1, z, w, TAMAO, TAMAO); timer1.Stop(); timer1.Interval = númeroAleatorio.Next(1, trackBar1.Value); a.Next(1, trackBar1.Value); timer1.Start(); } private void button4_Click(object sender, EventArgs e) { Close(); } } } Evidencia Borrachito Codigo using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace borrachito { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void pictureBox1_Click(object sender, EventArgs e) { int x, y, pasox, pasoy, nuevax, nuevay, pasos; Graphics papel; papel = pictureBox1.CreateGraphics(); Random n = new Random(); Pen la = new Pen(Color.Black); la.Width = 4; x = pictureBox1.Width / 2; y = pictureBox1.Height / 2; for(pasos=0;x<pictureBox1.Width &&x>0&&y<pictureBox1.Height&&y>0;pasos++) { pasox = n.Next(- 50, 50); pasoy = n.Next(- 50, 50); nuevax = x + pasox; nuevay = y + pasoy; papel.DrawLine(la, x, y, nuevax, nuevay); x = nuevax; y = nuevay; } label1.Text = "Se necesitaron " + Convert.ToString(pasos) + " para llegar a la pared"; } private void button1_Click(object sender, EventArgs e) { Close(); } private void button2_Click(object sender, EventArgs e) { } } } Evidencias Dados Código using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace dados { public partial class Form1 : Form { public Form1() { InitializeComponent();} private void trackBar1_Scroll(object sender, EventArgs e) { VerificarValores(); } private void trackBar2_Scroll(object sender, EventArgs e) { VerificarValores(); } private void VerificarValores() { int dado1, dado2, total; dado1 = trackBar1.Value; dado2 = trackBar2.Value; total = dado1 + dado2; label1.Text = "El total es " + Convert.ToString(total); if (total ==7) { label2.Text = "Usted gana"; } else if (total == 2) { label2.Text = "Usted gana"; } else { label2.Text = "Usted pierde"; } } } } Evidencias