计算器的原理直接使用数据结构中的表达式求值
首先了解算数四则运算的规则
(1)先乘除,后加减;
(2)从左算到右;
(3)先括号内,后括号外;
namespace CALCULATOR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//初始化 操作数栈num 操作符栈op
Stack num = new Stack();
Stack op = new Stack();
private void TextBox1_TextChanged(object sender, EventArgs e)
{
}
private void Equal_Click(object sender, EventArgs e)
{
String s = textBox1.Text+"=";
int p=0;double n;
/*如果是运算数num.push,如果是运算符判断与op栈顶的优先级*/
while (p < s.Length - 1)
{
string t = "";
while (s[p] >= '0' && s[p] <= '9'||s[p]=='.')
{
t += s[p];
p++;
}
if (t != "")
{
n = double.Parse(t);
num.Push(n);
}
if (s[p] == '+' || s[p] == '-' || s[p] == '*' || s[p] == '/'||s[p]=='('||s[p]==')')
{
if (op.Count == 0)
{
op.Push(s[p]);
p++;
}
else
{
char oprt = op.Peek();
switch (precede(oprt, s[p]))
{
case '<':
op.Push(s[p]);p++;break;
case '=':
op.Pop(); p++; break;
case '>':
double d2 = num.Pop();
double d1 = num.Pop();
char c1 = op.Pop();
num.Push(Operate(d1, d2, c1)); break;
}
/*if (precede(s[p], oprt))
{
double d1 = num.Pop();
double d2 = num.Pop();
char c1 = op.Pop();
num.Push(Operate(d1, d2, c1));
}
else
{
op.Push(s[p]);
p++;
}*/
}
}
if (s[p] == '=')
{
while(op.Count!=0)
{
double d2 = num.Pop();
double d1 = num.Pop();
char c1 = op.Pop();
num.Push(Operate(d1, d2, c1));
}
}
}//while
textBox1.Text = "" + num.Peek();
}//click
//运算
double Operate(double d1, double d2, char c1)
{
double result=0;//用resul返回计算方法
switch (c1)
{
case '+':
result = d1 + d2;break;
case '-':
result = d1 - d2; break;
case '*':
result = d1 * d2; break;
case '/':
result = d1 / d2; break;
}
return result;
}
//优先级;判断优先级还可以将上面的优先级表填入二维数组,通过查表的方法得到对应优先级
char precede(char c1, char c2)
{
char result = '.';//用result返回优先级
switch (c1)
{
case '+':
case '-':
if (c2 == '+' || c2 == '-' || c2 == ')')
result = '>';
else
result = '<';
break;
case '*':
case '/':
if (c2 == '(')
result = '<';
else
result = '>';
break;
case '(':
if (c2 == ')')
result = '=';
else
result = '<';
break;
case ')':
result = '<';
break;
}
return result;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void Num1_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void Num2_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void Num3_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void Num4_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void Num5_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void Num6_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void Num7_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void Num8_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void Num9_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void Num0_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void Point_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
private void Add_Click(object sender, EventArgs e)
{
textBox1.Text += "+";
}
private void Subtract_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
}
private void Multiply_Click(object sender, EventArgs e)
{
textBox1.Text += "*";
}
private void Divide_Click(object sender, EventArgs e)
{
textBox1.Text += "/";
}
private void Left_bracket_Click(object sender, EventArgs e)
{
textBox1.Text += "(";
}
private void Right_bracket_Click(object sender, EventArgs e)
{
textBox1.Text += ")";
}
private void Clear_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void Delete_Click(object sender, EventArgs e)
{
string s = textBox1.Text;
s=s.Substring(0, s.Length - 1);
textBox1.Text = s;
}
}
}