C# Hesap Makinesi Yapımı Form Uygulaması
Merhaba arkadaşlar bugün sizlere C#’da basit bir hesap makinesi nasıl yapılır onu göstericem.
ilk önce Visual Studio programımızı açalım ardından “File > New > Project > Form Application” menüsünü takip edip projemize isim verip oluşturalım.
Alttaki örnekde tasarım verilmiştir. Textbox’ımızın MultiLine özelliğini aktif edip boyutunu büyüttük(2 satır olacak şekilde) sebebini birazdan anlatıcam.
Tasarımı verdiğimize göre artık kodlarımıza geçelim.
Aşağıdaki kodumuzda ilk if bloğunda henüz bir toplama çıkartma çarpma bölme işlemi yapıp yapmadığımızı kontrol ediyoruz, ardından 2. if bloğumuzda karakter sınırımızı aşıp aşmadığını kontrol ediyoruz ve ekrana yazdırıyoruz. Eğer ilk if bloğu sonucunda kontrol ‘0’ dan büyük bir sayı ise else çalışır ve değerleri önce başlangıç değerlerine geri çevirip daha sonra ekrana yazdırma işlemini yapar.
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "1"; alt += "1"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "1"; alt += "1"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "2"; alt += "2"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "2"; alt += "2"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "3"; alt += "3"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "3"; alt += "3"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "4"; alt += "4"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "4"; alt += "4"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "5"; alt += "5"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "5"; alt += "5"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "6"; alt += "6"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "6"; alt += "6"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "7"; alt += "7"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "7"; alt += "7"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "8"; alt += "8"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "8"; alt += "8"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "9"; alt += "9"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "9"; alt += "9"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "0"; alt += "0"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "0"; alt += "0"; }
if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + ","; alt += ","; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + ","; alt += ","; }
Not: Aşağıdaki Environment.NewLine kodu bir alt satıra geçmek için kullanılmıştır.
if (alt.Length == 0) { //eğer boş ise silme işlemi hata verecektir. } else { textBox1.Text = ust + Environment.NewLine + alt.Substring(0, alt.Length - 1); alt = alt.Substring(0, alt.Length - 1); }
textBox1.Clear(); islem = 0; ust = ""; alt = "";
kontrol++; islem += float.Parse(alt); if(ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; } else { string duzenle = ust + "+" + alt; if(duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "+" + alt; }
kontrol++; if (ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; islem = float.Parse(alt); } else { islem -= float.Parse(alt); string duzenle = ust + "-" + alt; if (duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "-" + alt; }
kontrol++; if (ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; islem = float.Parse(alt); } else { islem *= float.Parse(alt); string duzenle = ust + "x" + alt; if (duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "x" + alt; }
kontrol++; if (ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; islem = float.Parse(alt); } else { islem /= float.Parse(alt); string duzenle = ust + "/" + alt; if (duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "/" + alt; }
Kodlar bu şekilde arkadaşlar alt tarafta projenin kodlarını bütün olarak görmekteyiz.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HesapMakinesi { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string ust = "", alt = ""; int sy = 0, kontrol = 0; float islem = 0; private void altsatir(object sender, EventArgs e) { } private void Cancel_Click(object sender, EventArgs e) { textBox1.Clear(); islem = 0; ust = ""; alt = ""; } private void b2_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "2"; alt += "2"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "2"; alt += "2"; } } private void b3_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "3"; alt += "3"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "3"; alt += "3"; } } private void b4_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "4"; alt += "4"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "4"; alt += "4"; } } private void b5_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "5"; alt += "5"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "5"; alt += "5"; } } private void b6_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "6"; alt += "6"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "6"; alt += "6"; } } private void b7_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "7"; alt += "7"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "7"; alt += "7"; } } private void b8_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "8"; alt += "8"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "8"; alt += "8"; } } private void b9_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "9"; alt += "9"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "9"; alt += "9"; } } private void b0_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "0"; alt += "0"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "0"; alt += "0"; } } private void virgul_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + ","; alt += ","; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + ","; alt += ","; } } private void b1_Click(object sender, EventArgs e) { if (kontrol == 0) { if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "1"; alt += "1"; } else { kontrol = 0; alt = ""; if (ust.Length > 15) { sy = ust.Length - 15; } textBox1.Text = ust.Substring(sy) + Environment.NewLine + alt + "1"; alt += "1"; } } private void Topla_Click(object sender, EventArgs e) { kontrol++; islem += float.Parse(alt); if(ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; } else { string duzenle = ust + "+" + alt; if(duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "+" + alt; } } private void Cikar_Click(object sender, EventArgs e) { kontrol++; if (ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; islem = float.Parse(alt); } else { islem -= float.Parse(alt); string duzenle = ust + "-" + alt; if (duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "-" + alt; } } private void Carp_Click(object sender, EventArgs e) { kontrol++; if (ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; islem = float.Parse(alt); } else { islem *= float.Parse(alt); string duzenle = ust + "x" + alt; if (duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "x" + alt; } } private void sil_Click(object sender, EventArgs e) { if (alt.Length == 0) { //eğer boş ise silme işlemi hata verecektir. } else { textBox1.Text = ust + Environment.NewLine + alt.Substring(0, alt.Length - 1); alt = alt.Substring(0, alt.Length - 1); } } private void Form1_Load(object sender, EventArgs e) { } private void Bol_Click(object sender, EventArgs e) { kontrol++; if (ust == "") { textBox1.Text = alt + Environment.NewLine + alt; ust += alt; islem = float.Parse(alt); } else { islem /= float.Parse(alt); string duzenle = ust + "/" + alt; if (duzenle.Length >= 15) { sy = duzenle.Length - 15; } textBox1.Text = duzenle.Substring(sy) + Environment.NewLine + islem; ust += "/" + alt; } } } }
Projenin Örneğini Buraya Tıklayarak İndirebilirsiniz.