1.- Calculadora
mi ejercicio (4.5 de 5 puntos debido a que no controle errores(no se pude dividir por cero) ni trabaje con decimales).
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 _1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Borrar
private void btnBorrar_Click(object sender, EventArgs e)
{
txtOperandos.Clear();
txtResultado.Clear();
}
//Numeros
private void button1_Click(object sender, EventArgs e)
{
txtOperandos.Text += 1;
}
private void button2_Click(object sender, EventArgs e)
{
txtOperandos.Text += 2;
}
private void button3_Click(object sender, EventArgs e)
{
txtOperandos.Text += 3;
}
private void button4_Click(object sender, EventArgs e)
{
txtOperandos.Text += 4;
}
private void button5_Click(object sender, EventArgs e)
{
txtOperandos.Text += 5;
}
private void button6_Click(object sender, EventArgs e)
{
txtOperandos.Text += 6;
}
private void button7_Click(object sender, EventArgs e)
{
txtOperandos.Text += 7;
}
private void button8_Click(object sender, EventArgs e)
{
txtOperandos.Text += 8;
}
private void button9_Click(object sender, EventArgs e)
{
txtOperandos.Text += 9;
}
private void button10_Click(object sender, EventArgs e)
{
txtOperandos.Text += 0;
}
//Operandos
string operando;
int n1;
private void btnSumar_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtOperandos.Text);
operando = "+";
txtOperandos.Clear();
}
private void btnRestar_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtOperandos.Text);
operando = "-";
txtOperandos.Clear();
}
private void btnMultiplicar_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtOperandos.Text);
operando = "*";
txtOperandos.Clear();
}
private void btnDividir_Click(object sender, EventArgs e)
{
n1 = int.Parse(txtOperandos.Text);
operando = "/";
txtOperandos.Clear();
}
//Resultado
int n2;
int resultado;
private void btnResultado_Click(object sender, EventArgs e)
{
n2 = int.Parse(txtOperandos.Text);
switch (operando)
{
case "+":
resultado = n1 + n2;
break;
case "-":
resultado = n1 - n2;
break;
case "*":
resultado = n1 * n2;
break;
case "/":
resultado = n1 / n2;
break;
}
txtResultado.Text = resultado.ToString();
}
}
}
Resultado profesor (5 pnt. El botón de imprimir es un extra que no entraba en el examen)
using System;
using System.IO;
using System.Windows.Forms;
namespace Actividad8_4_Calculadora
{
public partial class FrmCalculadora : Form
{
double a, b;
string operacion;
public FrmCalculadora()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
txtOperando.Text += "0";
}
private void btn1_Click(object sender, EventArgs e)
{
txtOperando.Text += "1";
}
private void btn2_Click(object sender, EventArgs e)
{
txtOperando.Text += "2";
}
private void btn3_Click(object sender, EventArgs e)
{
txtOperando.Text+= "3";
}
private void btnResta_Click(object sender, EventArgs e)
{
a = int.Parse(this.txtOperando.Text);
operacion = "-";
this.txtOperando.Clear();
this.txtOperando.Focus();
}
private void btnBorrar_Click(object sender, EventArgs e)
{
a = 0;
b = 0;
txtOperando.Clear();
txtResultado.Clear();
}
private void btnIgual_Click(object sender, EventArgs e)
{
b = int.Parse(txtOperando.Text);
switch (operacion)
{
case "+":
txtResultado.Text = Convert.ToString(b + a);
break;
case "-":
txtResultado.Text = Convert.ToString(a - b);
break;
case "*":
txtResultado.Text = Convert.ToString(b * a);
break;
case "/":
if (b != 0)
txtResultado.Text = Convert.ToString(a / b);
else
MessageBox.Show("No puedes dividir por cero","Error",MessageBoxButtons.OK);
break;
}
txtOperando.Clear();
}
private void btn4_Click(object sender, EventArgs e)
{
txtOperando.Text += "4";
}
private void btn5_Click(object sender, EventArgs e)
{
txtOperando.Text+= "5";
}
private void btn6_Click(object sender, EventArgs e)
{
if (txtOperando.Text == "")
{
txtOperando.Text = "6";
}
else
{
txtOperando.Text = txtOperando.Text + "6";
}
}
private void btn7_Click(object sender, EventArgs e)
{
if (txtOperando.Text == "")
{
txtOperando.Text = "7";
}
else
{
txtOperando.Text = txtOperando.Text + "7";
}
}
private void btn8_Click(object sender, EventArgs e)
{
if (txtOperando.Text == "")
{
txtOperando.Text = "8";
}
else
{
txtOperando.Text = txtOperando.Text + "8";
}
}
private void btn9_Click(object sender, EventArgs e)
{
if (txtOperando.Text == "")
{
txtOperando.Text = "9";
}
else
{
txtOperando.Text = txtOperando.Text + "9";
}
}
private void btnMultiplica_Click(object sender, EventArgs e)
{
a = int.Parse(this.txtOperando.Text);
operacion = "*";
this.txtOperando.Clear();
this.txtOperando.Focus();
}
private void btnDivide_Click(object sender, EventArgs e)
{
a = int.Parse(this.txtOperando.Text);
operacion = "/";
this.txtOperando.Clear();
this.txtOperando.Focus();
}
public void btnImprimir_Click(object sender, EventArgs e)
{
StreamWriter archivo = new StreamWriter("archivo.txt");
archivo.WriteLine("Operaciones: " + a + operacion + b + "=" +txtResultado.Text);
archivo.Flush();//pasa los datos que están en memoria volatil al dispositivo, es como un commit
archivo.Close();
System.Diagnostics.Process.Start("archivo.txt");//abre el archivo
}
private void btnSuma_Click(object sender, EventArgs e)
{
a = int.Parse(txtOperando.Text);
operacion = "+";
this.txtOperando.Clear();
this.txtOperando.Focus();
}
}
}
0 comentarios:
Publicar un comentario