Ksenya
20-10-2011, 19:56
Здравствуйте. В попытке создания простейшего калькулятора на с# возникли проблемы: действия "*" и "/". Ошибка зарылась в глобальных переменных начальных значений, только в чем именно уму не приходит(
namespace Kalkulator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_1_Click(object sender, EventArgs e)
{
textBox1.Text += 1;
textBox2.Text += 1;
}
private void button_2_Click(object sender, EventArgs e)
{
textBox1.Text += 2;
textBox2.Text += 2;
}
private void button_3_Click(object sender, EventArgs e)
{
textBox1.Text += 3;
textBox2.Text += 3;
}
private void button_4_Click(object sender, EventArgs e)
{
textBox1.Text += 4;
textBox2.Text += 4;
}
private void button_5_Click(object sender, EventArgs e)
{
textBox1.Text += 5;
textBox2.Text += 5;
}
private void button_6_Click(object sender, EventArgs e)
{
textBox1.Text += 6;
textBox2.Text += 6;
}
private void button_7_Click(object sender, EventArgs e)
{
textBox1.Text += 7;
textBox2.Text += 7;
}
private void button_8_Click(object sender, EventArgs e)
{
textBox1.Text += 8;
textBox2.Text += 8;
}
private void button_9_Click(object sender, EventArgs e)
{
textBox1.Text += 9;
textBox2.Text += 9;
}
private void button_0_Click(object sender, EventArgs e)
{
textBox1.Text += 0;
textBox2.Text += 0;
}
private void button_plus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '+';
sum += Double.Parse(textBox1.Text);
textBox1.Text = "";
textBox2.Text += "+";
}
}
private void button_minus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '-';
sum -= Double.Parse(textBox1.Text);
textBox1.Text = "";
textBox2.Text += "-";
}
}
private void button_mult_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '*';
sam *= Double.Parse(textBox1.Text);
sum = sam;
textBox1.Text = "";
textBox2.Text += "*";
}
}
private void button_del_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '/';
sam /= Double.Parse(textBox1.Text);
sum = sam;
textBox1.Text = "";
textBox2.Text += "/";
}
}
private void button_pick_Click(object sender, EventArgs e)
{
textBox1.Text += ",";
textBox2.Text += ".";
}
private void button_answ_Click(object sender, EventArgs e)
{
if (ch == '+')
sum += Double.Parse(textBox1.Text);
if (ch == '-')
sum -= Double.Parse(textBox1.Text);
if (ch == '*')
sum *= Double.Parse(textBox1.Text);
if (ch == '/')
sum /= Double.Parse(textBox1.Text);
textBox1.Text = sum.ToString() ;
textBox2.Text = textBox1.Text;
}
public double sam=1;
public double sum=0;
}
}
namespace Kalkulator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_1_Click(object sender, EventArgs e)
{
textBox1.Text += 1;
textBox2.Text += 1;
}
private void button_2_Click(object sender, EventArgs e)
{
textBox1.Text += 2;
textBox2.Text += 2;
}
private void button_3_Click(object sender, EventArgs e)
{
textBox1.Text += 3;
textBox2.Text += 3;
}
private void button_4_Click(object sender, EventArgs e)
{
textBox1.Text += 4;
textBox2.Text += 4;
}
private void button_5_Click(object sender, EventArgs e)
{
textBox1.Text += 5;
textBox2.Text += 5;
}
private void button_6_Click(object sender, EventArgs e)
{
textBox1.Text += 6;
textBox2.Text += 6;
}
private void button_7_Click(object sender, EventArgs e)
{
textBox1.Text += 7;
textBox2.Text += 7;
}
private void button_8_Click(object sender, EventArgs e)
{
textBox1.Text += 8;
textBox2.Text += 8;
}
private void button_9_Click(object sender, EventArgs e)
{
textBox1.Text += 9;
textBox2.Text += 9;
}
private void button_0_Click(object sender, EventArgs e)
{
textBox1.Text += 0;
textBox2.Text += 0;
}
private void button_plus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '+';
sum += Double.Parse(textBox1.Text);
textBox1.Text = "";
textBox2.Text += "+";
}
}
private void button_minus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '-';
sum -= Double.Parse(textBox1.Text);
textBox1.Text = "";
textBox2.Text += "-";
}
}
private void button_mult_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '*';
sam *= Double.Parse(textBox1.Text);
sum = sam;
textBox1.Text = "";
textBox2.Text += "*";
}
}
private void button_del_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("Данные не введены");
else
{
ch = '/';
sam /= Double.Parse(textBox1.Text);
sum = sam;
textBox1.Text = "";
textBox2.Text += "/";
}
}
private void button_pick_Click(object sender, EventArgs e)
{
textBox1.Text += ",";
textBox2.Text += ".";
}
private void button_answ_Click(object sender, EventArgs e)
{
if (ch == '+')
sum += Double.Parse(textBox1.Text);
if (ch == '-')
sum -= Double.Parse(textBox1.Text);
if (ch == '*')
sum *= Double.Parse(textBox1.Text);
if (ch == '/')
sum /= Double.Parse(textBox1.Text);
textBox1.Text = sum.ToString() ;
textBox2.Text = textBox1.Text;
}
public double sam=1;
public double sum=0;
}
}