基于Visual Studio的建议计算器设计
- 格式:doc
- 大小:1015.00 KB
- 文档页数:11
通信与信息工程学院IT实训报告班级:学号:**: **日期:2014年11月9日计算器实验1、实验名称:简易计算器2、实验内容:完成计算器功能3、实验结果:完成基本要求,实现2位整数加减乘除运算,带退位和清除功能。
完成扩展,实现按运算优先级进行任意位数小数加减乘除四则混合运算。
完成亮点,完整显示计算表达式,系统容错率高,可多次退位。
代码如下:using System;using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace jisuanqi{public partial class Form1 : Form{public Form1(){InitializeComponent();}float[] mainNum = new float[10]; // 存储第一个数int[] symbol = new int[10];private int i = 0 ;// private int yusuan_level ;private float result;private bool isSecond = false; // 用来判断输入的是第一个还是第二个数private bool isDone = false; // 用来判断是否按了等于按钮private bool isKeyupclear = true;//用来判断是否按了clear键,程序开始前默认按了;private bool isKeyupclear_2 = true;//用来判断是否按了clear键,程序开始前默认按了;private bool isCount = false ; //判断是否有计算符号public void setText(string textest) //设置显示文本框的值{if (textest.Equals("clear")){textBox1.Text = "0";isSecond = false;isDone = false;isKeyupclear = true;i = 0;}else if (textest.Equals("←")){if (this.textBox1.Text.Length == 1){this.textBox1.Text = "0";isKeyupclear = true;}else{this.textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length -1); }}else{if (isKeyupclear) //对是否按下clear键的判断{textBox1.Text = textest;isKeyupclear = false;}else{textBox1.Text += textest;}}}public void setText2(string textest) //设置运算文本框的值{if (textest.Equals("clear")){textBox2.Text = "0";isSecond = false;isKeyupclear_2 = true;i = 0;}else if (textest.Equals("←")){if (this.textBox2.Text.Length == 1){this.textBox2.Text = "0";isKeyupclear_2 = true;}else if (isCount){i--;isCount = false;}else{this.textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);}}else{if (isCount){textBox2.Text = textest;isCount = false;}else{if (isDone){textBox2.Text = textest;isDone = false;}else{if (isKeyupclear_2) //对是否按下clear键的判断{textBox2.Text = textest;isKeyupclear_2 = false;}elsetextBox2.Text += textest;}}}btnEqual.Select(); //设置"="号的焦点}//设置运算类型private void setOperator(int operation){if (textBox2.Text.Length > 0){symbol[i]=operation;mainNum[i] = float.Parse(textBox2.Text);i++;isSecond = true;isDone = false;isCount = true;btnEqual.Select(); //设置"="号的焦点}}private void Equal_Operation(int i)//优先级判断》》》简直写疯了。
{int j = 0;int m = 0;while( j<i ){if (symbol[j + 1] - symbol[j] > 1) //高优先级运算{j++;switch (symbol[j]){case 4:result = mainNum[j] * mainNum[j+1];break;case 5:result = mainNum[j] / mainNum[j+1];break;}//第一个优先级运算m = j - 1;while (m>=0)//第二个以后优先级运算{if (symbol[j + 1] - symbol[m] > 1){switch (symbol[j + 1]) //判断所进行的运算 {case 4:result = result * mainNum[j + 2];break;case 5:result = result / mainNum[j + 2];break;}j++;}else{switch (symbol[m]) //判断所进行的运算 {case 1:result = mainNum[m] + result;break;case 2:result = mainNum[m] - result;break;}m--;}}mainNum[j+1] = result;j++;}else//低优先级运算{switch ( symbol[j] ){case 1:result = mainNum[j] + mainNum[j + 1];break;case 2:result = mainNum[j] - mainNum[j + 1];break;case 4:result = mainNum[j] * mainNum[j + 1];break;case 5:result = mainNum[j] / mainNum[j + 1];break;}mainNum[j+1] = result;j++;}}setText(result.ToString());while (j > 0)//清空符号{j--;symbol[j] = 0;}}private void doEquals(){if (isSecond) //判断已经输入第二个数后按了"="号键 {mainNum[i] = float.Parse(textBox2.Text);Equal_Operation(i);setText2("clear");isDone = true;isSecond = false;}}private void Form1_Load(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){setText("1");setText2("1");}private void button2_Click_1(object sender, EventArgs e){setText("2");setText2("2");}private void button3_Click(object sender, EventArgs e){setText("3");setText2("3");}private void button4_Click(object sender, EventArgs e){setText("4");setText2("4");}private void button5_Click(object sender, EventArgs e){setText("5");setText2("5");}private void button6_Click(object sender, EventArgs e){setText("6");setText2("6");}private void button7_Click(object sender, EventArgs e){setText("7");setText2("7");}private void button8_Click(object sender, EventArgs e){setText("8");setText2("8");}private void button9_Click(object sender, EventArgs e){setText("9");setText2("9");}private void button13_Click(object sender, EventArgs e){setText("0");setText2("0");}private void btn_point_Click(object sender, EventArgs e){setText(".");setText2(".");}private void btnBackspace_Click(object sender, EventArgs e) {setText("←");setText2("←");}private void btn_clear_Click(object sender, EventArgs e){setText("clear");setText2("clear");}private void button10_Click(object sender, EventArgs e){setText("+");setOperator(1);}private void button11_Click(object sender, EventArgs e){setText("-");setOperator(2);}private void button12_Click(object sender, EventArgs e){setText("*");setOperator(4);}private void button15_Click(object sender, EventArgs e){setText("/");setOperator(5);}private void btnEqual_Click(object sender, EventArgs e){setText("=");doEquals();}private void textBox1_TextChanged(object sender, EventArgs e) {}private void textBox2_TextChanged(object sender, EventArgs e) {}}}。