这是一个用C#写的一个简单的四则运算器,因为不能上传附件,只好贴代码了,在VS 2005下通过。是个windows应用程序。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Arithmetic
{
public partial class Form1 : Form
{
Stack s_number, s_optr;//两个栈分别是数栈和操作符栈
string[] operatePriority;//操作符优先级数组
public Form1()
{
InitializeComponent();
s_number = new Stack();
s_optr = new Stack();
operatePriority = new string[4];
operatePriority[0] = "=";
operatePriority[1] = "+-";
operatePriority[2] = "*";
operatePriority[3] = "(";
}
private void button0_Click(object sender, EventArgs e)
{
switch (((Button)sender).Text)
{
case "0":
tb_view.Text = tb_view.Text + "0";
break;
case "1":
tb_view.Text = tb_view.Text + "1";
break;
case "2":
tb_view.Text = tb_view.Text + "2";
break;
case "3":
tb_view.Text = tb_view.Text + "3";
break;
case "4":
tb_view.Text = tb_view.Text + "4";
break;
case "5":
tb_view.Text = tb_view.Text + "5";
break;
case "6":
tb_view.Text = tb_view.Text + "6";
break;
case "7":
tb_view.Text = tb_view.Text + "7";
break;
case "8":
tb_view.Text = tb_view.Text + "8";
break;
case "9":
tb_view.Text = tb_view.Text + "9";
break;
case "+":
tb_view.Text = tb_view.Text + "+";
break;
case "-":
tb_view.Text = tb_view.Text + "-";
break;
case "*":
tb_view.Text = tb_view.Text + "*";
break;
case "(":
tb_view.Text = tb_view.Text + "(";
break;
case "CE":
tb_view.Text = "";
break;
}
}
private void bt_eque_Click(object sender, EventArgs e)
{
string temp = "";
int x = 0, y = 0;
tb_view.Text = tb_view.Text + "=";
for (int i = 0; i < tb_view.Text.Length; i++)
{
if (tb_view.Text >= '0' && tb_view.Text <= '9')
temp = temp + tb_view.Text;
else
{
if (tb_view.Text != '(' && temp != "")
{
s_number.Push((Int32.Parse(temp)));
temp = "";
}
if ((s_optr.Count == 0 && tb_view.Text != '=')|| tb_view.Text=='(')
s_optr.Push(tb_view.Text);
else
{
//如果当前的运算符的优先级大于栈顶的元素的优先级则将当前的运算符入栈,然后读入下一个数字
if ((tb_view.Text != ')' && OptrPriority((char)s_optr.Peek()) < OptrPriority(tb_view.Text)) || (char)s_optr.Peek() == '(')
{
s_optr.Push(tb_view.Text);
}
else
{
if (tb_view.Text == ')')
{
while ((char)s_optr.Peek() != '(' && s_number.Count > 1)
{
x = (int)s_number.Pop();
y = (int)s_number.Pop();
s_number.Push(Calculator(y, x, (char)s_optr.Pop()));
}
s_optr.Pop();
}
else
{
do
{
x = (int)s_number.Pop();
y = (int)s_number.Pop();
s_number.Push(Calculator(y, x, (char)s_optr.Pop()));
} while (s_number.Count > 1 && (OptrPriority((char)s_optr.Peek()) == OptrPriority(tb_view.Text) || tb_view .Text == '='));
if (tb_view.Text != '=')
s_optr.Push(tb_view.Text);
else
{
tb_view.Text += ((int)s_number.Pop()).ToString();
return;
}
}
}
}
}
}
}
private void bt_right_Click(object sender, EventArgs e)
{
tb_view.Text = tb_view.Text + ")";
}
private int Calculator(int a, int b, char opt)
{
switch (opt)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
}
return 0;
}
private int OptrPriority(char s)
{
for (int i = 0; i < 4; i++)
{
if (operatePriority.Contains(s.ToString ()))
return i;
}
return -1;
}
private void bt_back_Click(object sender, EventArgs e)
{
int n = tb_view.Text.Length - 1;
tb_view.Text=tb_view.Text.Remove(n, 1);
}
}
} |