- UID
- 36978
注册时间2007-11-4
阅读权限20
最后登录1970-1-1
以武会友
该用户从未签到
|
这个是我初学C#时做的计算器.
功能很简单.
尚存BUG:100.02-100计算结果出错..还有其他的暂时没有找到
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;
/* -------------bug ---------
* 连续运算错误 解决
* 无法锁定窗体大小 解决
*
*/
namespace 计算器
{
public partial class calc : Form
{
public calc()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
double num1,num2;//第一个数值,第二个数值或运算基数
double num4;//储存值
double num6;//储存复制粘贴中的值
string btn_a;//定义运算符
bool cz = false; //判断等于按钮和运算按钮是否已经被按下过,用于清空输入框的值
bool cz1; //判断等于按钮
bool db; //判断是否已经输入过小数点
bool op_bl; //判断运算键是否已经被按下过
bool op2 = true;//判断运算键第二次被按下
int flag; //定义标记
//格式化输入框
private void inbox_TextChanged(object sender, EventArgs e)
{
if (inbox.Text == "")
{
inbox.Text = "0";
}
}
//数字键传值
private void btn_1_Click(object sender, EventArgs e)
{
if (cz == true)
{
inbox.Text = "";
cz = false;
}
string btn_t = ((Button)sender).Text;
op2 = false;
if (inbox.Text == "0")
{
if (btn_t == ".")
{
inbox.Text = "0.";
}
else
{
inbox.Text = btn_t;
}
}
else
{
if (btn_t == ".")
{
int tb;
db = int.TryParse(inbox.Text,out tb);
if (db == false)
{
MessageBox.Show("已经输入了一位小数点");
}
else
{
inbox.Text += ".";
}
}
else
{
inbox.Text += btn_t;
}
}
}
//清空
private void btn_c_Click(object sender, EventArgs e)
{
inbox.Text = string.Empty;
num1 = 0;
num2 = 0;
op2 = true;
}
//运算键 精简ing..
private void btn_exc_Click(object sender, EventArgs e)
{
if (op_bl == false)
{
btn_a = ((Button)sender).Text;
num1 = Convert.ToDouble(inbox.Text);
op_bl = true;
}
else
{
if (op2 == false)
{
num2 = Convert.ToDouble(inbox.Text);
switch (btn_a)
{
case "+":
num1 += num2;
break;
case "-":
num1 -= num2;
break;
case "*":
num1 *= num2;
break;
case "/":
if (num2 == 0)
{
MessageBox.Show("不能除0");
break;
}
else
{
num1 /= num2;
break;
}
}
btn_a = ((Button)sender).Text;
}
inbox.Text = Convert.ToString(num1);
}
op2 = true;
cz = true;
cz1 = false;
}
//等于 结果计算
private void btn_mean_Click(object sender, EventArgs e)
{
if (cz1 == false)
{
num2 = Convert.ToDouble(inbox.Text);
}
cz1 = true;
cz = true;
op_bl = false;
switch (btn_a)
{
case "+":
num1 += num2;
break;
case "-":
num1 -= num2;
break;
case "*":
num1 *= num2;
break;
case "/":
if (num2 == 0)
{
MessageBox.Show("不能除0");
break;
}
else
{
num1 /= num2;
break;
}
}
inbox.Text = Convert.ToString(num1);
}
/*----------------------特殊按钮----------------------*/
// CE ???
private void btn_ce_Click(object sender, EventArgs e)
{
inbox.Text = string.Empty;
}
// +/- 转换按钮
private void btn_jj_Click(object sender, EventArgs e)
{
if (inbox.Text[0] != '-')
{
inbox.Text = "-" + inbox.Text;
}
else
{
inbox.Text = inbox.Text.Substring(1);
}
}
//退格键
private void btn_bs_Click(object sender, EventArgs e)
{
if (inbox.Text.Length >= 1)
{
inbox.Text = inbox.Text.Substring(0, inbox.Text.Length - 1);
}
if (inbox.Text.Length == 0)
{
inbox.Text = "0";
}
}
//开平方 Math.Sqrt 方法 返回指定数字的平方根。
private void btn_sqrt_Click(object sender, EventArgs e)
{
double num3;
num3 = Convert.ToDouble(inbox.Text);
if (inbox.Text[0] == '-')
{
inbox.Text = "负数不能开平方";
}
else
{
num3 = Math.Sqrt(num3);
inbox.Text = num3.ToString();
}
}
//百分比 Percentage
private void btn_per_Click(object sender, EventArgs e)
{
double pernum;
pernum = Convert.ToDouble(inbox.Text);
pernum = pernum / 100;
inbox.Text = pernum.ToString();
}
//倒数 Countdown
private void btn_cou_Click(object sender, EventArgs e)
{
double counum;
counum = Convert.ToDouble(inbox.Text);
if (counum == 0)
{
inbox.Text = "除数不可以为0!";
}
else
{
counum = 1 / counum;
inbox.Text = counum.ToString();
}
}
//MS 存储当前的值 全局变量?
private void btn_ms_Click(object sender, EventArgs e)
{
num4 = Convert.ToDouble(inbox.Text);
stuBox.Text = "M";
}
//MR 显示储存的值
private void btn_mr_Click(object sender, EventArgs e)
{
inbox.Text = num4.ToString();
}
//M+ 将储存的结果与现在的值相加并储存
private void btn_m_Click(object sender, EventArgs e)
{
double num5;
num5 = Convert.ToDouble(inbox.Text);
num4 += num5;
inbox.Text = num4.ToString();
}
//MC 清空储存的数值
private void btn_mc_Click(object sender, EventArgs e)
{
num4 = 0;
stuBox.Text = string.Empty;
}
/*--------------------------------菜单栏-----------------------------------*/
//复制
private void 复制CtrlToolStripMenuItem_Click(object sender, EventArgs e)
{
num6 = Convert.ToDouble(inbox.Text);
}
//粘贴
private void 粘贴CtrlVToolStripMenuItem_Click(object sender, EventArgs e)
{
num6 += Convert.ToDouble(inbox.Text);
inbox.Text =num6.ToString();
}
}
} |
|