设计思路:
Double.Parse()方法:将字符串形式的数字转换为数字(可简单理解为去掉双引号)
代码实现:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Calculator { public partial class Frm_Main : Form { StringBuilder StrData = new StringBuilder(); //数据及运算符保存 public Frm_Main() { InitializeComponent(); } private void Frm_Main_Load(object sender, EventArgs e) { calcResult.Clear(); calcProcess.Clear(); } public void DataProcess(string data) { calcProcess.Text = calcProcess.Text + data; StrData.Append(data); } //0 private void button16_Click(object sender, EventArgs e) { DataProcess("0"); } //1 private void button12_Click(object sender, EventArgs e) { DataProcess("1"); } //2 private void button11_Click(object sender, EventArgs e) { DataProcess("2"); } //3 private void button10_Click(object sender, EventArgs e) { DataProcess("3"); } //4 private void button5_Click(object sender, EventArgs e) { DataProcess("4"); } //5 private void button6_Click(object sender, EventArgs e) { DataProcess("5"); } //6 private void button7_Click(object sender, EventArgs e) { DataProcess("6"); } //7 private void button1_Click(object sender, EventArgs e) { DataProcess("7"); } //8 private void button2_Click(object sender, EventArgs e) { DataProcess("8"); } //9 private void button3_Click(object sender, EventArgs e) { DataProcess("9"); } //小数点 private void button15_Click(object sender, EventArgs e) { DataProcess("."); } //除 private void button4_Click(object sender, EventArgs e) { DataProcess("÷"); } //乘 private void button8_Click(object sender, EventArgs e) { DataProcess("×"); } //减 private void button9_Click(object sender, EventArgs e) { DataProcess("-"); } //加 private void button13_Click(object sender, EventArgs e) { DataProcess("+"); } //回退 private void button18_Click(object sender, EventArgs e) { StrData.Remove(StrData.Length-1,1); calcProcess.Text = StrData.ToString(); } //clear private void button20_Click(object sender, EventArgs e) { calcResult.Clear(); calcProcess.Clear(); StrData.Clear(); } double CalcData(string s) { char[] splitor = { '×', '÷' }; string[] strArr = s.Split(splitor); double tempData=0; int i = 0; tempData = Double.Parse(strArr[0]); foreach(char temps in s) { switch (temps) { case '÷': tempData = tempData / Double.Parse(strArr[i+1]); i++; break; case '×': tempData = tempData * Double.Parse(strArr[i + 1]); i++; break; default: break; } } return tempData; } //等于 private void button14_Click(object sender, EventArgs e) { char[] SplitSymbol = { '+', '-' }; string StrDataTemp = StrData.ToString(); ; string[] StrDataArr= StrDataTemp.Split(SplitSymbol); double ResultData = 0; int j = 0; //先以加减运算符为分隔符分割字符串,处理分割后数组中的乘除运算 for (int i=0; i< StrDataArr.Length;i++) { if(StrDataArr[i].Contains("×")|| StrDataArr[i].Contains("÷")) { StrDataArr[i] = CalcData(StrDataArr[i]).ToString(); } } //加减运算 ResultData= Double.Parse(StrDataArr[0]); foreach (char temps in StrDataTemp) { switch (temps) { case '-': ResultData = ResultData - Double.Parse(StrDataArr[j + 1]); j++; break; case '+': ResultData = ResultData + Double.Parse(StrDataArr[j + 1]); j++; break; default: break; } } calcResult.Text = ResultData.ToString(); } private void calcResult_TextChanged(object sender, EventArgs e) { } private void calcProcess_TextChanged(object sender, EventArgs e) { } } }