初始界面
代码:
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 计算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool isok = true; //用来判断是否刚刚点过运算符 private string biaodashi; //表达式 private double sum = 0; //记录运算的结果 private string Preyunsuanfu;//记录上一个运算符 private string btnow = "";//记录刚输入的数字 #region //数字输入 private void no0_Click(object sender, EventArgs e) { Button btn = (Button)sender; btnow = btn.Text; if (shuru.Text != "0") { if (isok) { shuru.Text = btn.Text.ToString(); isok = false; } else { shuru.Text += btn.Text.ToString(); } } else { if (isok) { shuru.Text = ""; shuru.Text = btn.Text; isok = false; } else { shuru.Text = ""; shuru.Text += btn.Text; } } } #endregion #region //运算符 private void btjia_Click(object sender, EventArgs e) { Button btn = (Button)sender; string yunsuanfu = btn.Text; if (Preyunsuanfu == null) { biaodashi += shuru.Text; sum = double.Parse(shuru.Text); } else { if (!isok) { if (Preyunsuanfu == "+") { sum += double.Parse(shuru.Text); } if (Preyunsuanfu == "-") { sum -= double.Parse(shuru.Text); } if (Preyunsuanfu == "*") { sum *= double.Parse(shuru.Text); } if (Preyunsuanfu == "/") { sum /= double.Parse(shuru.Text); } if (Preyunsuanfu == "%") { sum %= double.Parse(shuru.Text); } biaodashi = biaodashi + Preyunsuanfu + shuru.Text; shuru.Text = sum.ToString(); } } shuchu.Text = biaodashi + yunsuanfu; Preyunsuanfu = yunsuanfu; isok = true; } #endregion #region //退格 private void tuige_Click(object sender, EventArgs e) { int geshu= shuru.Text.ToString().Length; string str = shuru.Text.ToString(); if (geshu > 1) { str = str.Remove(geshu - 1, 1); shuru.Text = str; } else { shuru.Text = "0"; } } #endregion #region //清空 private void clear_Click(object sender, EventArgs e) { shuru.Text = "0"; shuchu.Text = ""; sum = 0; biaodashi = ""; Preyunsuanfu = ""; } #endregion #region //等于 private void equal_Click(object sender, EventArgs e) { shuchu.Text = ""; if (Preyunsuanfu == "+") { shuru.Text = (sum + double.Parse(btnow)).ToString(); } if (Preyunsuanfu == "-") { shuru.Text = (sum - double.Parse(btnow)).ToString(); } if (Preyunsuanfu == "*") { shuru.Text = (sum * double.Parse(btnow)).ToString(); } if (Preyunsuanfu == "/") { shuru.Text = (sum / double.Parse(btnow)).ToString(); } if (Preyunsuanfu == "%") { shuru.Text = (sum % double.Parse(btnow)).ToString(); } } #endregion #region //小数点 //private string dian = ""; private void point_Click(object sender, EventArgs e) { Button btn = (Button)sender; string predian = btn.Text; shuru.Text += btn.Text; } #endregion } }
时间: 2024-10-23 06:50:58