Download Here:Basic Calc
Screenshoot:
=============================Code=================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace calculate
{
public partial class basic_calculate : Form
{
int leijia = 0;
string temp = "";
Boolean kai = true;
double num1;
double num2;
string last_opreater="";
string now_opreater="";
public basic_calculate()
{
InitializeComponent();
cshow.Text = "0.";
mes.Text = "先输数,后取负";
}
public void cshow_add(string point)
{
mes.Text = "";
if (leijia < 40)
{
temp = temp + point;
cshow.Text = temp;
num2 = Double.Parse(temp);
leijia++;
}
else
{
mes.Text = "数字不能再多了!";
}
}
private void bdot_Click(object sender, EventArgs e)
{
if (kai == true) { cshow_add("."); }
kai = false;
}
public void go(string opreater)
{
now_opreater = opreater;
switch (last_opreater)
{
case "+":
num1 = num1 + num2;
cshow.Text = num1.ToString();
break;
case "-":
num1 = num1 - num2;
cshow.Text = num1.ToString();
break;
case "*":
num1 = num1 * num2;
cshow.Text = num1.ToString();
break;
case "/":
if (num2 != 0)
{
num1 = num1 / num2;
cshow.Text = num1.ToString();
}
else
{
mes.Text = "被除数不能为零,计算器已清零";
cshow.Text = "0";
leijia = 0;
temp = "";
last_opreater = "";
};
break;
case "":
num1 = num2;
leijia = 0;
temp = "";
break;
}
temp = "";
num2=0;
last_opreater = now_opreater;
}
private void b0_Click(object sender, EventArgs e)
{
cshow_add("0");
}
private void b1_Click_1(object sender, EventArgs e)
{
cshow_add("1");
}
private void b2_Click_1(object sender, EventArgs e)
{
cshow_add("2");
}
private void b3_Click(object sender, EventArgs e)
{
cshow_add("3");
}
private void b4_Click(object sender, EventArgs e)
{
cshow_add("4");
}
private void b5_Click(object sender, EventArgs e)
{
cshow_add("5");
}
private void b6_Click_1(object sender, EventArgs e)
{
cshow_add("6");
}
private void b7_Click(object sender, EventArgs e)
{
cshow_add("7");
}
private void b8_Click(object sender, EventArgs e)
{
cshow_add("8");
}
private void b9_Click(object sender, EventArgs e)
{
cshow_add("9");
}
private void bjia_Click(object sender, EventArgs e)
{
go("+");
}
private void bjian_Click_1(object sender, EventArgs e)
{
go("-");
}
private void bcheng_Click_1(object sender, EventArgs e)
{
go("*");
}
private void bchu_Click_1(object sender, EventArgs e)
{
go("/");
}
public void bqinling_Click(object sender, EventArgs e)
{
cshow.Text = "0";
leijia = 0;
temp = "";
last_opreater = "";
mes.Text = "";
}
private void bpinfang_Click(object sender, EventArgs e)
{
num2 = Math.Pow(num2, 2);
cshow.Text = num2.ToString();
temp = "";
}
private void bkaifang_Click(object sender, EventArgs e)
{
num2 = Math.Sqrt(num2);
cshow.Text = num2.ToString();
temp = "";
}
private void bdeng_Click(object sender, EventArgs e)
{
go("");
}
private void bfu_Click_1(object sender, EventArgs e)
{
num2 = -num2;
temp = num2.ToString();
cshow.Text = temp;
}
}
}
========================end code=============================