C#-та қосу, азайту, КӨбейту, БӨлу амалдары



бет19/19
Дата14.10.2023
өлшемі0,94 Mb.
#115131
1   ...   11   12   13   14   15   16   17   18   19
КАЛЬКУЛЯТОР 
Өткен сабақтарда біз қарапайым калькулятор жасаған болатынбыз. Енді, күрделірек түрін жасаймыз.

Мұнда 19 Button, 1 TextBox және 1 бос Label қолданылған. Осы түрдегі форма жасаймыз. TextBox-тың енін MultiLine-ды қолдана отырып үлкейттік.

1. TextBox пен Label-дің қасиеттерінде қаріп көлемін 12 пт деп өзгертеміз.

2. «0» батырмасын екі рет шертіп, келесі кодты жазамыз:
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 0;
}
3. Бұл қадамды барлық батырмалар үшін қайталап шығамыз (әр батырманы екі рет шертіп, пайда болған аймаққа сәйкесін тауып жазамыз):
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 1;
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 2;
}
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 3;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 4;
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 5;
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 6;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 7;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 8;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 9;
}
4. «.» батырмасы үшін:
private void button18_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ",";
}
5. Енді стандартты математикалық амалдарды орындау үшін келесі айнымалыларды код басында жариялаймыз:
float a, b;
int count;
bool znak = true;
6. «+» батырмасы үшін:
private void button4_Click(object sender, EventArgs e)
{
a = float.Parse(textBox1.Text);
textBox1.Clear();
count = 1;
label1.Text = a.ToString() + "+";
znak = true;
}
7. Жоғарыда көрсетілген сияқты «-», «*» және «/» батырмалары үшін келесі кодты жазамыз:
private void button8_Click(object sender, EventArgs e)
{
a = float.Parse(textBox1.Text);
textBox1.Clear();
count = 2;
label1.Text = a.ToString() + "-";
znak = true;
}
private void button12_Click(object sender, EventArgs e)
{
a = float.Parse(textBox1.Text);
textBox1.Clear();
count = 3;
label1.Text = a.ToString() + "*";
znak = true;
}
private void button16_Click(object sender, EventArgs e)
{
a = float.Parse(textBox1.Text);
textBox1.Clear();
count = 4;
label1.Text = a.ToString() + "/";
znak = true;
}
8. «=» батырмасы үшін:
private void button19_Click(object sender, EventArgs e)
{
calculate();
label1.Text = "";
}
9. Енді, calculate функциясын қолданамыз:
private void calculate()
{
switch(count)
{
case 1:
b = a + float.Parse(textBox1.Text);
textBox1.Text = b.ToString();
break;
case 2:
b = a - float.Parse(textBox1.Text);
textBox1.Text = b.ToString();
break;
case 3:
b = a * float.Parse(textBox1.Text);
textBox1.Text = b.ToString();
break;
case 4:
b = a / float.Parse(textBox1.Text);
textBox1.Text = b.ToString();
break;
default:
break;
}
}
10. «С» батырмасы үшін:
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "";
label1.Text = "";
}
11. «<–» батырмасы үшін:
private void button2_Click(object sender, EventArgs e)
{
int lenght = textBox1.Text.Length - 1;
string text = textBox1.Text;
textBox1.Clear();
for (int i = 0; i < lenght; i++)
{
textBox1.Text = textBox1.Text + text[i];
}
}
12. «+/-» батырмасы үшін:
private void button1_Click(object sender, EventArgs e)
{
if(znak==true)
{
textBox1.Text = "-" + textBox1.Text;
znak = false;
}
else if (znak==false)
{
textBox1.Text=textBox1.Text.Replace("-", "");
znak = true;
}
13. Бағдарлама нәтижесі:


Достарыңызбен бөлісу:
1   ...   11   12   13   14   15   16   17   18   19




©emirsaba.org 2024
әкімшілігінің қараңыз

    Басты бет