基本信息
源码名称:C# 简易计算器 入门级示例
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2018-05-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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 简易计算器
{
public partial class calc : Form
{
private bool ClearDisplay = true;
private bool Spequal = true; //判断等号是否按下
private bool Spresult = true; //控制result时不能<-
private bool Spfirst = true; //控制初始为0
private bool Spdis=false;//Operand1解决
private bool Spcomma=true;
// private bool Spoperator1;
// private bool Spoperator = false;//判断是否 -*/连续出现
private string Operator="=",Operator1="=";
private double Operand1;
private double Operand2;
private double result;
private System.Windows.Forms.Button btn;
public calc()
{
InitializeComponent();
if (Spfirst)
{
txtDisplay.Text = "0";
ClearDisplay = true;
Operand1 =Operand2 = 0;
Spfirst = false;
}
}
private void calc_Load(object sender, EventArgs e)
{
}
private void button_11_Click(object sender, EventArgs e) //C 清所有
{
Operand1 = 0;
Operand2 = 0;
result = 0;
txtDisplay.Text = "0";
Spequal = true;
ClearDisplay = true;
Spresult = false;
Spdis = false;
Operator1 = "=";
Operator = "=";
}
private void handleDigits(object sender, EventArgs e)
{
/* if ((Operator!="=")||(Operator1!="="))
{
Spoperator = true;
}
if (Spoperator)
{
//double result = 0;
Operand2 = System.Convert.ToDouble(txtDisplay.Text);
switch (Operator)
{
case " ":
Operand1 = Operand1 Operand2;
break;
case "-":
Operand1 = Operand1 - Operand2;
break;
case "*":
Operand1 = Operand1 * Operand2;
break;
case "/":
Operand1 = Operand1 / Operand2;
break;
case "^":
Operand1 = System.Math.Pow(Operand1, Operand2);
break;
}
txtDisplay.Text = Operand1.ToString();
ClearDisplay = true;
Spoperator = false;
// Spequal = false; //等号是否按下
// Spresult = false; //为结果时不能执行<-
}
else{
btn = (Button)sender;
if (ClearDisplay)
{
txtDisplay.Text = "";
ClearDisplay = false;
}
txtDisplay.Text = txtDisplay.Text btn.Text;
Spresult = true;
}*/
btn = (Button)sender;
if (ClearDisplay)
{
txtDisplay.Text = "";
ClearDisplay = false;
}
//////改
if (txtDisplay.Text.IndexOf(".") == -1)
{
txtDisplay.Text = txtDisplay.Text btn.Text;
Spresult = true; //控制result时不能 < -
Spdis = true;//Operand1解决
Spequal = true;
}
else
{
if(btn.Text ==".")
{
txtDisplay.Text = txtDisplay.Text;
}
else
{
txtDisplay.Text = txtDisplay.Text btn.Text ;
Spresult = true; //控制result时不能 < -
Spdis = true;//Operand1解决
Spequal = true;
}
}
/*原始
txtDisplay.Text = txtDisplay.Text btn.Text;
Spresult = true; //控制result时不能 < -
Spdis = true;//Operand1解决
Spequal = true;*/
// Operator1 = Operator;
}
private void handleoperator(object sender, EventArgs e)//操作符
{
// if (Spequal) //判断符号是否按下
// {
btn = (Button)sender;
Operator = btn.Text;
if ((Operator != "=") && (Operator1 != "=") && (Spdis))//Operand1解决
{
//txtDisplay.Text = "进了1";
switch (Operator1)
{
case " ":
Operand1 = Operand1 System.Convert.ToDouble(txtDisplay.Text);//txtDisplay.Text = "进了2";
txtDisplay.Text = Operand1.ToString();
break;
case "-":
Operand1 = Operand1 - System.Convert.ToDouble(txtDisplay.Text);//txtDisplay.Text = "进了3";
txtDisplay.Text = Operand1.ToString();
break;
case "*":
Operand1 = Operand1 * System.Convert.ToDouble(txtDisplay.Text);
txtDisplay.Text = Operand1.ToString();
break;
case "/":
Operand1 = Operand1 / System.Convert.ToDouble(txtDisplay.Text);
txtDisplay.Text = Operand1.ToString();
break;
case "^":
Operand1 = System.Math.Pow(Operand1, System.Convert.ToDouble(txtDisplay.Text));
txtDisplay.Text = Operand1.ToString();
break;
}
ClearDisplay = true;
// ***Spequal = true;
Spdis = false; //Operand1解决
Spresult = false; //等号没有按下,等号可使用
Operator1 = Operator;
Spequal = true;
Spfirst = false;
}
else
{
Operand1 = System.Convert.ToDouble(txtDisplay.Text);
ClearDisplay = true;
//***Spequal = true;
Spdis = false; //Operand1解决
Operator1 = Operator;
Spequal = true;
Spresult = false;
Spfirst = false;
}
//}
/*原始
btn = (Button)sender;
Operator = btn.Text;
Operand1 = System.Convert.ToDouble(txtDisplay.Text);
//txtDisplay.Text = "";
ClearDisplay = true;
Spequal = true;*/
}
private void button_20_Click(object sender, EventArgs e) //=
{
if (Spequal && Spdis) //Spdis -Operand1解决
{
double result = 0;
Operand2 = System.Convert.ToDouble(txtDisplay.Text);
switch (Operator)
{
case " ":
result = Operand1 Operand2;
break;
case "-":
result = Operand1 - Operand2;
break;
case "*":
result = Operand1 * Operand2;
break;
case "/":
result = Operand1 / Operand2;
break;
case "^":
result = System.Math.Pow(Operand1, Operand2);
break;
}
txtDisplay.Text = result.ToString();
ClearDisplay = true;
Spequal = false; //判断等号是否按下
Spresult = false; //控制result时不能<-
Operand1 = Operand2 = 0;
//Operator1 = "=";
}
Operator = "=";
Operator1 = "=";
}
private void button_18_Click(object sender, EventArgs e) // -/
{
result = -Convert.ToDouble(txtDisplay.Text);
txtDisplay.Text = result.ToString();
}
private void button_19_Click(object sender, EventArgs e) // 1/x
{
if (txtDisplay.Text != "0")
{
result = 1.0 / Convert.ToDouble(txtDisplay.Text);
txtDisplay.Text = result.ToString();
ClearDisplay = true;
}
}
private void button_22_Click(object sender, EventArgs e) //CE 清除目前
{
txtDisplay.Text = "0";
ClearDisplay = true;
Spequal = true;
Spdis = false;
}
private void button_21_Click(object sender, EventArgs e) // <-
{
if(Spresult)
{
if (txtDisplay.Text.Length > 0)
{
txtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1);
if (txtDisplay.Text.Length == 0)
{
txtDisplay.Text = "0";
ClearDisplay = true;
}
}
}
}
}
}