c#计算器 实验报告
一、实验目的
设计一个简单计算器,在功能上功能尽量模拟windows操作系统中的计算器,系统界面不做强制要求。
最低要求:应具有一般计算功能,能进行基本的加减乘除运算。其他计算功能作为加分项。
二、实验原理
1.本实验包含22个控件,其中20个button按钮用于表达式的输入,2个textbox用于显示输出表达式和计算结果。
2.按下button按钮时,textbox的内容会随之更新。expression列表会根据布尔类型的pull值进行更新。当pull为真时,表示当前输入运算符,会单独占用列表的一个位置。当pull为假时,表示当前输入数字,会根据具体情况决定是否与其他内容共同占用一个列表位置。
3.按下AC清零按钮时,textbox中的文本清空,expression列表内容清空。
4.按下CE清除按钮时,textbox删除一个字符。当expression最后一个字符串中含有小数点或者不含有小数点但大于9时,此字符串删除最后一个字符,除此之外,删除该字符串。
5.按下等于按钮时,输出计算结果。首先将中缀表达式转后缀表达式,数字直接入后缀表达式,运算符优先级高,入运算符列表,运算符优先级低,运算符列表出去一个元素,直至完成。再对后缀表达式求值,如果是数字,加入到数组中,如果是运算符,从数组中取出两个数进行运算,结果放到数组中。最后进行输出。
三、实验要求
具有一般计算功能,能进行基本的加减乘除运算,包括小数的运算,并且能够使用括号,能够清零和删除。
四、实验结果(程序)及分析
源代码:
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;
namespace WindowsFormsApplication1
{
public partial class Calculator : Form
{
List expression = new List();
bool pull = false; //运算符未输入设为false,输入设为true
public Calculator()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
resultBox.Text = “”;
}
private void button0_Click(object sender, EventArgs e){addExpression(\"0\");}private void button1_Click(object sender, EventArgs e){addExpression(\"1\");}private void button2_Click(object sender, EventArgs e){addExpression(\"2\");}private void button3_Click(object sender, EventArgs e){addExpression(\"3\");}private void button4_Click(object sender, EventArgs e){addExpression(\"4\");}private void button5_Click(object sender, EventArgs e){addExpression(\"5\");}private void button6_Click(object sender, EventArgs e){addExpression(\"6\");}private void button7_Click(object sender, EventArgs e){addExpression(\"7\");}private void button8_Click(object sender, EventArgs e){addExpression(\"8\");}private void button9_Click(object sender, EventArgs e){addExpression(\"9\");}private void addbutton_Click(object sender, EventArgs e){addOperat(\"+\");}private void subbutton_Click(object sender, EventArgs e){addOperat(\"-\");}private void mulbutton_Click(object sender, EventArgs e){addOperat(\"*\");}private void divbutton_Click(object sender, EventArgs e){addOperat(\"/\");}private void buttonleft_Click(object sender, EventArgs e){addOperat(\"(\");}private void buttonright_Click(object sender, EventArgs e){addOperat(\")\");}private void button_pot_Click(object sender, EventArgs e){resultBox.Text += \".\";expression[expression.Count - 1] = expression.Last() + \".\";pull = false;}//添加表达式的运算符private void addOperat(string str){resultBox.Text += str;expression.Add(str);pull = true;}//添加表达式的值private void addExpression(string str){resultBox.Text += str;if (pull) expression.Add(str);else{if (expression.Count <= 0) expression.Add(str);else expression[expression.Count - 1] = expression.Last() + str;}pull = false;}//优先级判断public bool priority(string a, string b){if (a == \"+\"){if (b == \"-\" || b == \"(\") return true;else return false;}else if (a == \"-\"){if (b == \"(\") return true;else return false;}else if (a == \"(\") return true;else if (a == \")\"){if (b == \"(\") return true;else return false;}else if (a == \"*\"){if (b == \"/\" || b == \"+\" || b == \"-\" || b ==\"(\") return true;else return false;}else if (a == \"/\"){if (b == \"+\" || b == \"-\" || b == \"(\") return true;else return false;}else return false;}//等号点击事件触发private void equalbutton_Click(object sender, EventArgs e){formulaBox.Text = resultBox.Text + \"=\";resultBox.Text = Convert.ToString(Result(Suffix()));//中缀转后缀,后缀表达四求值}//中缀表达式转后缀表达式private List<string> Suffix(){List<string> suffix = new List<string>();List<string> operat = new List<string>();operat.Add(\"(\");for (int i = 0; i < expression.Count; i++){//数字直接入后缀表达式列表if (isNum(expression[i]))suffix.Add(expression[i]);//运算符考虑优先级else{//运算符优先级高,入运算符列表if (priority(expression[i], operat.Last())){if (operat.Last() == \"(\" && expression[i] == \")\")operat.RemoveAt(operat.Count-1);else operat.Add(expression[i]);}//运算符优先级低,运算符列表出去一个元素,继续比较此元素else{suffix.Add(operat.Last());operat.RemoveAt(operat.Count - 1);i--;}}}//处理列表剩余元素while (operat.Count > 1){suffix.Add(operat.Last());operat.RemoveAt(operat.Count - 1);}return suffix;}//判断字符串是否为数字private bool isNum(string str){ if(str!=\"+\"&&str!=\"-\"&&str!=\"*\"&&str!=\"/\"&&str!=\"(\"&&str!=\")\")return true;elsereturn false;}//后缀表达式求值private double Result(List<string> suffix){double []stack = new double[100];int j = -1;for (int i = 0; i < suffix.Count; i++){//如果是数字,加入到数组中if (isNum(suffix[i]))stack[++j] = Convert.ToDouble(suffix[i]);//是运算符,从数组中取出两个数进行运算,结果放到数组中elseif (suffix[i] == \"+\"){double b = stack[--j] + stack[++j];stack[--j] = b;}else if (suffix[i] == \"-\"){double b = stack[--j] - stack[++j];stack[--j] = b;}else if (suffix[i] == \"*\"){double b = stack[--j] * stack[++j];stack[--j] = b;}else if (suffix[i] == \"/\"){double b = stack[--j] / stack[++j];stack[--j] = b;}}double result = stack[0];return result;}//清除按钮事件触发private void buttonCE_Click(object sender, EventArgs e){resultBox.Text = resultBox.Text.Remove(resultBox.Text.Count() - 1);//当前字符串有\".\"或者字符串数值大于9,此字符串去掉最后一个字符if (isPot(expression.Last()) || isOperate(expression.Last())==false && Convert.ToDouble(expression.Last()) > 9.0)expression[expression.Count - 1] = expression.Last().Remove(expression.Last().Length - 1);//去掉该字符串else expression.Remove(expression.Last());//需要对pull的值进行修改if (isOperate(expression.Last()))pull = true;else pull = false;}//判断字符串中是否有\".\"private bool isPot(string str){for (int i = 0; i < str.Count(); i++)if (str[i] == \'.\') return true;return false;}//判断字符串中是否有加减乘除和括号private bool isOperate(string str){if (str == \"+\" || str == \"-\" || str == \"*\" || str == \"/\" || str == \"(\" || str == \")\")return true;else return false;}//清0按钮触发private void buttonAC_Click(object sender, EventArgs e){formulaBox.Text = \"\";resultBox.Text = \"\";expression.RemoveRange(0, expression.Count);pull = false;}}
}
实验结果: