欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)簡易計算器

 更新時間:2021年01月10日 08:35:27   作者:xiaoxiongyuan__s  
這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)簡易計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

C#編寫一個簡易計算器,供大家參考,具體內(nèi)容如下

界面

代碼

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 + ")"; //保證左括號數(shù)量大于右括號

  for (int i = 0; i < str.Length; i++)
  {
  if (str[i] >= '0' && str[i] <= '9')
  {
   int j = i, tmp = 0; //多位數(shù)字
   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] != ' ') //'-' 代表負(fù)號
   {
    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 //將 + - 號前面的運算符優(yōu)先級高的結(jié)算,加,減優(yōu)先級最低。前面都可以結(jié)算
   {
    while ((char)ops.Peek() != '(') Cal();
    ops.Push(c);
   }
   }
   else if (c == '*' || c == '/' || c == '%') //將 * / 號前面的運算符優(yōu)先級高或等于的結(jié)算
   {
   while ((char)ops.Peek() == '*' || (char)ops.Peek() == '/' || (char)ops.Peek() == '^' || (char)ops.Peek() == '%') Cal();
   ops.Push(c);
   }
   else if (c == '^') //將 '^' 號前面的運算符優(yōu)先級高或等于的結(jié)算
   {
   while ((char)ops.Peek() == '^') Cal();
   ops.Push(c);
   }
   else if (c == ')') // 將整個括號結(jié)算
   {
   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("{數(shù)據(jù)不合法");
  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("{數(shù)據(jù)不合法");
  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;
 }
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 三十分鐘快速掌握C# 6.0知識點

    三十分鐘快速掌握C# 6.0知識點

    這篇文章主要介紹了C# 6.0的相關(guān)知識點,文中介紹的非常詳細(xì),通過這篇文字可以讓大家在三十分鐘內(nèi)快速的掌握C# 6.0,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • C#圖表算法之有向圖

    C#圖表算法之有向圖

    這篇文章介紹了C#圖表算法之有向圖,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • Directory文件類的實例講解

    Directory文件類的實例講解

    下面小編就為大家分享一篇Directory文件類的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • c#剪切板操作的簡單實例

    c#剪切板操作的簡單實例

    這篇文章介紹了c#剪切板操作的簡單實例,有需要的朋友可以參考一下
    2013-11-11
  • Jquery+Ajax+Json+存儲過程實現(xiàn)高效分頁

    Jquery+Ajax+Json+存儲過程實現(xiàn)高效分頁

    這篇文章主要介紹Jquery+Ajax+Json+存儲過程實現(xiàn)分頁,需要的朋友可以參考下
    2015-08-08
  • C#實現(xiàn)最完整的文件和目錄操作類實例

    C#實現(xiàn)最完整的文件和目錄操作類實例

    這篇文章主要介紹了C#實現(xiàn)最完整的文件和目錄操作類,涉及C#針對文件與目錄的創(chuàng)建、獲取、檢測、刪除等常用操作技巧,非常具有實用價值,需要的朋友可以參考下
    2015-05-05
  • C#中GDI+繪制圓弧及圓角矩形等比縮放的繪制

    C#中GDI+繪制圓弧及圓角矩形等比縮放的繪制

    這篇文章主要介紹了C#中GDI+繪制圓弧及圓角矩形等比縮放的繪制,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • C# Resources資源詳解

    C# Resources資源詳解

    這篇文章主要為大家詳細(xì)介紹了C# Resources資源,包括Resource Basics、Strongly Typed Resources等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • WPF實現(xiàn)自帶觸控鍵盤的文本框

    WPF實現(xiàn)自帶觸控鍵盤的文本框

    這篇文章實現(xiàn)了WPF自帶觸控鍵盤的文本框,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • C#實現(xiàn)自定義光標(biāo)并動態(tài)切換

    C#實現(xiàn)自定義光標(biāo)并動態(tài)切換

    這篇文章主要為大家詳細(xì)介紹了如何利用C#語言實現(xiàn)自定義光標(biāo)、并動態(tài)切換光標(biāo)類型,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-07-07

最新評論