当前位置:首页 >> 计算机软件及应用 >>

C#编写可以开根和幂运算计算器的详细过程


首先,面板布局如下:

控件命名规律为: 1:bt1, 2:bt2 ....以此类推,从程序中可以很容易看出来. 下面就是详细的程序代码 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 科学计算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); txt_result.Text += "";//默认打开计算器时屏幕显示"空格" } #region 数字按键的屏幕显示 private void bt1_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) ==0)//判断是否已经输入了数字 // txt_result.Text = "1"; //如果已经输入,并且不是 0,则加 1 //else

txt_result.Text += "1"; } private void bt2_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "2";//如果已经输入,并且不是 0,则加 2 //else txt_result.Text += "2"; } private void bt3_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "3";//如果已经输入,并且不是 0,则加 3 //else txt_result.Text += "3"; } private void bt4_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "4";//如果已经输入,并且不是 0,则加 4 //else txt_result.Text += "4"; } private void bt5_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "5";//如果已经输入,并且不是 0,则加 5 //else txt_result.Text += "5"; } private void bt6_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "6";//如果已经输入,并且不是 0,则加 6 //else txt_result.Text += "6"; }

private void bt7_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "7";//如果已经输入,并且不是 0,则加 7 //else txt_result.Text += "7"; } private void bt8_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "8";//如果已经输入,并且不是 0,则加 8 //else txt_result.Text += "8"; } private void bt9_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "9";//如果已经输入,并且不是 0,则加 9 //else txt_result.Text += "9"; } private void bt0_Click(object sender, EventArgs e) { //if (float.Parse(txt_result.Text) != 0)//判断是否已经输入了数字 // txt_result.Text += "0";//如果已经输入,并且不是 0,则加 0 //else txt_result.Text += "0"; } //上述判断是否输入数字的方法在小数点以及运算符处还没有实现,先用别的 方法代替一下 private void btpoint_Click(object sender, EventArgs e)//小数点 { txt_result.Text += "."; } private void btc_Click(object sender, EventArgs e)//C 键的功能实现 { txt_result.Text = ""; } private void bttuige_Click(object sender, EventArgs e)//退格的功能实现

{ if (txt_result.Text != "") txt_result.Text = txt_result.Text.Substring(0, txt_result.Text.Length - 1); } private void btadd_Click(object sender, EventArgs e) { txt_result.Text += "+"; } private void btsub_Click(object sender, EventArgs e) { txt_result.Text += "-"; } private void btpro_Click(object sender, EventArgs e) { txt_result.Text += "*"; } private void btdiv_Click(object sender, EventArgs e) { txt_result.Text += "/"; } private void btinv_Click(object sender, EventArgs e) { txt_result.Text += "^"; } private void btevo_Click(object sender, EventArgs e) { txt_result.Text += "√"; } private void btzuo_Click(object sender, EventArgs e) { txt_result.Text += "("; } private void btyou_Click(object sender, EventArgs e) { txt_result.Text += ")"; }

#endregion #region "="计算功能实现 private void btequ_Click(object sender, EventArgs e) { string str = txt_result.Text; List<string> lists = new List<string>(); string temp = ""; int index = 0, sum = str.Length; foreach (char c in str) //循环遍历计算式的每个字符,并逐一识别 { index++; if (c != '+' && c != '-' && c != '*' && c != '/' && c != '(' && c != ')' && c != '^' && c != '√') { temp += c; } else { if (temp != "") { lists.Add(temp); } lists.Add(c.ToString()); temp = ""; } if (index == sum && temp != "") { lists.Add(temp); } } //开始运算 try { #region 遍历表达式是否存在括号 index = 0; for (int i = 0; i < lists.Count; i++) { if (i > 0 && lists[i - 1] != ")" && lists[i] == "^") 算

//首先进行幂运

{ lists[i + 1] = Make(i - 1, lists[i - 1], lists[i + 1], lists[i]).ToString(); lists.RemoveAt(i - 1); lists.RemoveAt(i - 1); i -= 1; } if (lists[i] == ")") //表达式中有括号的情况 { for (int j = i; j >= 0; j--) { if (lists[j] == "(")//j i 分别为左右括号的位置 { index = j + 1; do { string type1 = lists[index + 1];//遍历找符号,列 表顺序 if (type1 == "+" || type1 == "-") { if (i - j <= 5) { if (lists[index + 2] == "√") { lists[index + 2] = kaigen(Int32.Parse(lists[index + 3])).ToString(); lists.RemoveAt(index + 3); i -= 1; } else { lists[index + 2] = Make(index, lists[index], lists[index + 2], type1).ToString(); lists.RemoveAt(index); lists.RemoveAt(index); i -= 2; } }//定义一级运算 else { string type2 = lists[index + 3]; string type3 = lists[index + 4]; if (type3 == "√")//定义三级运算 开根

{ lists[index + 4] = kaigen(Int32.Parse(lists[index + 5])).ToString(); lists.RemoveAt(index + 5); i -= 1; } else if (type2 == "*" || type2 == "/" || type2 == "^")//定义二级运算 { lists[index + 2] = Make(index + 2, lists[index + 2], lists[index + 4], type2).ToString(); lists.RemoveAt(index + 3); lists.RemoveAt(index + 3); i -= 2; } else { lists[index + 2] = Make(index, lists[index], lists[index + 2], type1).ToString(); lists.RemoveAt(index); lists.RemoveAt(index); i -= 2; } } } else { lists[index + 2] = Make(index, lists[index], lists[index + 2], type1).ToString(); lists.RemoveAt(index); lists.RemoveAt(index); i -= 2; } if (i - j == 2) { lists.RemoveAt(j); lists.RemoveAt(j + 1); i -= 2; break; //跳出 while 循环 } } while (1 == 1); break; } //跳出 for(j)循环

} } } #endregion #region 表达式中没括号的情况 index = 0; do { if (lists.Count == 1) { txt_result.Text += "=" + lists[0]; break; } string type1 = lists[index + 1]; if (type1 == "+" || type1 == "-") { if (lists.Count <= 4) { if (lists[index + 2] == "√") { lists[index + 2] = kaigen(Int32.Parse(lists[index + 3])).ToString(); lists.RemoveAt(index + 3); } else { lists[index + 2] = Make(index, lists[index], lists[index + 2], type1).ToString(); lists.RemoveAt(index); lists.RemoveAt(index); } } else { string type2 = lists[index + 3]; if (type2 == "*" || type2 == "/" || type2 == "^") { lists[index + 2] = Make(index + 2, lists[index + 2], lists[index + 4], type2).ToString(); lists.RemoveAt(index + 3); lists.RemoveAt(index + 3);

} else { lists[index + 2] = Make(index, lists[index], lists[index + 2], type1).ToString(); lists.RemoveAt(index); lists.RemoveAt(index); } } } else { lists[index + 2] = Make(index, lists[index], lists[index + 2], type1).ToString(); lists.RemoveAt(index); lists.RemoveAt(index); } if (lists.Count == 1) { txt_result.Text += "=" + lists[0]; break; } } while (1 == 1); #endregion } catch (Exception ex) { MessageBox.Show("请输入正确的运算表达式" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion #region make 定义 private object Make(int index, string noStr1, string noStr2, string type) { double tempSum = 0, no1 = Convert.ToDouble(noStr1), no2 = Convert.ToDouble(noStr2); switch (type) { case "+": tempSum = no1 + no2; break; case "-":

tempSum = no1 - no2; break; case "*": tempSum = no1 * no2; break; case "/": tempSum = no1 / no2; break; case "^": tempSum = miyunsuan(no1, (int)no2); break; case "√": tempSum = kaigen((int)no2); break; } return tempSum; } #endregion #region miyunsuan 定义 private static double miyunsuan(double x, int y) { double Res = 0; if (y == 0) { Res = 1; } else if (y == 1) { Res = x; } else { Res = x * miyunsuan(x, y - 1); } return Res; } #endregion #region kaigen 函数定义 private static double kaigen(int a) { double Res = 0; Res = Math.Sqrt(a);

return Res; } #endregion } } 以上就是一个具有开根和幂运算的,简单的计算器,希望对大家有帮助,有问题可以私信我,互 相学习.


相关文章:
更多相关标签: