溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用C#怎么實(shí)現(xiàn)一個(gè)加減乘除計(jì)算器

發(fā)布時(shí)間:2021-04-16 16:19:04 來源:億速云 閱讀:263 作者:Leah 欄目:編程語言

使用C#怎么實(shí)現(xiàn)一個(gè)加減乘除計(jì)算器?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
 
namespace WindowsApplication1 
{ 
 public partial class Main : Form 
 { 
  public Main() 
  { 
   InitializeComponent(); 
  } 
 
  private void Main_Load(object sender, EventArgs e) 
  { 
 
  } 
 
  private void txtInshu1_TextChanged(object sender, EventArgs e) 
  { 
 
  } 
 
  private void txtInshu1_KeyPress(object sender, KeyPressEventArgs e) 
  { 
   OnlyEnterNumber(sender, e); 
  } 
 
  //// <summary> 
  /// 只能輸入數(shù)字(含負(fù)號小數(shù)點(diǎn)) 
  /// </summary> 
  /// <param name="sender"></param> 
  /// <param name="e"></param> 
  public static void OnlyEnterNumber(object sender, KeyPressEventArgs e) 
  { 
   if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46) 
   { 
    e.Handled = true; 
   } 
 
   // 輸入為負(fù)號時(shí),只能輸入一次且只能輸入一次 
   if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true; 
   if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true; 
  } 
 
 
  /* 
   * 參數(shù):d表示要四舍五入的數(shù);i表示要保留的小數(shù)點(diǎn)后位數(shù)。 
   * 正負(fù)數(shù)都四舍五入,適合數(shù)據(jù)統(tǒng)計(jì)的顯示 
   */ 
  double Round(double d, int i) 
  { 
   if (d >= 0) 
   { 
    d += 5 * Math.Pow(10, -(i + 1)); 
   } 
   else 
   { 
    d += -5 * Math.Pow(10, -(i + 1)); 
   } 
   string str = d.ToString(); 
   string[] strs = str.Split('.'); 
   int idot = str.IndexOf('.'); 
   string prestr = strs[0]; 
   string poststr = strs[1]; 
   if (poststr.Length > i) 
   { 
    poststr = str.Substring(idot + 1, i); 
   } 
   string strd = prestr + "." + poststr; 
   d = Double.Parse(strd); 
   return d; 
  } 
 
  private void txtInshu2_TextChanged(object sender, EventArgs e) 
  { 
 
  } 
 
  private void txtInshu2_KeyPress_1(object sender, KeyPressEventArgs e) 
  { 
   OnlyEnterNumber(sender, e); 
  } 
 
  private void btnJisuan_Click(object sender, EventArgs e) 
  { 
   if (txtInshu1.Text == "") { 
    MessageBox.Show("因數(shù)1不能為空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
    return; 
   } 
   if (txtInshu2.Text == "") 
   { 
    MessageBox.Show("因數(shù)2不能為空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
    return; 
   } 
 
   double inshu1 = Convert.ToDouble(txtInshu1.Text); 
   double inshu2 = Convert.ToDouble(txtInshu2.Text); 
   double result = 0.0; 
 
   if (radioBtnJia.Checked) { 
    result = inshu1 + inshu2; 
   } 
 
   if (radioBtnJian.Checked) 
   { 
    result = inshu1 - inshu2; 
   } 
 
   if (radioBtnCheng.Checked) 
   { 
    result = inshu1 * inshu2; 
   } 
 
   if (radioBtnChu.Checked) 
   { 
    if (0 == inshu2) 
    { 
     MessageBox.Show("因數(shù)2做除數(shù)不能為0!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     return; 
    } 
    result = inshu1 / inshu2; 
    result = Round(result, 6); 
   } 
 
   txtResult.Text = Convert.ToString(result); 
  } 
 } 
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI