首页 > 编程语言 > C#实现简易计算器
2021
01-10

C#实现简易计算器

C#编写一个简易计算器,供大家参考,具体内容如下

界面

代码

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;
using System.Collections;


namespace calculor
{
 public partial class Form1 : Form
 {
 public Form1()
 {
  InitializeComponent();
 }

 //stack<int> nums;
 Stack nums = new Stack();
 //stack<char> ops;
 Stack ops = new Stack();

 //快速幂
 int qmi(int a, int b)
 {
  int res = 1;

  while (b > 0)
  {
  if ((b & 1) == 1) res = res * a;
  a = a * a;
  b >>= 1;
  }

  return res;
 }

 //计算
 void Cal()
 {
  if (nums.Count <= 1)
  {
  if(ops.Count >= 0)
  {
   ops.Pop();
  }
  return;
  }
  double a = (double)nums.Peek(); nums.Pop();
  double b = (double)nums.Peek(); nums.Pop();
  char c = (char)ops.Peek(); ops.Pop();
  double d = 1;
  if (c == '+') d = a + b;
  else if (c == '-') d = b - a;
  else if (c == '*') d = b * a;
  else if (c == '/')
  {
  if (a == 0)
  {
   MessageBox.Show("Invliad operator");
   return;  
  }
  else
  {
   d = b / a;
  }
  
  }
  else if (c == '%') d = b % a;
  else
  {
  d = Math.Pow(b, a);
  }

  nums.Push(d);
 }

 double Calculate(string str)
 {
  if (str[0] == '0') str = '0' + str; //-(1 + 1)
  string left = "";
  for (int i = 0; i < str.Length; i++) left += "(";
  str = left + str + ")"; //保证左括号数量大于右括号

  for (int i = 0; i < str.Length; i++)
  {
  if (str[i] >= '0' && str[i] <= '9')
  {
   int j = i, tmp = 0; //多位数字
   bool flag = false;
   double t = 0;
   while (j < str.Length && ((str[j] >= '0' && str[j] <= '9') || str[j] == '.'))
   {
   if(str[j] == '.')
   {
    tmp = j;
    j++;
    flag = true;
    continue;
   }
   t = t * 10 + str[j] - '0';
   j++;
   }

   i = j - 1;
   if (flag)
   {
   for (int l = 0; l < j - tmp - 1; l++)
   {
    t *= 0.1;
   }
   }
   nums.Push(t);
  }
  else
  {
   char c = str[i];
   if (c == ' ') continue;
   if (c == '(') ops.Push(c);
   else if (c == '+' || c == '-')
   {
   if (c == '-' && i > 0 && !(str[i - 1] >= '0' && str[i - 1] <= '9') && str[i - 1] != ')' && str[i - 1] != ' ') //'-' 代表负号
   {
    if (str[i + 1] == '(') // 将-(...)变成-1 * (...)
    {
    nums.Push(-1);
    ops.Push('*');
    }
    else
    {
    int j = i + 1, tmp = 0;
    double t = 0;
    bool flag = false;
    while (j < str.Length && ((str[j] >= '0' && str[j] <= '9') || str[j] == '.'))
    {
     if (str[j] == '.')
     {
     tmp = j;
     j++;
     flag = true;
     continue;
     }
     t = t * 10 + str[j] - '0';
     j++;
    }

    i = j - 1;
    if (flag)
    {
     for (int l = 0; l < j - tmp - 1; l++)
     {
     t *= 0.1;
     }
    }
    
    nums.Push(-t);

    
    }
   }
   else //将 + - 号前面的运算符优先级高的结算,加,减优先级最低。前面都可以结算
   {
    while ((char)ops.Peek() != '(') Cal();
    ops.Push(c);
   }
   }
   else if (c == '*' || c == '/' || c == '%') //将 * / 号前面的运算符优先级高或等于的结算
   {
   while ((char)ops.Peek() == '*' || (char)ops.Peek() == '/' || (char)ops.Peek() == '^' || (char)ops.Peek() == '%') Cal();
   ops.Push(c);
   }
   else if (c == '^') //将 '^' 号前面的运算符优先级高或等于的结算
   {
   while ((char)ops.Peek() == '^') Cal();
   ops.Push(c);
   }
   else if (c == ')') // 将整个括号结算
   {
   while ((char)ops.Peek() != '(') Cal();
   ops.Pop(); //删除'('
   }
   //else MessageBox.Show("invalid operator!");
  }


  }

  if(nums.Count != 0) return (double)nums.Peek();

  return -1;

 }


 private void Form1_Load(object sender, EventArgs e)
 {

 }

 private void equal_Click(object sender, EventArgs e)
 {
  string str = txt_Result.Text;
  double res = Calculate(str);
  if (res == -1)
  {
  txt_Result.Text = "";
  return;
  }
  txt_Result.Text = res.ToString();
 }

 private void digitOne_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitOne.Text;
 }

 private void digitTwo_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitTwo.Text;
 }

 private void digitThree_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitThree.Text;
 }

 private void digitFour_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitFour.Text;
 }

 private void digitFive_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitFive.Text;
 }

 private void digitSix_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitSix.Text;
 }

 private void digitZero_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitZero.Text;
 }

 private void digitSeven_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitSeven.Text;
 }

 private void digitEight_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitEight.Text;
 }

 private void digitNine_Click(object sender, EventArgs e)
 {
  txt_Result.Text += digitNine.Text;
 }

 private void cal_Sqrt_Click(object sender, EventArgs e)
 {
  string str = txt_Result.Text;
  double res = Calculate(str);
  if (res == -1)
  {
  txt_Result.Text = "";
  return;
  }
  if (res < 0)
  {
   MessageBox.Show("{数据不合法");
  txt_Result.Text = "";
  return;
  }
  double res1 = Math.Sqrt(res);
  txt_Result.Text = res1.ToString();
 }

 private void Dot_Click(object sender, EventArgs e)
 {
  txt_Result.Text += Dot.Text;
 }

 private void cal_Multi_Click(object sender, EventArgs e)
 {
  txt_Result.Text += cal_Multi.Text;
 }

 private void cal_Sub_Click(object sender, EventArgs e)
 {
  txt_Result.Text += cal_Sub.Text;
 }

 private void cal_Add_Click(object sender, EventArgs e)
 {
  txt_Result.Text += cal_Add.Text;
 }

 private void cal_Rem_Click(object sender, EventArgs e)
 {
  txt_Result.Text += cal_Rem.Text;
 }

 private void left_brack_Click(object sender, EventArgs e)
 {
  txt_Result.Text += left_brack.Text;
 }

 private void right_brack_Click(object sender, EventArgs e)
 {
  txt_Result.Text += right_brack.Text;
 }

 private void cal_log_Click(object sender, EventArgs e)
 {
  string str = txt_Result.Text;
  double res = Calculate(str);
  if (res == -1)
  {
  txt_Result.Text = "";
  return;
  }
  if (res < 0)
  {
  MessageBox.Show("{数据不合法");
  txt_Result.Text = "";
  return;
  }
  double res1 = Math.Log(res);
  txt_Result.Text = res1.ToString();
 }

 private void btn_Clear_Click(object sender, EventArgs e)
 {
  txt_Result.Text = "";
 }

 private void cal_mi_Click(object sender, EventArgs e)
 {
  txt_Result.Text += cal_mi.Text;
 }

 private void cal_Div_Click(object sender, EventArgs e)
 {
  txt_Result.Text += cal_Div.Text;
 }

 private void btn_backspace_Click(object sender, EventArgs e)
 {
  string str = txt_Result.Text;
  string newStr = "";
  int len = str.Length;
  if (len > 0)
  {
  for(int i = 0; i < len - 1; i++)
  {
   newStr += str[i];
  }
  }
  txt_Result.Text = newStr;
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧