最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)簡易計(jì)算器

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

C#編寫一個(gè)簡易計(jì)算器,供大家參考,具體內(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;
 }

 //計(jì)算
 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 + ")"; //保證左括號(hào)數(shù)量大于右括號(hào)

  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ù)號(hào)
   {
    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 //將 + - 號(hào)前面的運(yùn)算符優(yōu)先級(jí)高的結(jié)算,加,減優(yōu)先級(jí)最低。前面都可以結(jié)算
   {
    while ((char)ops.Peek() != '(') Cal();
    ops.Push(c);
   }
   }
   else if (c == '*' || c == '/' || c == '%') //將 * / 號(hào)前面的運(yùn)算符優(yōu)先級(jí)高或等于的結(jié)算
   {
   while ((char)ops.Peek() == '*' || (char)ops.Peek() == '/' || (char)ops.Peek() == '^' || (char)ops.Peek() == '%') Cal();
   ops.Push(c);
   }
   else if (c == '^') //將 '^' 號(hào)前面的運(yùn)算符優(yōu)先級(jí)高或等于的結(jié)算
   {
   while ((char)ops.Peek() == '^') Cal();
   ops.Push(c);
   }
   else if (c == ')') // 將整個(gè)括號(hào)結(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#如何生成唯一訂單號(hào)

    C#如何生成唯一訂單號(hào)

    這篇文章主要為大家詳細(xì)介紹了C#如何生成唯一訂單號(hào),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • C#編寫的生辰八字計(jì)算程序

    C#編寫的生辰八字計(jì)算程序

    這篇文章主要介紹了C#編寫的生辰八字計(jì)算程序,假設(shè)一個(gè)人的公歷出生時(shí)間,范圍必須要在2012-2015年之間,因?yàn)楸臼纠绦蛑惶峁┝诉@幾年的農(nóng)歷數(shù)據(jù),小伙伴們參考下,可以自由擴(kuò)展
    2015-03-03
  • 淺談C#2.0泛型中的變化:default關(guān)鍵字

    淺談C#2.0泛型中的變化:default關(guān)鍵字

    下面就詳細(xì)的說明一下。之所以會(huì)用到default關(guān)鍵字,是因?yàn)樾枰诓恢李愋蛥?shù)為值類型還是引用類型的情況下,為對象實(shí)例賦初值
    2013-09-09
  • C#控制臺(tái)實(shí)現(xiàn)飛行棋游戲

    C#控制臺(tái)實(shí)現(xiàn)飛行棋游戲

    這篇文章主要為大家詳細(xì)介紹了C#控制臺(tái)實(shí)現(xiàn)飛行棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • C#簡單實(shí)現(xiàn)子窗體向父窗體傳值的方法

    C#簡單實(shí)現(xiàn)子窗體向父窗體傳值的方法

    這篇文章主要介紹了C#簡單實(shí)現(xiàn)子窗體向父窗體傳值的方法,以實(shí)例形式較為詳細(xì)的分析了C#窗體間傳值的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • C#利用Spire.Pdf包實(shí)現(xiàn)為PDF添加數(shù)字簽名

    C#利用Spire.Pdf包實(shí)現(xiàn)為PDF添加數(shù)字簽名

    Spire.PDF for .NET 是一款專業(yè)的基于.NET平臺(tái)的PDF文檔控制組件。它能夠讓開發(fā)人員在不使用Adobe Acrobat和其他外部控件的情況下,運(yùn)用.NET 應(yīng)用程序創(chuàng)建,閱讀,編寫和操縱PDF 文檔。本文將利用其實(shí)現(xiàn)添加數(shù)字簽名,需要的可以參考一下
    2022-08-08
  • C# List實(shí)現(xiàn)行轉(zhuǎn)列的通用方案

    C# List實(shí)現(xiàn)行轉(zhuǎn)列的通用方案

    本篇通過行轉(zhuǎn)列引出了System.Linq.Dynamic,并且介紹了過濾功能,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • C#端口轉(zhuǎn)發(fā)用法詳解

    C#端口轉(zhuǎn)發(fā)用法詳解

    這篇文章主要介紹了C#端口轉(zhuǎn)發(fā)用法,以實(shí)例形式較為詳細(xì)的分析了C#實(shí)現(xiàn)端口轉(zhuǎn)發(fā)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • C#在圖片增加文字的實(shí)現(xiàn)代碼

    C#在圖片增加文字的實(shí)現(xiàn)代碼

    最近做項(xiàng)目需要?jiǎng)討B(tài)給圖片增加文字(書本的封面圖片),修改字體大小、字體、顏色、控制位置等,下面通過實(shí)例代碼給大家分享C#在圖片增加文字的實(shí)現(xiàn)代碼,一起看看吧
    2017-06-06
  • c# ArrayList的使用方法小總結(jié)

    c# ArrayList的使用方法小總結(jié)

    arraylist是接口list的實(shí)現(xiàn)類,所以在使用過程中比較推薦使用list接口來實(shí)現(xiàn),arraylist在程序開發(fā)過程中應(yīng)用非常廣泛,接下來,腳本之家的小編給大家總結(jié)了arraylist的使用,有需要的朋友可以參考下
    2015-09-09

最新評論

怀远县| 福泉市| 连州市| 房产| 全州县| 恩施市| 枣庄市| 温州市| 天祝| 武宣县| 江都市| 平塘县| 漳州市| 黔江区| 崇礼县| 龙游县| 海伦市| 睢宁县| 东光县| 晋宁县| 定日县| 视频| 蓝田县| 吉木萨尔县| 深泽县| 屏东市| 扎赉特旗| 河南省| 江陵县| 黄山市| 南开区| 九寨沟县| 武冈市| 北票市| 黎城县| 高州市| 望江县| 米林县| 望谟县| 富平县| 邯郸市|