一个枯燥无味的C#计算器-Windows计算器
发布日期:2021-06-29 11:32:28
浏览次数:2
分类:技术文章
本文共 7290 字,大约阅读时间需要 24 分钟。
首先上效果图
可能很多地方处理得并没有很好,刚上这个C#窗口课反正是一脸蒙蔽。就被布置了个写计算器的作业。可能老师对我的理解能力太自信了,我是真的写哪写什么都没搞懂,写哪会显示出什么都不知道,反正胡搞毛搞交作业。本来是不太乐意交trash的人,但这我是真没懂。
不过既然发了,那观众照着这个一定是可以做出我这图上的效果的。看别的博客真是个不断踩坑的过程。
界面控件的名称和属性设置
控件类型 | 控件名称Name | 属性值TabIndex | 备注 |
---|---|---|---|
textbox | textbox1 | 数字打印 | |
button | button1 | 1 | 数字1 |
button | button2 | 2 | 数字2 |
button | button3 | 3 | 数字3 |
button | button4 | 4 | 数字4 |
button | button5 | 5 | 数字5 |
button | button6 | 6 | 数字6 |
button | button7 | 7 | 数字7 |
button | button8 | 8 | 数字8 |
button | button9 | 9 | 数字9 |
button | button12 | 0 | 数字0 |
button | button_add | + | 加 |
button | button_subtract | - | 减 |
button | button_multiply | * | 乘 |
button | button_divide | / | 除 |
button | button_log | log | 对数 |
button | button_ln | ln | 常用对数 |
button | button_power | x^2 | 开方 |
button | button_sqrt | sqrt | 开根号 |
button | button11 | C | 清除 |
button | button_equal | = | 等于 |
拖动控件摆出计算器样式,按上面的表格设置控件名称和属性值,再直接复制下面的代码到From.cs
文件,你的计算器就完成了。当然,之后你也可以修改一些文字大小、颜色背景之类的样式,或者添加控件让它变得更美观。
- 以防有零基础的或者有还没学会的,在设计视图里
双击
控件,就可以进入控件操作编写代码
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 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 button2_Click(object sender, EventArgs e) { if (c == true) //有值的话置为空 { textBox1.Text = ""; c = false; } textBox1.Text += "2"; } private void button3_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "3"; } private void button6_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "4"; } private void button5_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "5"; } private void button4_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "6"; } private void button9_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 button7_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "9"; } private void button12_Click(object sender, EventArgs e) { if (c == true) { textBox1.Text = ""; c = false; } textBox1.Text += "0"; } private void button_add_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 button_subtract_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 button_multiply_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 button15_Click(object sender, EventArgs e) { } private void button_divide_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 button11_Click(object sender, EventArgs e) { textBox1.Text = ""; } private void button14_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { c = true; b = double.Parse(textBox1.Text); d = "x^2"; } } private void button_sqrt_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { c = true; b = double.Parse(textBox1.Text); d = "sqrt"; } } private void button_log_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { c = true; b = double.Parse(textBox1.Text); d = "log"; } } private void button_ln_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) { 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 "x^2": a = b * b; break; case "sqrt": a = Math.Sqrt(b); break; case "log": a = Math.Log10(b); break; case "ln": a = Math.Log(b); break; } textBox1.Text = a + ""; c = true; } }}
这个代码的意思大致是,b保存第一个数字,textBox1.Text录入新输入的数字,d保存运算操作,a输出运算结果,c判断输入是否正确。
大部分按钮动作都是判断点击事件,给相应的位置赋值以便最后输出运算结果。这篇可能是我写的最阿巴阿巴的博客了,希望大家都能学好吧!
转载地址:https://blog.csdn.net/ZYJ_OvO/article/details/115129615 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月13日 18时39分14秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
读博读废了是种什么样的体验?
2019-04-29
2020年,技术圈十大“翻车”事件!
2019-04-29
C语言状态机编程思想
2019-04-29
为什么很多电器设备都要使用单片机?
2019-04-29
在中国做操作系统研发 20 年是种什么体验?
2019-04-29
PCB走线角度为90度到底行不行?
2019-04-29
这个 17 岁的黑客天才,破解了第一代 iPhone!
2019-04-29
在STM32价格疯长下,哪些国产32可以替代?
2019-04-29
半导体芯片原厂涨价及调价声明新增了这些!
2019-04-29
为什么你学C++这么难?
2019-04-29
无人机破巡检难题,秒变电网卫士
2019-04-29
五年,我成为了一名嵌入式工程师。
2019-04-29
2020年电赛题目,命题专家们怎么看?
2019-04-29
PCB元器件摆放不可忽略的10个技巧
2019-04-29
掌握AI核心技术没有秘籍,能自己创造就是王道
2019-04-29
大学老师的月薪多少?实话实说:4万多一点……
2019-04-29
2020年电赛题目,命题专家权威解析!
2019-04-29
如何掌握“所有”的程序语言?没错,就是所有!
2019-04-29
39岁单身程序员入住养老院
2019-04-29
写论文,这个神器不能少!
2019-04-29