c#窗口科学计算机,c#窗口科学计算器连等如何实现,大神帮忙一下好么?
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
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; //表示如果再輸入數(shù)字或小數(shù)點(diǎn),先清除編輯框中顯示的前一個加數(shù)
string strOper = "+"; //記錄輸入的運(yùn)算符
double dbSencond;//保存第二個數(shù)據(jù)
public void addNum(int num)//添加數(shù)字
{
textBox1.Text = textBox1.Text + num.ToString();
//如果顯示框的第一個字符為0,而且第二個字符不是小數(shù)點(diǎn),則自動去掉第一個字符‘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) //如果為真,輸入下一個加數(shù)前,先清除textBox1顯示內(nèi)容
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0") //如果前面已經(jīng)輸入非零數(shù)字,如:12
textBox1.Text += b1.Text; //此時(shí)鍵入0,應(yīng)為120
else
textBox1.Text = b1.Text; //如已輸入的數(shù)字為0,顯示的數(shù)字應(yīng)為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) //如果未輸入第二個操作符,不運(yùn)算,例如連續(xù)輸入+-*/或=
{
switch (strOper) //按上次記錄的運(yùn)算符號運(yùn)算
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
textBox1.Text = Convert.ToString(sum); // 顯示部分和
blnClear = true; // 設(shè)置標(biāo)記使鍵入另一個加數(shù)前,先清除顯示的前個加數(shù)
}
//等于
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;
}
//清除當(dāng)前顯示數(shù)字
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = "0";//顯示屏清空
sum = 0; //臨時(shí)計(jì)算結(jié)果歸零
blnClear = false;
strOper = "+";
}
//清除當(dāng)前計(jì)算
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
sum = 0;
blnClear = false;
strOper = "+";
}
//小數(shù)點(diǎn)
private void button30_Click(object sender, EventArgs e)
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
int n = textBox1.Text.IndexOf(".");
if (n == -1) //如果沒有小數(shù)點(diǎn),增加小數(shù)點(diǎn),否則不增加
textBox1.Text = textBox1.Text + ".";
}
//正負(fù)符號
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)
{
}
}
}
總結(jié)
以上是生活随笔為你收集整理的c#窗口科学计算机,c#窗口科学计算器连等如何实现,大神帮忙一下好么?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年朱少醒管理的基金有哪些 202
- 下一篇: lua运行外部程序_一起聊聊redis(