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.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
double sum = 0;//記錄部分和
bool blnClear = false; //表示如果再輸入數字或小數點,先清除編輯框中顯示的前一個加數
string strOper = "+"; //記錄輸入的運算符
double dbSencond;//保存第二個數據
public void addNum(int num)//添加數字
{
textBox1.Text = textBox1.Text + num.ToString();
//如果顯示框的第一個字符為0,而且第二個字符不是小數點,則自動去掉第一個字符‘0’
if(textBox1.Text.Substring(0,1)=="0"&&textBox1.Text.Substring(1,1)!=".")
{
textBox1.Text=textBox1.Text.Substring(1);
}
}
//0
private void button28_Click(object sender, EventArgs e)
{
if (blnClear) //如果為真,輸入下一個加數前,先清除textBox1顯示內容
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0") //如果前面已經輸入非零數字,如:12
textBox1.Text += b1.Text; //此時鍵入0,應為120
else
textBox1.Text = b1.Text; //如已輸入的數字為0,顯示的數字應為0
//dbSencond = 0;
}
//1
private void button22_Click(object sender, EventArgs e)
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
//dbSencond = 1;
}
//除法
private void button14_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
{
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
if (sender == button14)
{
strOper = "/";
}
if (sender == button27)
{
strOper = "=";
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//乘法
private void button20_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
{
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
if (sender == button20)
{
strOper = "*";
}
if (sender == button27)
{
strOper = "=";
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//減法
private void button26_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//加法
private void button32_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear) //如果未輸入第二個操作符,不運算,例如連續輸入+-*/或=
{
switch (strOper) //按上次記錄的運算符號運算
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
textBox1.Text = Convert.ToString(sum); // 顯示部分和
blnClear = true; // 設置標記使鍵入另一個加數前,先清除顯示的前個加數
}
//等于
private void button27_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
{
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//清除當前顯示數字
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = "0";//顯示屏清空
sum = 0; //臨時計算結果歸零
blnClear = false;
strOper = "+";
}
//清除當前計算
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
sum = 0;
blnClear = false;
strOper = "+";
}
//小數點
private void button30_Click(object sender, EventArgs e)
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
int n = textBox1.Text.IndexOf(".");
if (n == -1) //如果沒有小數點,增加小數點,否則不增加
textBox1.Text = textBox1.Text + ".";
}
//正負符號
private void button8_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
textBox1.Text = Convert.ToString(-dbSencond);
}
//√
private void button9_Click(object sender, EventArgs e)
{
}
}
}
總結
以上是生活随笔為你收集整理的c#窗口科学计算机,c#窗口科学计算器连等如何实现,大神帮忙一下好么?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年朱少醒管理的基金有哪些 202
- 下一篇: lua运行外部程序_一起聊聊redis(