C#编写一个计算器
编写一个计算器,练习在窗体上添加控件、调整控件的布局,设置或修改控件属性,编写事件处理程序的方法。
代码:
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 Lab07_1
{
public partial class form1 : Form
{
double a = 0;
double b = 0;
bool c = false;
string d;
public form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "1";
} private void textBox1_TextChanged(object sender, EventArgs e)
{ } private void button5_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "5"; } private void button13_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "0";
if (d == "/")
{
textBox1.Clear();
MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
} } private void button10_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "-"; } private void 雨轩计算机_Load(object sender, EventArgs e)
{ } private void button2_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "2"; } private void button7_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "7";
} private void button8_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "8"; } private void button9_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "9";
} private void button4_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "4"; } private void button6_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "6"; } private void button3_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "3"; } private void button14_Click(object sender, EventArgs e)
{
textBox1.Text = "";
} private void button15_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "+"; } private void button11_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "*"; } private void button12_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "/"; } private void button16_Click(object sender, EventArgs e)
{
switch (d)
{
case "+": a = b + double.Parse(textBox1.Text); break;
case "-": a = b - double.Parse(textBox1.Text); break;
case "*": a = b * double.Parse(textBox1.Text); break;
case "/": a = b / double.Parse(textBox1.Text); break;
case "平方": a = b * b; break;
case "开方": a =Math.Sqrt(b); break;
case "log": a = Math.Log10(b); break;
case "ln": a = Math.Log(b); break;
}
textBox1.Text = a + "";
c = true; } private void button20_Click(object sender, EventArgs e)
{
if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
else
{
c = true;
b = double.Parse(textBox1.Text);
d = "ln";
}
} private void button17_Click(object sender, EventArgs e)
{
if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
else
{
c = true;
b = double.Parse(textBox1.Text);
d = "平方";
}
} private void button18_Click(object sender, EventArgs e)
{
if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
else
{
c = true;
b = double.Parse(textBox1.Text);
d = "开方";
}
} private void button19_Click(object sender, EventArgs e)
{
if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
else
{
c = true;
b = double.Parse(textBox1.Text);
d = "log";
}
}
}
}
界面演示:


C#编写一个计算器的更多相关文章
- 用python编写一个计算器
# 1 - 2 * ((60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2)))# 通过Pyt ...
- [javascript] 编写一个计算器,实现加减法
1.代码 <script> function sum(){ //加法 var value1 = document.getElementById("num1").valu ...
- python 正则的使用 —— 编写一个简易的计算器
在 Alex 的博客上看到的对正则这一章节作业是编写一个计算器,要求能计算出下面的算式. 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 + ...
- 编写一个通用的Makefile文件
1.1在这之前,我们需要了解程序的编译过程 a.预处理:检查语法错误,展开宏,包含头文件等 b.编译:*.c-->*.S c.汇编:*.S-->*.o d.链接:.o +库文件=*.exe ...
- CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL
CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL +BIT祝威+悄悄在此留下版了个权的信息说: 开始 本文用step by step的方式,讲述如何使 ...
- .NET Core RC2发布在即,我们试着用记事本编写一个ASP.NET Core RC2 MVC程序
在.NET Core 1.0.0 RC2即将正式发布之际,我也应应景,针对RC2 Preview版本编写一个史上最简单的MVC应用.由于VS 2015目前尚不支持,VS Code的智能感知尚欠火候,所 ...
- 网络爬虫:使用Scrapy框架编写一个抓取书籍信息的爬虫服务
上周学习了BeautifulSoup的基础知识并用它完成了一个网络爬虫( 使用Beautiful Soup编写一个爬虫 系列随笔汇总 ), BeautifulSoup是一个非常流行的Python网 ...
- 作业二:个人编程项目——编写一个能自动生成小学四则运算题目的程序
1. 编写一个能自动生成小学四则运算题目的程序.(10分) 基本要求: 除了整数以外,还能支持真分数的四则运算. 对实现的功能进行描述,并且对实现结果要求截图. 本题发一篇随笔,内容包括: 题 ...
- 用Java语言编写一个简易画板
讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...
随机推荐
- mysql通过mysqldunp命令重做从库详细操作步骤
mysql通过mysqldunp命令重做从库详细操作步骤 背景 生产环境上的主从复制集群,因为一些异常或人为原因,在从库做了一些操作,导致主从同步失败.一般修复起来比较麻烦,通过重做mysql从库的方 ...
- dependencies与devDependencies中应该放哪些依赖
网上一般的解释都是,开发环境用devDependencies,生产环境用dependencies,说的很简明,但是这里有个问题是,哪些包需要放到devDependencies中,哪些包需要放到depe ...
- python+selenium+pycharm使用
上一章节讲了安装python及selenium,该章节讲解下使用pycharm编辑器进行代码编写 下载地址:http://www.jetbrains.com/pycharm/download/#sec ...
- SP2742题解
晚自习用10min推出结论,太屑了 设 \(S=\sum_{i=1}^n a_i\),很显然每个位置的答案 \(ans_i\) 只和 \(a_i\) 和 \(S\) 有关.让我们打个表,找一下规律: ...
- CF549G题解
变菜了,一年前做这种题10min出结论,现在对着样例胡半天都没结果 首先考虑从判断无解入手. 定义两个位置 \((i,j)\),若 \(a[i]=a[j]+(j-i)\),则 \(i\) 和 \(j\ ...
- kali换源及安装docker
kali换源及安装docker 以管理员身份运行. 换源,记得先备份, cp /etc/apt/sources.list /etc/apt/sources.list.bak 然后修改/etc/apt/ ...
- 内网渗透----域环境搭建(server 2012)
先确定两台服务器相通 1.配置静态IP与DNS 2.配置域服务 点击服务器管理器-添加角色和功能-下一步-添加AD域服务: 3.提升为域控制器 安装完成后,可在旗帜处选择提升为域控制器 添加新林 添加 ...
- 利用MSSQL getshell
此次复现使用的sql server 2000 和sql server 2008两个环境进行的 是在已知数据库密码的基础上进行的 0x01 MSSQL连接 连接MSSQL 2000 新建连接: 填写目的 ...
- sql注入之简要注入
#mysql简要注入流程 mysql注入简要流程如下图: 由于还没学习完先结合sqlilabs的第一关讲解信息收集的一部分和数据注入的information_schema的有据查询. #sqli-la ...
- 如何理解Node.js和JavaScript的关系
一.Javascript的引擎 浏览器一般有两个引擎,一个是Html引擎,一个是脚本引擎. JavaScript是一种脚本语言,最初用于浏览器的动态显示,方便操作页面数据和内容.但实际上,它也可以在浏 ...