Saturday, July 10, 2010

C# Tutorial: Standard Calculator

Here is the code

//System Media //
using System.Media;
//Declaration of variables //
int clear = 1; decimal result, mresult = 0; string op;
//Eval function //
decimal eval(string op)
{
      clear = 1;
      try
      {
           switch (op)
           {
                case "+": result = result + Convert.ToDecimal(label1.Text); break;
                case "-": result = result - Convert.ToDecimal(label1.Text); break;
                case "/": result = result / Convert.ToDecimal(label1.Text); break;
                case "*": result = result * Convert.ToDecimal(label1.Text); break;
                case "Mod": result = result % Convert.ToDecimal(label1.Text); break;
                default: result = Convert.ToDecimal(label1.Text); break;
            }
       }
       catch (System.OverflowException) { label2.Text = ""; label2.Text = "Overflow"; clear = 2; SystemSounds.Asterisk.Play(); }
       catch (System.DivideByZeroException) { label2.Text = ""; label2.Text = "Cannot divide by zero"; clear = 2; SystemSounds.Asterisk.Play(); }
       return result; 
}
//Clear function //
int Clear(int cl)
{
    switch (cl)
    {
        case 1:
        {
             label1.Text = "";
        }break;
        case 2:
        {
            label1.Text = ""; label2.Text = ""; op = "";
        }break;
        case 3:
        {
            label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('r'));  
        }break;
        case 4:
        {
            label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('s'));
        }break;
        case 5:
        {
            label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.LastIndexOf(' ') + 1);
        }break;
    }
    return 0;
}
//Button 0,1,2,3,4,5,6,7,8,9(replace the 0 with the other numbers) //
clear = Clear(clear);
if (label1.Text.Length < 28)
      label1.Text = label1.Text + "0";
else SystemSounds.Beep.Play();
//Button "." //
if (label1.Text.Contains('.'))
{
      SystemSounds.Beep.Play();
}
else label1.Text = label1.Text + ".";
//Button +,-,/,*,Mod(replace the + with the other operators) //
if (clear == 3 || clear == 4 || clear == 5)
      label2.Text = label2.Text + " + ";
else label2.Text = label2.Text + label1.Text + " + ";
label1.Text = eval(op).ToString();
op = "+";
//Button 1/x //
if (label2.Text.Contains("reciproc"))
{
      label2.Text = label2.Text.Insert(label2.Text.IndexOf('r'), "reciproc("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
}
else label2.Text = label2.Text + "reciproc(" + label1.Text + ")";
label1.Text = (1 / Convert.ToDecimal(label1.Text)).ToString(); clear = 3; 
//Button √ //
if (label2.Text.Contains("sqrt"))
{
     label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqrt("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
}
else label2.Text = label2.Text + "sqrt(" + label1.Text + ")";
label1.Text = Math.Sqrt(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
//Button % //
label1.Text = (result * Convert.ToDecimal(label1.Text) / 100).ToString();
label2.Text = label2.Text + label1.Text; clear = 5;
//Button ± //
if (label1.Text.Contains('-'))
{
     label1.Text = label1.Text.Remove(label1.Text.IndexOf('-'), 1);
}
else label1.Text = "-" + label1.Text;
//Button = //
if (clear != 3 && clear != 4 && clear != 5)
        label2.Text = label2.Text + label1.Text;
label1.Text = eval(op).ToString(); clear = 2;
//Button C //
Clear(1); clear = 1; label1.Text = "0";
//Button CE //
Clear(2); clear = 1; label1.Text = "0"; result = 0;
//Button MS //
mresult = Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
//Button MR //
label1.Text = mresult.ToString(); clear = 1;
//Button M+ //
mresult = mresult + Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
//Button M- //
mresult = mresult - Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
//Button MC //
mresult = 0; clear = 1; label3.Text = "";
//Label1 TextChanged Event //
if (label1.Text.Length <= 20)
{
     Font font = new Font("Consolas", 14, FontStyle.Regular); label1.Font = font;
}
else if (label1.Text.Length > 20 && label1.Text.Length <= 26)
{
     Font font = new Font("Consolas", 11, FontStyle.Regular); label1.Font = font;
}
else { Font font = new Font("Consolas", 9, FontStyle.Regular); label1.Font = font; }

8 comments:

  1. Do u have the solution available?

    ReplyDelete
  2. there is one bug in this code that is when we click any operator consecutively it doesnt disable the operation instead of that it keeps on doing calculation by taking previous result
    for eg i have key clicked as 12 ,+,5,+,+,+;
    then answer should only be 17 but it is not
    it keeps on adding gives the sum as 68

    ReplyDelete
    Replies
    1. This is not a bug, it is the functionality of normal calculator.
      Please check and comment.

      Delete
  3. Quite late, but I do have one readily available!

    DOWNLOAD IT NOW FOR FREE

    ReplyDelete
  4. i want to update new button like "x^y" (Math.Pow(x,y)),
    so can you help me?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. if you want i may help you:

      try this:

      Add this to your eval function:
      case "^": result = (decimal)(Math.Pow((double)result,(double)Convert.ToDecimal(label1.Text))); break;

      add Pow's Button:
      private void button29_Click(object sender, EventArgs e)// pow button
      {
      if (clear == 3 || clear == 4 || clear == 5)
      label2.Text = label2.Text + " ^ ";
      else label2.Text = label2.Text + label1.Text + " ^ ";
      label1.Text = eval(op).ToString();
      op = "^";
      }

      Delete
  5. where i can download this calculator?

    ReplyDelete