harshityadav95
1/24/2018 - 12:04 PM

code.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Infragistics.Win.Misc;

namespace calculatorApp
{
    public partial class Form1 : Form
    {
       public string _inputmainBox=null;
       public double _value1, _value2;
       public double  _flag=0;
       public string _operand = null;



        public Form1()
        {
            InitializeComponent();
            inputBoxMain.Select();
            
            
        }
        /// <summary>
        /// A single Function to handel all button press events from UI  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnClick(object sender, EventArgs e)
        {

            UltraButton button = sender as UltraButton;
            try
            {


                try
                {
                    int a = Convert.ToInt32(button.Text);
                    if (a >= 0 && a <= 9)
                    {
                        inputBoxMain.AppendText(button.Text);
                    }
                }
                catch (Exception)
                {
                        
                        
						if(button.Text=="+" || button.Text=="-" || button.Text=="*" || button.Text=="/" || button.Text=="%" )
						{
							ValueProcessor(button.Text);
                                _flag += 1;
						}
						else if (button.Text=="±" || button.Text=="√" || button.Text=="1/x")
						{
								ValueProcessor(button.Text);
                                _flag = 2;
                                ValueProcessor(button.Text);
							
						}
						else if(button.Text==".")
						{
							 inputBoxMain.AppendText(".");
						}
						else if(button.Text=="=")
						{
							  if (_flag != 0)
                                {
                                    ValueProcessor(button.Text);
                                }

						}
						
                                                             
          
                }

                
            }
            catch (Exception)
            {

                inputBoxMain.Text = "Invalid Input";
            }
            
        }

     /// <summary>
     /// Analysing the Input from the Keyboard for particular key combinations'
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>

        private void ultraTextEditor1_KeyPress(object sender, KeyPressEventArgs e)
        {
           
                if (e.KeyChar == '+' || e.KeyChar == '-' || e.KeyChar == '*' || e.KeyChar == '/' || e.KeyChar == '%')
                {
                    ValueProcessor(Convert.ToString(e.KeyChar));
                    _flag += 1;
                    e.Handled = true;

                }
                else if (e.KeyChar == '=' || e.KeyChar == '\r')
                {
                    if (_flag != 0)
                    {
                        ValueProcessor("=");
                    }

                    e.Handled = true;

                }
              

            }
          
    
        /// <summary>
        /// The main Calculation engine which stores input in variables and display result
        /// </summary>
        /// <param name="sim"></param>
        public void ValueProcessor(string sim)
        {
            try
            {
                if (_flag == 0)
                {
                    
                    _value1 = Convert.ToDouble(inputBoxMain.Text);
                    if (sim != "=" )
                    {
                        if (sim != "±" && sim != "√" && sim != "1/x")
                        {
                            inputBoxMain.AppendText(sim);
                        }
                        
                        _operand = sim;
                    }
                    if (sim != "±")
                    {
                        overBox.Text = inputBoxMain.Text;
                        inputBoxMain.Clear();
                    }
                    

                }
                else if (_flag == 1)
                {
                    _value2 = Convert.ToDouble(inputBoxMain.Text);
                    _flag += 1;
                }
            }
            catch (Exception)
            {

                inputBoxMain.Text = "Invalid Input";
            }
           
             if (_flag == 2)
            {
                if (_operand != null)
                {
                    operations ob = new operations();
                    try
                    {
                        try
                        {
                            double temp = ob.engine(_operand,_value1, _value2);
                            inputBoxMain.Text = Convert.ToString(temp);
                            overBox.Text = string.Empty;



                        }
                        catch (DivideByZeroException)
                        {
                            inputBoxMain.Text = "Zero Divison";
                      

                        }
                        if (sim != "=" && sim != "±" && sim != "√" && sim != "1/x")
                        {
                            _value1 = Convert.ToDouble(inputBoxMain.Text);
                            inputBoxMain.AppendText(sim);
                            _operand = sim;
                            overBox.Text = inputBoxMain.Text;
                            inputBoxMain.Clear();

                            _flag =0;
                            _value2 = 0;
                        }
                        else
                        {
                            _flag = 0;
                            _value2 = 0;
                            _operand = null;
                        }
                             
                       
                      
                        
                    }
                    catch (Exception)
                    {

                        inputBoxMain.Text = "Engine Failed";
                    }

                }
            }
                
            
        }

      /// <summary>
      /// Complete clear button function
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>

        private void clear_Click(object sender, EventArgs e)
        {
            inputBoxMain.Text = string.Empty;
            overBox.Text = string.Empty;
            _flag = 0;
            _value2 = 0;
           _value1 = 0;
            _operand = null;
          
        }
      /// <summary>
      /// CE Button click method
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        private void operatorClearLast_Click(object sender, EventArgs e)
        {
            inputBoxMain.Text = string.Empty;
        }
        /// <summary>
        /// Backspace Key from Buttons UI  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Backspacekey_Click(object sender, EventArgs e)
        {
            string temp = inputBoxMain.Text;
            temp = temp.Remove(temp.Length - 1);
            inputBoxMain.Text = temp;
        }

       
    }
}