
সুপ্রিয় বন্ধুরা, আশা করি ভাল আছেন। আজ আমি আপনাদের দেখাব কিভাবে C# এর মাধ্যমে একটি scientific calculator তৈরি করতে পারবেন।
১. নিচের মত ডিজাইন করুন।
২. Control গুলোর method name এবং text name change করুন নিচের মত।
| Control name | (Name) | Text |
| button1 | button1 | 1 |
| button2 | button2 | 2 |
| button3 | button3 | 3 |
| button4 | button4 | 4 |
| button5 | button5 | 5 |
| button6 | button6 | 6 |
| button7 | button7 | 7 |
| button8 | button8 | 8 |
| button9 | button9 | 9 |
| button10 | button0 | 0 |
| button11 | btnPoint | . |
| button12 | btnExp | Exp |
| button13 | btnDel | Del |
| button14 | btnClear | C |
| button15 | btnPlus | + |
| button16 | btnMinus | - |
| button17 | btnMul | * |
| button18 | btnDiv | / |
| button19 | btnAns | Ans |
| button20 | btnEqual | = |
| button21 | btnSqr | Sqr |
| button22 | btnPower | x^y |
| button23 | btnRoot | Root |
| button24 | btnInv | 1/x |
| button25 | btnSin | Sin |
| button26 | btnCos | cos |
| button27 | btnTan | tan |
| button28 | btnLog | Log |
| button29 | btnln | ln |
| button30 | btnPI | Pi |
| button31 | btnFact | x! |
| textBox1 | txtDisplay | Null |
| Form1 | Form1 | Scientific Calculator |
৩. Button1 এ double click করুন ও নিচের code লিখুন।
{
txtDisplay.Text = txtDisplay.Text + button1.Text;
}
এর অর্থ হল আপনি button1 এ যতবার click করবেন ততবার button1 এ টেক্সট হিসেবে 1 digit বারবার display তে দেখাবে।
৪.এবার button2 থেকে button 12 পর্যন্ত প্রতিটি button এ double click করে নিচের code গুলো লিখুন।
private void button2_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button3.Text;
}
private void button4_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button4.Text;
}
private void button5_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button5.Text;
}
private void button6_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button6.Text;
}
private void button7_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button7.Text;
}
private void button8_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button8.Text;
}
private void button9_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button9.Text;
}
private void button0_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + button0.Text;
}
private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
}
private void btnExp_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
mulButtonClicked = false;
divButtonClicked = false;
pwrButtonClicked = false;
expButtonClicked = true;
}
}
৫. এই code গুলোর নিচে নিচের code গুলো লিখুন।
double total1 = 0; double total2 = 0; bool plusButtonClicked = false;//bool means that if u dont press the oprator buttons //normally it contain false logic. bool minusButtonClicked = false; bool mulButtonClicked = false; bool divButtonClicked = false; bool pwrButtonClicked = false; bool expButtonClicked = false;
এখানে total1 variable নেয়া হয়ছে button click করার পরে টেক্সট রাখার জন্য আর total2 variable নেয়া হয়েছে result রাখার জন্য। যেমন আপনি button1 এ click করলে 1 value টি total1 variable এ থাকবে। তারপর plus button এ ক্লিক করে button2 তে click করলে button2 এর value total1 এ থাকবে। তারপর equal button এ click করলে যোগফল 3 total2 variable এ জমা হবে।
৬. এবার plus, minus, multiply, division ও equal button গুলোতে double click করুন ও নিচের code গুলো লিখুন।
private void btnPlus_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = true;//true means that if u press plus button then it
//will perform the only plus button logic.
minusButtonClicked = false;
mulButtonClicked = false;
divButtonClicked = false;
pwrButtonClicked = false;
expButtonClicked = false;
}
}
private void btnMinus_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = true;
mulButtonClicked = false;
divButtonClicked = false;
pwrButtonClicked = false;
expButtonClicked = false;
}
}
private void btnMul_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
mulButtonClicked = true;
divButtonClicked = false;
pwrButtonClicked = false;
expButtonClicked = false;
}
}
private void btnDiv_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
mulButtonClicked = false;
divButtonClicked = true;
pwrButtonClicked = false;
expButtonClicked = false;
}
}
private void btnEqual_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked == true)
{
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (mulButtonClicked == true)
{
total2 = total1 * double.Parse(txtDisplay.Text);
}
else if (divButtonClicked == true)
{
total2 = total1 / double.Parse(txtDisplay.Text);
}
else if (pwrButtonClicked == true)
{
total2 = Math.Pow(total1, (double.Parse(txtDisplay.Text)));
}
else if (expButtonClicked == true)
{
total2 = total1 * Math.Pow(10, (double.Parse(txtDisplay.Text)));
}
total1 = 0;
txtDisplay.Text = total2.ToString();
}
৭. এবার clear, del, ans, square, power, root ,inv, log, ln, PI,fact button গুলোতে double ক্লিক করুন ও নিচের কোড গুলো লিখুন।
private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Text = "";
}
private void btnDel_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.Text.Length - 1);
}
private void btnAns_Click(object sender, EventArgs e)
{
double ans;
ans = total2;
txtDisplay.Text = ans.ToString();
}
private void btnSqr_Click(object sender, EventArgs e)
{
double sqr;
if (txtDisplay.Text.Length > 0)
{
sqr = Math.Pow(double.Parse(txtDisplay.Text), 2);
txtDisplay.Text = sqr.ToString();
}
}
private void btnPower_Click(object sender, EventArgs e)
{
if (txtDisplay.Text.Length > 0)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
mulButtonClicked = false;
divButtonClicked = false;
pwrButtonClicked = false;
expButtonClicked = false;
pwrButtonClicked = true;
}
}
private void btnRoot_Click(object sender, EventArgs e)
{
double sqrt;
if (txtDisplay.Text.Length > 0)
{
sqrt = Math.Sqrt(double.Parse(txtDisplay.Text));
txtDisplay.Text = sqrt.ToString();
}
}
private void btnInv_Click(object sender, EventArgs e)
{
double inv;
if (txtDisplay.Text.Length > 0)
{
inv = 1 / (double.Parse(txtDisplay.Text));
txtDisplay.Text = inv.ToString();
}
}
private void btnLog_Click(object sender, EventArgs e)
{
double log;
if (txtDisplay.Text.Length > 0)
{
log = Math.Log10(double.Parse(txtDisplay.Text));
txtDisplay.Text = log.ToString();
}
}
private void btnln_Click(object sender, EventArgs e)
{
double ln;
if (txtDisplay.Text.Length > 0)
{
ln = Math.Log(double.Parse(txtDisplay.Text));
txtDisplay.Text = ln.ToString();
}
}
private void btnPI_Click(object sender, EventArgs e)
{
double pi;
pi = Math.PI;
txtDisplay.Text = pi.ToString();
}
private void btnFact_Click(object sender, EventArgs e)
{
double n, i, f;
f = 1;
if (txtDisplay.Text.Length > 0)
{
n = double.Parse(txtDisplay.Text);
for (i = 1; i <= n; i++)
{
f = f * i;
}
txtDisplay.Text = f.ToString();
}
}
৮. Sin,cos,ও tan button এ double click করুন ও নিচের কোড গুলো লিখুন।
private void btnSin_Click(object sender, EventArgs e)
{
double sin, res;
if (txtDisplay.Text.Length > 0)
{
sin = (double.Parse(txtDisplay.Text) * Math.PI) / 180;
res = Math.Sin(sin);
txtDisplay.Text = res.ToString();
}
}
private void btnCos_Click(object sender, EventArgs e)
{
double cos, res;
if (txtDisplay.Text.Length > 0)
{
cos = (double.Parse(txtDisplay.Text) * Math.PI) / 180;
res = Math.Cos(cos);
txtDisplay.Text = res.ToString();
}
}
private void btnTan_Click(object sender, EventArgs e)
{
double tan, res;
if (txtDisplay.Text.Length > 0)
{
tan = (double.Parse(txtDisplay.Text) * Math.PI) / 180;
res = Math.Tan(tan);
txtDisplay.Text = res.ToString();
}
}
৯. এবার F5 press করে compile করুন।
আমি Salman Srabon। বিশ্বের সর্ববৃহৎ বিজ্ঞান ও প্রযুক্তির সৌশল নেটওয়ার্ক - টেকটিউনস এ আমি 13 বছর 1 মাস যাবৎ যুক্ত আছি। টেকটিউনস আমি এ পর্যন্ত 24 টি টিউন ও 49 টি টিউমেন্ট করেছি। টেকটিউনসে আমার 4 ফলোয়ার আছে এবং আমি টেকটিউনসে 0 টিউনারকে ফলো করি।
ভাই, আজকের ক্যালকুলেটরের প্রোগ্রামটা বুঝলাম না, শুধু একটি বাটনে কোড লিখে কিভাবে ক্যালকুলেটরের অন্য বাটন কাজ করবে, প্লিজ ভাই যদি বুঝিয়ে দিতেন! আর একটি কথা- অনেক দিন পর কিন্তু আপনার পোস্ট পাইলাম, ভাই এ পর্যন্ত কমেন্ট করার দরকার হয় নাই, তাই করি নাই। চালিয়ে গেলে উপকৃত হতাম, আল্লাহ আপনার চলার পথ সুন্দর করুন……