Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#
Article

Scientific Calculator

Rate me:
Please Sign up or sign in to vote.
3.56/5 (44 votes)
11 Mar 2008CPOL 146.2K   10.2K   52   14
Scientific calculator that calculates fibonacci modulo factorial sin cos tan.. you are able to also change background color and color of the buttons
Scientific_Calculator

Introduction

This is a scientific calculator that calculates fibonacci modulo factorial sin cos tan.. you are also able to change the background color and color of the buttons.

Using the Code

C#
// using button number 1
private void button1_Click(object sender, EventArgs e)
{
    but_backspace.Enabled = true;        // number button is clicked, enable backspace
    but_backspace.BackColor = Color.LightCoral; // color button when enabled

    if (isOperation == true)          // if we used an operation clear output
        {textBox_output.Text = "";}   // empty output

    if (tempSign == "Sminus")
        { textBox_output.Text += "-1"; }

    else
        textBox_output.Text += "1";    // add 1 to output
        isOperation = false;           // no operation pressed yet
}

// using addition button
private void button12_Click(object sender, EventArgs e)
{
    if (textBox_output.Text == "")
    { // to prevent exception appearing
    }
    else
    {
        Operations.add(double.Parse(textBox_output.Text));
        textBox_output.Text = Convert.ToString(Operations.getResult());
        isOperation = true; // user pressed an operation
        tempSign = "plus";
    }
}

// factorial method
public static long factorial(long num)
{
    if(num >= 21 || num < 0)
    {
        MessageBox.Show("Enter number between 0 & 20",
        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return 0;
    }
    else
    {
        if(num <= 1)
            return 1;
        else
            return num * factorial(num - 1); // recursive function
    }
}

History

  • 11th March, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Liberia Liberia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUseful Pin
Ethnica6-Nov-09 21:18
Ethnica6-Nov-09 21:18 
I really like the coding. Well explained and really useful. I have a suggestion for the numbers.
Instead of using same code piece for each number button by changing the numbers, you could define a method and add a parameter as a value type(numbers).
A sample from your code;

private void button7_Click(object sender, EventArgs e)
        {
            but_backspace.Enabled = true;               // number button is clicked, enable backspace
            but_backspace.BackColor = Color.LightCoral; // color button when it enable
            
            if (isOperation == true)                    // if we used an operation clear output
            {textBox_output.Text = "";}                 // empty output

            if (tempSign == "Sminus")
            { textBox_output.Text += "-7"; }

            else
            textBox_output.Text += "7";                 // add 7 to output
            isOperation = false;                        // no operation pressed yet


with a method;

private void numberClick(string outputVal)
        {
            but_backspace.Enabled = true;               // number button is clicked, enable backspace
            but_backspace.BackColor = Color.LightCoral; // color button when it enable

            if (isOperation == true)                    // if we used an operation clear output
            { textBox_output.Text = ""; }                 // empty output

            if (tempSign == "Sminus")
            { textBox_output.Text += "-"+outputVal; }

            else
                textBox_output.Text += outputVal;                 // add number to output
            isOperation = false;                        // no operation pressed yet
        }


And all you need to do, after you defined the method to use your method for each number block.

Like this;

private void button7_Click(object sender, EventArgs e)
        {
            numberClick("7");
        }

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.