Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Scientific Calculator

Rate me:
Please Sign up or sign in to vote.
3.56/5 (44 votes)
11 Mar 2008CPOL 145.4K   10.2K   52  
Scientific calculator that calculates fibonacci modulo factorial sin cos tan.. you are able to also change background color and color of the buttons
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Calculator_GUI
{
    class Operations
    {
        private static double result; // create result

        public Operations()
        {
            result = 0; // initialize result to '0'
        }

        // addition function
        public static void add(double num)
        {
            if(result == 0)
            {
                result = num; // save first number in result
            }
            else
                 result += num; // adding second. third.... numbers to result
        }

        // subtraction function
        public static void sub(double num)
        {
            if(result == 0)
            {
                result = num; // save first number in result
            }
            else
            {
                result -= num;
            }
        }
 
        // starting with minus ex: -3-3
        public static void Ssub(double num)
        {
            if(result == 0)
            {
                result = num; // save first number in result
            }
            else
            {
                result -= num;
            }
        }

        // multiplication method
        public static void mult(double num)
        {
            if(result == 0)
            {
                result = num; // save first number in result
            }
            else
                result *= num;
        }

        // division method
        public static void div(double num)
        {
            if (result != 0 && num == 0)
            {
                MessageBox.Show("Cannot divide by 0");
            }               
            if(result == 0)
            {
                result = num; // save first number in result
            }
            else
                result /= num;
        }

        // sin method
        public static void sin(double num)
        {
            if(result == 0)
            {
                result = Math.Sin(num);
            }
            else
                result = Math.Sin(result);          
        }

        // cos method
        public static void cos(double num)
        {
            if(result == 0)
            {
                result = Math.Cos(num);
            }
            else
                result = Math.Cos(result);
        }

        // tan method
        public static void tan(double num)
        {
            if(result == 0)
            {
                result = Math.Tan(num);
            }
            else
                result = Math.Tan(result); 
        }

        // square root method
        public static void sqrt(double num)
        {
            if(result == 0)
            {
                result = Math.Sqrt(num);
            }
            else
                result = Math.Sqrt(result);
        }

        // modulus method
        public static void modulo(double num)
        {
            if(result == 0)
            {
                result = num;
            }
            else
            {
                result %= num;
            }
        }

        // square method
        public static void square(double num)
        {
                result = num * num;
        }

        // cube method
        public static void cube(double num)
        {
            result = num * num * num;
        }

        // 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
            }
        }

        // fibonacci method
        public static long fibonacci(long num)
        {
            if(num >= 40 || num < 0)
            {
                MessageBox.Show("Enter number between 0 & 40", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }
            else
            {
                if(num == 0 || num == 1)
                    return num;
                else
                    return fibonacci(num - 1) + fibonacci(num - 2);
            }
        }

        public static void clear()
        {
            result = 0; // clear result
        }

        public static double getResult()
        {
            return result; // get result
        }

    } // end class Operations
} // end namespace Calculator_GUI

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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