欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现简易计算器

程序员文章站 2022-06-24 18:14:54
c#编写一个简易计算器,供大家参考,具体内容如下界面代码using system;using system.collections.generic;using system.componentmode...

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;
 }
 }
}

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

相关标签: C# 计算器