Pierwszy program (kalkulator w C#)
Napisane: poniedziałek, 29 października 2012, 16:00
Witam.
Stworzyłem swój pierwszy program lecz mam pewne problemy:/
Otóż nie potrafię zastosować wyjątki dzielenia przez zero:/ gdy próbuje dzielić przez zero wyskakuje mi granica(nieskończoność). Zapewne nie to powinno wyjść:/ Jak to poprawić?
Drugi mój problem to jak zrobić abym mógł wpisywać cyfry dwucyfrowe? Bo w tym momencie mogę wpisywać tylko jedności:/
Oto kod:
Stworzyłem swój pierwszy program lecz mam pewne problemy:/
Otóż nie potrafię zastosować wyjątki dzielenia przez zero:/ gdy próbuje dzielić przez zero wyskakuje mi granica(nieskończoność). Zapewne nie to powinno wyjść:/ Jak to poprawić?
Drugi mój problem to jak zrobić abym mógł wpisywać cyfry dwucyfrowe? Bo w tym momencie mogę wpisywać tylko jedności:/
Oto kod:
Code: Zaznacz cały
using System;
using System.Windows.Forms;
namespace Kalkulator
{
public partial class Form1 : Form
{
public double LB1;
public double LB2;
public string znak;
public double liczba;
public double ujemna;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button19_Click(object sender, EventArgs e)
{
LB1 = 0;
LB2 = 0;
textBox1.Text = " ";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = button3.Text;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = button4.Text;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = button5.Text;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = button6.Text;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = button7.Text;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = button8.Text;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = button9.Text;
}
private void button18_Click(object sender, EventArgs e)
{
textBox1.Text = button18.Text;
}
private void button12_Click(object sender, EventArgs e)
{
LB1 = System.Double.Parse(textBox1.Text);
textBox1.Text = button12.Text;
znak = "+";
}
private void button15_Click(object sender, EventArgs e)
{
LB1 = System.Double.Parse(textBox1.Text);
textBox1.Text = button15.Text;
znak = "*";
}
private void button14_Click(object sender, EventArgs e)
{
LB1 = System.Double.Parse(textBox1.Text);
textBox1.Text = button14.Text;
znak = "/";
}
private void button13_Click(object sender, EventArgs e)
{
LB1 = System.Double.Parse(textBox1.Text);
textBox1.Text = button13.Text;
znak = "-";
}
private void button17_Click(object sender, EventArgs e)
{
wynik();
}
public void wynik()
{
LB2 = System.Double.Parse(textBox1.Text);
switch (znak)
{
case "+": liczba = LB1 + LB2;
break;
case "-": liczba = LB1 - LB2;
break;
case "*": liczba = LB1 * LB2;
break;
case "/":
try
{
liczba = LB1 / LB2;
}
catch (DivideByZeroException e)
{
textBox1.Text = e.Message;
}
break;
}
textBox1.Text = LB1.ToString() + znak + LB2.ToString() + " = " + liczba.ToString();
}
private void button10_Click(object sender, EventArgs e)
{
LB1 = System.Double.Parse(textBox1.Text);
liczba = LB1 * (-1);
textBox1.Text = liczba.ToString();
}
}
}