using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Calc
{
public partial class Form1 : Form
{
private string m_value;
private ArrayList m_store;
public Form1()
{
InitializeComponent();
m_value = "";
m_store = new ArrayList();
}
private void AddOperatorToArray(Button bttn)
{
m_store.Add(m_value);
txtResult.Text += bttn.Text;
m_value = "";
m_store.Add(bttn.Text);
btnDot.Enabled = true;
SetEnableOperatorBttns(false);
}
private void SetEnableOperatorBttns(bool enable)
{
btnPlus.Enabled = enable;
btnMinus.Enabled = enable;
btnMulti.Enabled = enable;
btnDivide.Enabled = enable;
btnEqual.Enabled = enable;
}
private void AddToArray(Button bttn)
{
m_value += bttn.Text;
txtResult.Text += bttn.Text;
SetEnableOperatorBttns(true);
}
private void Reset()
{
m_value = "";
txtResult.Text = "";
m_store.Clear();
btnDot.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn1_Click(object sender, EventArgs e)
{
AddToArray(btn1);
}
private void btn2_Click(object sender, EventArgs e)
{
AddToArray(btn2);
}
private void btn3_Click(object sender, EventArgs e)
{
AddToArray(btn3);
}
private void btn4_Click(object sender, EventArgs e)
{
AddToArray(btn4);
}
private void btn5_Click(object sender, EventArgs e)
{
AddToArray(btn5);
}
private void btn6_Click(object sender, EventArgs e)
{
AddToArray(btn6);
}
private void btn7_Click(object sender, EventArgs e)
{
AddToArray(btn7);
}
private void btn8_Click(object sender, EventArgs e)
{
AddToArray(btn8);
}
private void btn9_Click(object sender, EventArgs e)
{
AddToArray(btn9);
}
private void btn0_Click(object sender, EventArgs e)
{
AddToArray(btn0);
}
private void btnDot_Click(object sender, EventArgs e)
{
AddToArray(btnDot);
btnDot.Enabled = false;
}
private void btnClr_Click(object sender, EventArgs e)
{
Reset();
}
private void btnPlus_Click(object sender, EventArgs e)
{
AddOperatorToArray(btnPlus);
}
private void btnMinus_Click(object sender, EventArgs e)
{
AddOperatorToArray(btnMinus);
}
private void btnMulti_Click(object sender, EventArgs e)
{
AddOperatorToArray(btnMulti);
}
private void btnDivide_Click(object sender, EventArgs e)
{
AddOperatorToArray(btnDivide);
}
private void btnEqual_Click(object sender, EventArgs e)
{
try
{
m_store.Add(m_value);
m_store.Add(btnEqual.Text);
/* m_res here take the first element in m_store if this element is
* operator like +*-/ Exception give us Error else it will take the
* Digits or dot point
*/
float m_res = float.Parse(m_store[0].ToString());
/* this loop extract all element in Array then check it
* if the element is operator it will calculate the prefix
* and postfix and give us the result
* Example 1+2/3*5
* 1) 1+2 = 3
* 2) 3/3 = 1
* 3) 1*5 = 5
* note:- no periority operator and no brakets so it's basic calculator:)
*/
for (int i = 0; i < m_store.Count; i++)
{
if (m_store[i].ToString() == "+")
{
txtResult.Text = "";
m_res += float.Parse(m_store[i + 1].ToString());
txtResult.Text = m_res.ToString();
}
else if (m_store[i].ToString() == "-")
{
txtResult.Text = "";
m_res -= float.Parse(m_store[i + 1].ToString());
txtResult.Text = m_res.ToString();
}
else if (m_store[i].ToString() == "*")
{
txtResult.Text = "";
m_res *= float.Parse(m_store[i + 1].ToString());
txtResult.Text = m_res.ToString();
}
else if (m_store[i].ToString() == "/")
{
txtResult.Text = "";
m_res /= float.Parse(m_store[i + 1].ToString());
txtResult.Text = m_res.ToString();
}
}
m_store.Clear();
m_value = txtResult.Text;
for (int i = 0; i < m_value.Length; i++)
{
if (m_value[i].ToString() == ".")
{
btnDot.Enabled = false;
break;
}
else
{
btnDot.Enabled = true;
}
}
btnEqual.Enabled = false;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error");
}
}
}
}