一个枯燥无味的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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Python基础知识作业1
下一篇:2017年蓝桥杯C组真题及解析

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月13日 18时39分14秒