C#’ta Permütasyon Hesaplama
- Abdullah Akpınar
- 4.605
- Orta
- 03 Nisan 2014
Bu dersimizde C#’ta permütasyon hesaplayan programı yazacağız.
Bu dersimizde C#’ta permütasyon hesaplayan programı yazacağız. İlk olarak programın görüntüsünü oluşturalım: Daha sonra ilerde çıkabilecek hataları önlemek için textboxlara sadece sayı girilmesini sağlamak için gerekli kodu yazalım:
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
int n = 1, r = 1; double sonuc = 1;
try { if (txtn.Text != "" && txtr.Text != "") { n = int.Parse(txtn.Text); r = int.Parse(txtr.Text); if (n >= r) { sonuc = double.Parse(n.ToString()); for (int i = 1; i < r; i++) { sonuc *= double.Parse((n - i).ToString()); } lblSonuc.Text = sonuc.ToString(); } else { MessageBox.Show("r, n'den büyük olamaz..!", "Uyarı..!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else if (txtn.Text == "") { MessageBox.Show("n sayısını giriniz...", "Uyarı..!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (txtr.Text == "") { MessageBox.Show("r sayısını giriniz...", "Uyarı..!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception hata) { MessageBox.Show(hata.Message.ToString(), "Hata..!", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { n = 1; r = 1; }
int n = 1, r = 1; double sonuc = 1; private void btnPermutasyonHesapla_Click(object sender, EventArgs e) { try { if (txtn.Text != "" && txtr.Text != "") { n = int.Parse(txtn.Text); r = int.Parse(txtr.Text); if (n >= r) { sonuc = double.Parse(n.ToString()); for (int i = 1; i < r; i++) { sonuc *= double.Parse((n - i).ToString()); } lblSonuc.Text = sonuc.ToString(); } else { MessageBox.Show("r, n'den büyük olamaz..!", "Uyarı..!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else if (txtn.Text == "") { MessageBox.Show("n sayısını giriniz...", "Uyarı..!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (txtr.Text == "") { MessageBox.Show("r sayısını giriniz...", "Uyarı..!", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception hata) { MessageBox.Show(hata.Message.ToString(), "Hata..!", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { n = 1; r = 1; } } private void txtn_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar); }
Yorum yazabilmek için üye girişi yapmanız gerekiyor!