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 System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Stack num_st = new Stack();
Stack oper_st = new Stack ();
char[,] aq = new char[5, 5]
{
{'>','>','<','<','<'},
{'>','>','<','<','<'},
{'>','>','>','>','<'},
{'>','>','>','>','<'},
{'<','<','<','<','='}
};
public Form1()
{
InitializeComponent();
oper_st.Push("#");
}
public int prio(string s)
{
if (s == "+")
return 0;
else if (s == "-")
return 1;
else if (s == "*")
return 2;
else if (s == "/")
return 3;
else
return 4;
}
public double oper(string s, double num1, double num2)
{
if (s == "*")
return num1 * num2;
if (s == "+")
return num1 + num2;
if (s == "-")
return num1 - num2;
if (s == "/")
return num1 / num2;
return 0;
}
public int compare(int x, int xx)
{
if (aq[x, xx] == '>')
return 1;
else if (aq[x, xx] == '<')
return 0;
else
return -1;
}
private void btn5_Click(object sender, EventArgs e)
{
txtDisplay.Text += "5";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn1_Click(object sender, EventArgs e)
{
num_st.Push(txtDisplay.Text);
if (compare(prio(oper_st.Peek().ToString()), prio("+")) == 0)
{
oper_st.Push("+");
txtDisplay.Text = "";
}
else
{
double num2, num1, tempresult;
while (compare(prio(oper_st.Peek().ToString()), prio("+")) != 0)
{
num2 = Convert.ToDouble(num_st.Pop());
num1 = Convert.ToDouble(num_st.Pop());
string operation = oper_st.Pop().ToString();
tempresult = oper(operation, num1, num2);
num_st.Push(tempresult);
}
oper_st.Push("+");
txtDisplay.Text = "";
}
}
private void btn2_Click(object sender, EventArgs e)
{
num_st.Push(txtDisplay.Text);
if (compare(prio(oper_st.Peek().ToString()), prio("-")) == 0)
{
oper_st.Push("-");
txtDisplay.Text = "";
}
else
{
double num2, num1, tempresult;
while (compare(prio(oper_st.Peek().ToString()), prio("-")) != 0)
{
num2 = Convert.ToDouble(num_st.Pop());
num1 = Convert.ToDouble(num_st.Pop());
string operation = oper_st.Pop().ToString();
tempresult = oper(operation, num1, num2);
num_st.Push(tempresult);
}
oper_st.Push("-");
txtDisplay.Text = "";
}
}
private void btn3_Click(object sender, EventArgs e)
{
num_st.Push(txtDisplay.Text);
if ( compare ( prio(oper_st.Peek().ToString()), prio("*") ) == 0 )
{
oper_st.Push("*");
txtDisplay.Text = "";
}
else
{
double num2, num1, tempresult;
while (compare(prio(oper_st.Peek().ToString()), prio("*")) != 0)
{
num2 = Convert.ToDouble(num_st.Pop());
num1 = Convert.ToDouble(num_st.Pop());
string operation = oper_st.Pop().ToString();
tempresult = oper(operation, num1, num2);
num_st.Push(tempresult);
}
oper_st.Push("*");
txtDisplay.Text = "";
}
}
private void btn4_Click(object sender, EventArgs e)
{
num_st.Push(txtDisplay.Text);
if (compare(prio(oper_st.Peek().ToString()), prio("/")) == 0)
{
oper_st.Push("/");
txtDisplay.Text = "";
}
else
{
double num2, num1, tempresult;
while (compare(prio(oper_st.Peek().ToString()), prio("/")) != 0)
{
num2 = Convert.ToDouble(num_st.Pop());
num1 = Convert.ToDouble(num_st.Pop());
string operation = oper_st.Pop().ToString();
tempresult = oper(operation, num1, num2);
num_st.Push(tempresult);
}
oper_st.Push("/");
txtDisplay.Text = "";
}
}
private void btnequal_Click(object sender, EventArgs e)
{
num_st.Push(txtDisplay.Text);
double num2,num1,tempresult;
while (compare(prio(oper_st.Peek().ToString()), prio("=")) != -1)
{
num2 = Convert.ToDouble(num_st.Pop());
num1 = Convert.ToDouble(num_st.Pop());
string operation = oper_st.Pop().ToString();
tempresult = oper(operation, num1, num2);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
}
private void btn04_Click(object sender, EventArgs e)
{
txtDisplay.Text += "4";
}
private void btn01_Click(object sender, EventArgs e)
{
txtDisplay.Text += "1";
}
private void btn02_Click(object sender, EventArgs e)
{
txtDisplay.Text += "2";
}
private void btn03_Click(object sender, EventArgs e)
{
txtDisplay.Text += "3";
}
private void btn06_Click(object sender, EventArgs e)
{
txtDisplay.Text += "6";
}
private void btn07_Click(object sender, EventArgs e)
{
txtDisplay.Text += "7";
}
private void btn08_Click(object sender, EventArgs e)
{
txtDisplay.Text += "8";
}
private void btn09_Click(object sender, EventArgs e)
{
txtDisplay.Text += "9";
}
private void btn_clear_Click(object sender, EventArgs e)
{
txtDisplay.Text = "";
num_st.Clear();
oper_st.Clear();
oper_st.Push("#");
}
private void btnback_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1);
}
private void btnsqrt_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Sqrt(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btnreserve_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = 1 / num1;
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btnlog_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Log(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btne_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Exp(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btnsin_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Sin(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btncos_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Sin(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btntan_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Tan(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btnasin_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Asin(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btnacos_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Acos(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btnatan_Click(object sender, EventArgs e)
{
double num1, tempresult;
num_st.Push(txtDisplay.Text);
num1 = Convert.ToDouble(num_st.Pop());
tempresult = Math.Atan(num1);
num_st.Push(tempresult);
txtDisplay.Text = tempresult.ToString();
}
private void btn0_Click(object sender, EventArgs e)
{
txtDisplay.Text += "0";
}
private void btnpoint_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtDisplay.Text))
txtDisplay.Text = "0.0";
else if (!txtDisplay.Text.Contains("."))
txtDisplay.Text += ".";
}
}
}