计算器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CalDesWindowsApplication
{
public partial class Form1 : Form
{
float numFirst;
string oprator;
int flagDot = 0;
bool pressDot = false;
float numberRember;
public Form1()
{
InitializeComponent();
}
private void delZero(string str)
{
if (str != "")
{
if (str.Substring(0, 1) == "0")
{
if (flagDot != 1)
{
txtShow.Text = "";
flagDot = 0;
}
}
}
}
private void btnBackspace_Click(object sender, EventArgs e)
{
string str = txtShow.Text, strTest;
if (txtShow.Text.Length != 0)
{
strTest = str.Substring(str.Length - 1, 1);
if(strTest=="."){
if(str.Substring(0,1)=="0"){
txtShow.Text = "";
pressDot = false;
}
}
txtShow.Text = str.Substring(0, str.Length - 1);
}
else
{
txtShow.Text = "";
}
flagDot = 0;
}
private void btnC_Click(object sender, EventArgs e)
{
pressDot = false;
txtShow.Text = "";
}
private void btn7_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 7;
}
private void btn8_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 8;
}
private void btn9_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 9;
}
private void btn4_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 4;
}
private void btn5_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 5;
}
private void btn6_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 6;
}
private void btn1_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 1;
}
private void btn2_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 2;
}
private void btn3_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 3;
}
private void btn0_Click(object sender, EventArgs e)
{
delZero(txtShow.Text);
txtShow.Text = txtShow.Text + 0;
}
private void btnDot_Click(object sender, EventArgs e)
{
flagDot = 1;
delZero(txtShow.Text);
if (pressDot == false)
{
pressDot = true;
txtShow.Text = txtShow.Text + ".";
}
}
private void btnDev_Click(object sender, EventArgs e)
{
numFirst = float.Parse(txtShow.Text);
oprator = "/";
txtShow.Text = "";
pressDot = false;
}
private void btnMul_Click(object sender, EventArgs e)
{
numFirst = float.Parse(txtShow.Text);
oprator = "*";
txtShow.Text = "";
pressDot = false;
}
private void btnSub_Click(object sender, EventArgs e)
{
numFirst = float.Parse(txtShow.Text);
oprator = "-";
txtShow.Text = "";
pressDot = false;
}
private void btnAdd_Click(object sender, EventArgs e)
{
numFirst = float.Parse(txtShow.Text);
oprator = "+";
txtShow.Text = "";
pressDot = false;
}
private void btnEqu_Click(object sender, EventArgs e)
{
float result;
if (oprator == "/")
{
result = numFirst / (float.Parse(txtShow.Text));
txtShow.Text = result.ToString();
}
else if (oprator == "+")
{
result = numFirst + (float.Parse(txtShow.Text));
txtShow.Text = result.ToString();
}
else if (oprator == "*")
{
result = numFirst * (float.Parse(txtShow.Text));
txtShow.Text = result.ToString();
}
else if (oprator == "-")
{
result = numFirst - (float.Parse(txtShow.Text));
txtShow.Text = result.ToString();
}
else {
result = numFirst %(float.Parse(txtShow.Text));
txtShow.Text = result.ToString();
}
}
private void btnRem_Click(object sender, EventArgs e)
{
numberRember = float.Parse(txtShow.Text);
}
private void btnGet_Click(object sender, EventArgs e)
{
txtShow.Text = numberRember.ToString();
}
private void button21_Click(object sender, EventArgs e)
{
numFirst = float.Parse(txtShow.Text);
oprator = "%";
txtShow.Text = "";
pressDot = false;
}
private void button22_Click(object sender, EventArgs e)
{
float numTra;
numTra = float.Parse(txtShow.Text);
numTra = -numTra;
txtShow.Text = numTra.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}