Winfrom编写简单的计算器
来源:互联网 发布:ubuntu怎么安装deb包 编辑:程序博客网 时间:2024/06/06 02:48
简单的计算器
基本布局:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class calculator : Form
{
public calculator()
{
InitializeComponent();
lb_1.Text = null;
}
private void calculator_Load(object sender, EventArgs e)
{
}
private string FuHao;
private float? Mun;
private float? Num;
private bool IsCounting = false;
private bool IsHaving = false;
private bool IsJiSuan = false;
private void ShuRu(string shu)
{
if(!IsCounting)
{
txtShuchu.Text = txtShuchu.Text + shu;
}
else
{
txtShuchu.Text = "";
txtShuchu.Text = txtShuchu.Text + shu;
IsCounting = false;
}
}
private void XianShi(string fuhao)
{
if (!IsHaving)
{
Mun = float.Parse(txtShuchu.Text);
IsHaving = true;
}
else
{
Num = float.Parse(txtShuchu.Text);
}
if((lb_1.Text.EndsWith("+")|| lb_1.Text.EndsWith("-")|| lb_1.Text.EndsWith("×") || lb_1.Text.EndsWith("÷")||lb_1.Text.EndsWith("="))&&IsCounting)
{
lb_1.Text = lb_1.Text.Remove(lb_1.Text.Length - 1, 1);
}
else
{
lb_1.Text +=txtShuchu.Text;
if(IsJiSuan)
{
Mun=JiSuan(Mun, Num);
txtShuchu.Text = Mun.ToString();
}
}
FuHao = fuhao;
lb_1.Text += FuHao;
IsCounting = true;
IsJiSuan = true;
}
private float? JiSuan(float? a,float? b)
{
float? c=0;
switch(FuHao)
{
case "+":
c = a + b;
break;
case "-":
c = a - b;
break;
case "×":
c = a * b;
break;
case "÷":
if (b == 0)
{
MessageBox.Show("被除数不能为0");
}
else
{
c = a / b;
}
break;
case "=":
c = a;
break;
default:
break;
}
return c;
}
#region btn_click_munber
private void btn_1_Click(object sender, EventArgs e)
{
ShuRu(btn_1.Text);
}
private void btn_2_Click(object sender, EventArgs e)
{
ShuRu(btn_2.Text);
}
private void btn_3_Click(object sender, EventArgs e)
{
ShuRu(btn_3.Text);
}
private void btn_4_Click(object sender, EventArgs e)
{
ShuRu(btn_4.Text);
}
private void btn_5_Click(object sender, EventArgs e)
{
ShuRu(btn_5.Text);
}
private void btn_6_Click(object sender, EventArgs e)
{
ShuRu(btn_6.Text);
}
private void btn_7_Click(object sender, EventArgs e)
{
ShuRu(btn_7.Text);
}
private void btn_8_Click(object sender, EventArgs e)
{
ShuRu(btn_8.Text);
}
private void btn_9_Click(object sender, EventArgs e)
{
ShuRu(btn_9.Text);
}
private void btn_0_Click(object sender, EventArgs e)
{
ShuRu(btn_0.Text);
}
#endregion
#region btn_click_zhiling
private void btn_jia_Click(object sender, EventArgs e)
{
XianShi(btn_jia.Text);
}
private void btn_jian_Click(object sender, EventArgs e)
{
XianShi(btn_jian.Text);
}
private void btn_cheng_Click(object sender, EventArgs e)
{
XianShi(btn_cheng.Text);
}
private void btn_chu_Click(object sender, EventArgs e)
{
XianShi(btn_chu.Text);
}
private void btn_deng_Click(object sender, EventArgs e)
{
XianShi(btn_deng.Text);
}
private void btn_clear_Click(object sender, EventArgs e)
{
lb_1.Text = "";
txtShuchu.Text = "";
Mun = null;
Num = null;
IsCounting = false;
IsHaving = false;
IsJiSuan = false;
}
}
#endregion
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class calculator : Form
{
public calculator()
{
InitializeComponent();
lb_1.Text = null;
}
private void calculator_Load(object sender, EventArgs e)
{
}
private string FuHao;
private float? Mun;
private float? Num;
private bool IsCounting = false;
private bool IsHaving = false;
private bool IsJiSuan = false;
private void ShuRu(string shu)
{
if(!IsCounting)
{
txtShuchu.Text = txtShuchu.Text + shu;
}
else
{
txtShuchu.Text = "";
txtShuchu.Text = txtShuchu.Text + shu;
IsCounting = false;
}
}
private void XianShi(string fuhao)
{
if (!IsHaving)
{
Mun = float.Parse(txtShuchu.Text);
IsHaving = true;
}
else
{
Num = float.Parse(txtShuchu.Text);
}
if((lb_1.Text.EndsWith("+")|| lb_1.Text.EndsWith("-")|| lb_1.Text.EndsWith("×") || lb_1.Text.EndsWith("÷")||lb_1.Text.EndsWith("="))&&IsCounting)
{
lb_1.Text = lb_1.Text.Remove(lb_1.Text.Length - 1, 1);
}
else
{
lb_1.Text +=txtShuchu.Text;
if(IsJiSuan)
{
Mun=JiSuan(Mun, Num);
txtShuchu.Text = Mun.ToString();
}
}
FuHao = fuhao;
lb_1.Text += FuHao;
IsCounting = true;
IsJiSuan = true;
}
private float? JiSuan(float? a,float? b)
{
float? c=0;
switch(FuHao)
{
case "+":
c = a + b;
break;
case "-":
c = a - b;
break;
case "×":
c = a * b;
break;
case "÷":
if (b == 0)
{
MessageBox.Show("被除数不能为0");
}
else
{
c = a / b;
}
break;
case "=":
c = a;
break;
default:
break;
}
return c;
}
#region btn_click_munber
private void btn_1_Click(object sender, EventArgs e)
{
ShuRu(btn_1.Text);
}
private void btn_2_Click(object sender, EventArgs e)
{
ShuRu(btn_2.Text);
}
private void btn_3_Click(object sender, EventArgs e)
{
ShuRu(btn_3.Text);
}
private void btn_4_Click(object sender, EventArgs e)
{
ShuRu(btn_4.Text);
}
private void btn_5_Click(object sender, EventArgs e)
{
ShuRu(btn_5.Text);
}
private void btn_6_Click(object sender, EventArgs e)
{
ShuRu(btn_6.Text);
}
private void btn_7_Click(object sender, EventArgs e)
{
ShuRu(btn_7.Text);
}
private void btn_8_Click(object sender, EventArgs e)
{
ShuRu(btn_8.Text);
}
private void btn_9_Click(object sender, EventArgs e)
{
ShuRu(btn_9.Text);
}
private void btn_0_Click(object sender, EventArgs e)
{
ShuRu(btn_0.Text);
}
#endregion
#region btn_click_zhiling
private void btn_jia_Click(object sender, EventArgs e)
{
XianShi(btn_jia.Text);
}
private void btn_jian_Click(object sender, EventArgs e)
{
XianShi(btn_jian.Text);
}
private void btn_cheng_Click(object sender, EventArgs e)
{
XianShi(btn_cheng.Text);
}
private void btn_chu_Click(object sender, EventArgs e)
{
XianShi(btn_chu.Text);
}
private void btn_deng_Click(object sender, EventArgs e)
{
XianShi(btn_deng.Text);
}
private void btn_clear_Click(object sender, EventArgs e)
{
lb_1.Text = "";
txtShuchu.Text = "";
Mun = null;
Num = null;
IsCounting = false;
IsHaving = false;
IsJiSuan = false;
}
}
#endregion
}
总结:
长时间的没有整体的写一写自己东西,一直感觉自己在原地踏步,这次的实验虽然有点简单,写的也并不是很好,但是证明了这么些时间大量的读代码还是有用的。
没什么难度。希望以后看到这些东西的时候能给自己一个狗屎的评价。
阅读全文
0 0
- Winfrom编写简单的计算器
- JS编写的简单计算器
- html 编写的简单计算器
- Java编写的简单的计算器
- 自己编写的一个简单的计算器
- 用java编写一个简单的计算器
- 用Java swing 编写简单的计算器
- 安卓编写简单的计算器
- C语言编写的简单计算器程序
- 使用html编写简单的网页计算器
- 编译原理简单计算器的编写
- 怎样用Java编写一个简单的计算器
- 利用Java语言编写简单的计算器
- Java语言编写计算器(简单的计算器)
- C++编写简单计算器
- javaScript编写简单计算器
- JavaScript编写简单计算器
- c#简单计算器编写
- 让终端支持播放mp3,移植mp3解码库libmad和madplay到嵌入式linux
- 大写字母换小写字母
- 系统设计原则
- hackerrank Word Order
- 习题6 6.5
- Winfrom编写简单的计算器
- 程序
- Servlet实现用户登录页面(通过数据库验证)
- 创建SDE企业级数据库报错——ST_Geometry 形状库路径可能无效
- Maven详解
- git上传文件时出现origin does not to be a git repository
- 面向对象【OOP】的设计原则
- 6.15
- Activity四种启动模式杂谈