C# Hesap Makinesi Kodları ve Adım Adım Açıklaması

07-01-2019 16:04
C# Hesap Makinesi Kodları ve Adım Adım Açıklaması

C# hesap makinesi yapımı, programlamayı yeni öğrenenler için başlangıç pratikleri arasında yer alıyor. Basitçe yapabileceğiniz hesap makinesi için Visual Studio’da ilk yazdığınız C# kodlarını bir araya getirmeniz yeterli oluyor.


Şimdi, birlikte bir hesap makinesi yapalım.


Adım 1: Visual Studio’da Form oluşturmak


C# için en yaygın kullanılan program olan Visual Studio’yu hala bilgisayarınıza yüklemediyseniz buradan indirebilirsiniz. Az da olsa İngilizce biliyorsanız, dilini İngilizce kullanmanızı öneririz. Visual Studio’nun Türkçe versiyonu da bulunuyor. Visual Studio’nun Mac için uygun sürümü olsa da C#, Mac bilgisayarlarda çalışmıyor. Windows işletim sistemine ihtiyacınız var.


Visual Studio’yu açtıktan sonra New Project’i seçerek Windows Forms Application oluşturun. Ekranda karşınıza boş bir Form1 kutucuğu çıkacak.


Adım 2: Ögeleri yerleştirmek

 

Ekranın sol tarafında Toolbox (Araçlar), sağ tarafındaysa Properties (Özellikler) kısımlarını görüyorsunuz. Sağda Form1’in özelliklerinden formunuzun adını Hesap Makinesi olarak değiştirebilir, isterseniz bir ikon yerleştirebilirsiniz. Hesap makinesinin düzenlenmesi için özellikler bölümünde bazı değişiklikler yapabilirsiniz. Örneğin, StartPosition özelliğini CenterScreen olarak değiştirirseniz hesap makinenizi çalıştırdığınızda ekranın ortasında çıkacaktır. FormBorderStyle özelliğini FixedDialog olarak değiştirmek hesap makinenizin boyutunu sabitlemenize yarar.


Ancak bu yazıda hesap makinesinin kodlarına ağırlık vereceğimiz için bu özellikleri şimdilik kısa kesiyoruz.


Sol taraftaki Toolbox’tan sürükle - bırak yaparak bir TextBox, ve bir Button ekleyin ve butonun boyutunu, yazı tipini, yazı boyutunu istediğiniz şekilde düzenleyin. Daha sonra butonu kopyala - yapıştır yaparak aşağıdaki şekilde yerleştirin. Butonların üzerinde yazan rakam ve sembolleri Text özelliğini değiştirerek yazabilirsiniz. Sonunda bir hesap makinesi görüntüsü elde etmeniz gerekiyor.



Adım 3: Kodlama

 

Şimdi sıra geldi kod yazma kısmına. C# bilgi ve becerilerinizi geliştirdikçe farklı hesap makinesi kodları keşfedebilir ya da deneyebilirsiniz. Biz bu yazıda, en basit C# hesap makinesi kodlarını sizinle paylaşacağız. Visual Studio’da kod yazmak için ögelere çift tıklamanız yeterlidir. Ögelere tıklandığında ne yapılmasını, bu ögede hangi işlemin gerçekleşmesini istiyorsanız bunun kodunu yazacağınız alan açılır.


Aşağıda Button1, Button2 şeklinde devam eden kısım sizin butonları sıralamanıza göre değişiklik gösterebilir. Buton ile kodu doğru şekilde eşleştirdiğinizden emin olun. Kodlama hata yapmadan öğrenilmez! Hata yapmaktan korkmayın.


C# öğrenmeye kararlıysanız Fahrettin Erdinç’in hazırladığı C# ile Nesne Tabanlı Programlama adlı kitap ile yolunuzda daha emin adımlarla ilerleyebilirsiniz. Eğitim videolarıyla desteklenen kitap programlamayı sıfırdan öğrenmek isteyenler için harika bir kaynak.



 

Bizim yazdığımız C# hesap makinesi kodları şöyle:


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 WindowsFormsApp2

{

public partial class Form1 : Form

{

double FirstNumber;

string Operation;

public Form1()

{

InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text==null)

{

textBox1.Text = "1";

}

else

{

textBox1.Text = textBox1.Text + "1";

}

}


private void button2_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "0";

}

else

{

textBox1.Text = textBox1.Text + "0";

}

}


private void button3_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "2";

}

else

{

textBox1.Text = textBox1.Text + "2";

}

}


private void button4_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "3";

}

else

{

textBox1.Text = textBox1.Text + "3";

}

}


private void button5_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "4";

}

else

{

textBox1.Text = textBox1.Text + "4";

}

}


private void button6_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "5";

}

else

{

textBox1.Text = textBox1.Text + "5";

}

}


private void button7_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "6";

}

else

{

textBox1.Text = textBox1.Text + "6";

}

}


private void button8_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "7";

}

else

{

textBox1.Text = textBox1.Text + "7";

}

}


private void button9_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "8";

}

else

{

textBox1.Text = textBox1.Text + "8";

}

}


private void button10_Click(object sender, EventArgs e)

{

if (textBox1.Text == "0" && textBox1.Text == null)

{

textBox1.Text = "9";

}

else

{

textBox1.Text = textBox1.Text + "9";

}

}


private void button11_Click(object sender, EventArgs e)

{

textBox1.Text = textBox1.Text + ",";

}


private void button12_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "+";

}


private void button13_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "-";

}


private void button14_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "*";

}


private void button15_Click(object sender, EventArgs e)

{

FirstNumber = Convert.ToDouble(textBox1.Text);

textBox1.Text = null;

Operation = "/";

}


private void button16_Click(object sender, EventArgs e)

{

double SecondNumber;

double Result;


SecondNumber = Convert.ToDouble(textBox1.Text);


if (Operation=="+")

{

Result = (FirstNumber + SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

if (Operation == "-")

{

Result = (FirstNumber - SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;


}

if (Operation == "*")

{

Result = (FirstNumber * SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

if (Operation == "/")

if(SecondNumber==0)

{

textBox1.Text = "Uygulanamaz";

}

else

{

Result = (FirstNumber / SecondNumber);

textBox1.Text = Convert.ToString(Result);

FirstNumber = Result;

}

}


private void button17_Click(object sender, EventArgs e)

{

textBox1.Text = null;

}

}

}

 Konu hakkında daha fazla bilgi sahigi sahibi olmak isterseniz c# ile Nesne Tabanlı Programlama  kitabımızı inceleyebilirsiniz


IdeaSoft® | E-Ticaret paketleri ile hazırlanmıştır.