Simple Calculator

Simple Calculator using a Class in C#

Details: Simple Calculator

A calculator is made up of several parts, such as the user interface, number system, and functions. The user interface can be represented by the class interface which defines the look and feel. The number system defines how numbers are displayed— as numbers or as text. Lastly, functions perform calculations on data and store the results.

In this post, we will illustrate using C# programming language how to create a program that can compute the sum, difference, product and quotient as a basic calculator using class.

Guide: Simple Calculator

1 – To proceed, we start by creating a Windows Form Application using Microsoft Visual Studio following the steps below
– Navigate to File
– Click on New Project
– Select Windows Application.

2 – Now add the following;
– A TextBox labelled TextBox1 for inputting number
– Another TextBox labelled TextBox2 for inputting a second number
– A third TextBox labelled TextBox3 for displaying the total.
– Also, insert 4 RadioButton;
– Radio_Addition for addition function
– Radio_Substraction for substration function
– Radio_Division for division function
– Radion_Multiplication for multiplication function.
Your interface should look like the image layout below.

3 – Now proceed to create a class and name it “calculate” and insert the code below.

				
					using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Collections.Generic;
using System.Linq;
 
 
namespace MDAS
{
    public class calculate
    {
        //declare a private variable as a double
        private double _num1;
        private double _num2;
        private double _total;
 
        //set a readonly property for the total of the MDAS
        public double total
        {
            get
            {
                return _total; //return the total
            }
        }
        //create a property for a private variable so that you can access it
        public double num1
        {
            get
            {
                return _num1; //return the first value
            }
            set
            {
                _num1 = value; //set the first value
            }
        }
        public double num2
        {
            get
            {
                return _num2;
            }
            set
            {
                _num2 = value;
            }
        }
 
        //create the sub procedures of the MDAS
        public void multiply()
        {
            //formula of multiplication
            _total = num1 * num2;
        }
        public void divide()
        {
            //formula of division
            _total = num1 / num2;
        }
        public void add()
        {
            //formula of addition
            _total = num1 + num2;
        }
        public void subtract()
        {
            //formula of subtraction
            _total = num1 - num2;
        }
 
 
    }
 
}
				
			

4 – For the calculator operations, navigate to your Form and insert this code below.

				
					using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Collections.Generic;
using System.Linq;
 
 
 
namespace MDAS
{
    public partial class Form1
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        //create a sub procedure for the events of clicking the radio button that handles all of it.
        public void RadioButton_Click(object sender, System.EventArgs e)
        {
 
            //call a constructor method and return to cal as an instance of a class
            calculate cal = new calculate();
 
            //declaring the string variable represent as a textbox
            string txtnum1 = TextBox1.Text;
            string txtnum2 = TextBox2.Text;
            //declaring the double variable
            double dbl_val1 = default(double);
            double dbl_val2 = default(double);
 
            if (Information.IsNumeric(txtnum1) && Information.IsNumeric(txtnum2)) //check if the textbox has a numeric value
            {
                //convert the string to double
                dbl_val1 = double.Parse(txtnum1);
                dbl_val2 = double.Parse(txtnum2);
 
                //get the value of the converted variable
                //to pass it into the variable in the class
                cal.num1 = dbl_val1;
                cal.num2 = dbl_val2;
 
                //the condition is, if the radiobutton is clicked,
                //the operation of MDAS executes.
                if (Radio_Multiplication.Checked)
                {
                    //result:
                    cal.multiply(); //call a subname in a class for multiplying
                }
                else if (Radio_Division.Checked)
                {
                    //result:
                    cal.divide(); //call a subname in a class for dividing
                }
                else if (Radio_Addition.Checked)
                {
                    //result:
                    cal.add(); //call a subname in a class for adding
                }
                else if (Radio_Subtaction.Checked)
                {
                    //result:
                    cal.subtract(); //call a subname in a class for subtracting
                }
            }
            else
            {
                //the result is:
                //if the textbox is empty or has a string value
                TextBox3.Text = "Enter a number";
                return ;
            }
            //put the result of the MDAS to a textbox.
            TextBox3.Text = cal.total.ToString();
        }
 
 
    }
 
}
				
			

Outputs: Simple Calculator

Simple Calculator using a Class in C#, free source code, c#, programmning, language, free download, source code

Don’t forget to share this post!

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Article