Math Sınıfı örnekleri – Matematiksel Metodlar – Csharp
MATH SINIFI — MATEMATİKSEL METODLAR
Visual Studio içerisinde basit matematiksel işlemler gerçekleştirmek için gerekli operatörler mevcut olduğu gibi, detaylı matematiksel işlemler için de Math sınıfından faydalanılır.
***Bir sayının radyan türünden sinüsünü bulmak için Math.Sin( sayı ) biçimindeki kullanım
yeterli iken, aynı sonucu derece cinsinden Math.Sin( Math.PI/180 * sayı ) biçimindeki kullanımı ile buluruz.
//www.gorselprogramlama.com
Örnek 1 :Math sınıfı ile ilgili bir metin kutusuna girilen sayının sinüs ve kosinüsünü radyan ve derece cinsinden bulacak örnek bir uygulama yapalım.
privatevoid button1_Click(object sender, EventArgs e)
{
double sayı = Convert.ToDouble(textBox1.Text);
if (radioButton1.Checked)
{
textBox1.Text = Math.Sin(Math.PI / 180 * sayı).ToString();
}
if (radioButton2.Checked)
{
textBox1.Text = Math.Sin(sayı).ToString();
}
}
privatevoid button2_Click(object sender, EventArgs e)
{
double sayı = Convert.ToDouble(textBox1.Text);
if (radioButton1.Checked)
{
textBox1.Text = Math.Cos(Math.PI / 180 * sayı).ToString();
}
if (radioButton2.Checked)
{
textBox1.Text = Math.Cos(sayı).ToString();
}
}
Örnek olarak öncelikle 30 değerinin sinüsünü buluyoruz radyan türünden buluyoruz.
Daha sonra aynı değerin sinüsünü, derece türünden buluyoruz
Örnek 2 : Math sınıfı ile bir metot tanımlayarak comboBox’ın içindeki işlemleri derece cinsinden yapan örnek bir uygulama yapalım.(Form açıldığında comboBox’a SİNÜS, COSİNÜS ve TANJANT değerleri eklenecektir . Açıları derece cinsinden bulunacak.)
//www.gorselprogramlama.com
namespace WindowsFormsApplication1
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
double acıhesapla(double s1)
{
double sonuc;
if (comboBox1.SelectedItem == “COSİNÜS”) sonuc = Math.Cos(Math.PI * s1 / 180);
elseif(comboBox1.SelectedItem == “SİNÜS”) sonuc = Math.Sin(Math.PI * s1 / 180);
else sonuc = Math.Tan(Math.PI * s1 / 180);
return sonuc;
}
privatevoid button1_Click(object sender, EventArgs e)
{
double s1 = Convert.ToInt16(textBox1.Text);
MessageBox.Show(acıhesapla(s1).ToString());
}
privatevoid Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add(“SİNÜS”);
comboBox1.Items.Add(“COSİNÜS”);
comboBox1.Items.Add(“TANJANT”);
}
}
Örnek 3 :Math sınıfı ile kökalma ve üsalma isimli iki metot tanımlayıpformdaki işlemleri ilgili textBoxlara yazdıran örnek bir uygulama yapalım.
publicpartialclassForm1 : Form
| FORM1 |
{
public Form1()
{
InitializeComponent();
}
publicstaticdouble karekök(double s1)
{
double sonuc = Math.Sqrt(s1);
return sonuc;
}
publicstaticdouble üsalma(double s1, double s2)
{
double sonuc = Math.Pow (s1,s2);
return sonuc;
}
privatevoid button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
Form2 frm2 = newForm2();
frm2.Show();
}
if (radioButton2.Checked)
{
Form3 frm3 = newForm3();
frm3.Show()
| FORM2 |
}
//www.gorselprogramlama.com
publicpartialclassForm2 : Form
{
public Form2()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
int s1 = Convert.ToInt16(textBox1.Text);
textBox2.Text = Form1.karekök(s1).ToString();
}
| FORM3 |
publicpartialclassForm3 : Form
{
public Form3()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
int s1 = Convert.ToInt16(textBox1.Text);
int s2 = Convert.ToInt16(textBox2.Text);
textBox3.Text = Form1.üsalma(s1, s2).ToString();
}

Örnek olarak öncelikle üs almayı seçiyoruz ve seç butonuna tıklıyoruz. Daha 2 üssü 4 işlemini yaptırıyoruz ve 16 sonucunu alıyoruz.
Örnek 4 :Math sınıfı ile textBox’a girilen sayıyı alt ve üst sayıya yuvarlayan messageBox’la ekrana yazdıran örnek bir uygulama yapalım.
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
double s1 = Convert.ToDouble(textBox1.Text);
MessageBox.Show(Math.Ceiling(s1).ToString());
}
privatevoid button2_Click(object sender, EventArgs e)
{
double s2 = Convert.ToDouble(textBox1.Text);
MessageBox.Show(Math.Floor(s2).ToString());
}
}//www.gorselprogramlama.com
Örnek olarak 2,4 sayısını girip alt sayıya yuvarla butonuna bastığımızda messageBox’la 2 sonucunu alıyoruz
Örnek 5 :Math sınıfı ile girilen sayının işaretini bulan, işareti pozitif, negatif veya sıfır şeklinde messageBox’la ekrana yazdıran ve son işlemleri listBox’ta tutan örnek bir uygulama yapalım.
privatevoid button1_Click(object sender, EventArgs e)
{
int sayı = Convert.ToInt16(textBox1.Text);
int sonuc = Math.Sign(sayı);
if (sonuc == -1)
{
MessageBox.Show(“NEGATİF”);
listBox1.Items.Add(Math.Sign(sayı));
}
if ( sonuc == 1)
{
MessageBox.Show(“POZİTİF”);
listBox1.Items.Add(Math.Sign(sayı));
}
if ( sonuc == 0 )
{
MessageBox.Show(” SIFIR ” );
listBox1.Items.Add(Math.Sign(sayı));
}
Örnek olarak 5 sayısını girdiğimizde POZİTİF mesajını verip listBox’a POZİTİF değerini ekliyor ikinci olarak 0 sayısını girdiğimizde SIFIR mesajını verip listBox’a SIFIR değerini ekliyor son olarak -5 sayısını girdiğimizde NEGATİF mesajını verip listBox’a NEGATİF değerini ekliyor.
//www.gorselprogramlama.com
Örnek 6 : Form yüklendiğinde comboBox’ta 0’dan 20’ ye kadar olan sayılar yazıcaktır.Seçilen değerin karesini Math sınıfı ile bulup textBox’a yazdıran örnek bir uygulama yapalım.

privatevoid Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 20; i++)
{
comboBox1.Items.Add(i);
}
}
privatevoid comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int s2 = 2;
textBox1.Text = Math.Pow(comboBox1.SelectedIndex, s2).ToString();
}
}
Örnek olarak 7’yi seçtiğimizde 7’nin karesi 49’u textBox’ta görüyoruz.
Örnek 7 : Form açıldığında label1, label2, textBox1, textBox2 gizlenecektir. İşlem seçildiğnide ilgili alanlar görünür olacak ve sonuç Math sınıfıyla hesaplanıp textBox3’e yazdıran örnek bir uygulama yapalım.
privatevoid button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
int s1 = Convert.ToInt16(textBox1.Text);
textBox3.Text = Math.Log(s1).ToString();
}
if (radioButton2.Checked)
{
int s1 = Convert.ToInt16(textBox1.Text);
int s2 = Convert.ToInt16(textBox1.Text);
textBox3.Text = Math.Log(s1,s2).ToString();
}
if (radioButton3.Checked)
{
int s1 = Convert.ToInt16(textBox1.Text);
textBox3.Text = Math.Log10(s1).ToString();
}//www.gorselprogramlama.com
}
privatevoid radioButton1_CheckedChanged(object sender, EventArgs e)
{
label2.Visible = true;
textBox1.Visible = true;
label3.Visible = false;
textBox2.Visible = false;
}
privatevoid radioButton2_CheckedChanged(object sender, EventArgs e)
{
label2.Visible = true;
textBox1.Visible = true;
label3.Visible = true;
textBox2.Visible = true;
}
privatevoid radioButton3_CheckedChanged(object sender, EventArgs e)
{
label2.Visible = true;
textBox1.Visible = true;
label3.Visible = false;
textBox2.Visible = false;
}
}
Örnek olarak log 10 tabanında 2 yi girdiğimizde 0,301029995663981 sonucunu alırız.
Örnek 8 : Sayının faktöriyelini hesaplayıp sonucu textBox’a yazan ve sayı negatif ise Math sınıfı ile mutlak değerini alıp faktöriyelini hesaplayan örnek bir uygulama yapalım.
privatevoid button1_Click(object sender, EventArgs e)
{
int s1 = Convert.ToInt16(textBox1.Text);
int sonuc = 1;
if (s1 > 0)
{
for (int i = 1; i <= s1; i++)
{
sonuc *= i;
textBox2.Text=sonuc.ToString();
}
}
if (s1 < 0)
{
for (int i = 1; i <= Math.Abs(s1); i++)
{
sonuc *= i;//www.gorselprogramlama.com
textBox2.Text =sonuc.ToString();
}
}
}
privatevoid Form1_Load(object sender, EventArgs e)
{
textBox2.Enabled = false;
}
}

Örnek olarak -5 girdiğimizde -5’in mutlak değerini alıp faktöriyel hesaplandığında 120 sonucunu verir.
Örnek 9 : Math sınıfı ile girilen sayının sinüsünü, cosinüsünü, tanjantını, hiperbolik sinüsünü, hiperbolik cosinüsünü, hiperbolik tanjantını ilgili label’lere yazan örnek bir uygulama yapalım.
privatevoid Form1_Load(object sender, EventArgs e)
{
label8.Text = “”;
label9.Text = “”;
label10.Text = “”;
label11.Text = “”;
label12.Text = “”;
label13.Text = “”;
}
privatevoid button1_Click(object sender, EventArgs e)
{
int s1 = Convert.ToInt16(textBox1.Text);
label8.Text = Math.Sin(s1).ToString();
label9.Text = Math.Sinh(s1).ToString();
label10.Text = Math.Cos(s1).ToString();
label11.Text = Math.Cosh(s1).ToString();
label12.Text = Math.Tan(s1).ToString();
label13.Text = Math.Tanh(s1).ToString();
}

Örnek olarak 30 değerini girdiğimizde aşağıdaki sonuçları elde ederiz.
Örnek 10 :Aşağıdaki formdaki olayları Math sınıfı ile yapacağımız örnek bir uygulama yapalım.

privatevoid button1_Click(object sender, EventArgs e)
{//www.gorselprogramlama.com
if ((checkBox1.Checked == true) && (checkBox2.Checked == false))
{
int s1 = Convert.ToInt16(textBox1.Text);
MessageBox.Show( “KAREKÖK = ” + Math.Sqrt(s1).ToString());
}
if ((checkBox2.Checked == true) && (checkBox1.Checked == false))
{
int s1 = Convert.ToInt16(textBox1.Text);
int s2 = Convert.ToInt16(textBox2.Text);
MessageBox.Show(“ÜS = ” + Math.Pow(s1,s2).ToString());
}
if ((checkBox2.Checked == true) && (checkBox1.Checked == true))
{
int s1 = Convert.ToInt16(textBox1.Text);
int s2 = Convert.ToInt16(textBox2.Text);
MessageBox.Show(“KAREKÖK = ” + Math.Sqrt(s1) + ” ” + “ÜS = ” + Math.Pow(s1,s2).ToString());
}
}

Örnek olarak 5 üssü 2’yi girdiğimizde messageBox’ta 25 sonucunu alıyoruz.













Pıroglam larınız cok güzel sizi tebrik ederim dostu