原文内容来自免费考研论坛,请点击查看全文
http://bbs.freekaoyan.com/viewthread.php?tid=288805
这是一个用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);
}
}
}
一个简单四则运算器的实现
haizzz 免费考研论坛/2008-07-22
相关话题/
领限时大额优惠券,享本站正版考研考试资料!
优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...考试优惠券 本站小编 Free壹佰分学习网 2022-09-19
Free考研考试FreeKaoYan.Com
欢迎来到Free考研考试,"为实现人生的Free而奋斗"
© 2020 FreeKaoYan! . All rights reserved.
