C#实现简易计算器功能(附源码)
作者:Just Do Its 发布时间:2021-07-18 00:16:00
标签:C#,计算器
本文实例为大家分享了C#实现简易计算器功能的具体代码,供大家参考,具体内容如下
剖析:
1、先设计界面(按钮、文本框(一个显示算式,一个显示结果))布局
2、单击按钮将其对应内容显示在文本框中
3、单击符号(+、-、×、÷、%)时将第一次输入的数储存起来
4、单击等号时将第二次输入的数存储起来并将第一次输入的数与第二次输入的数按照所单击的符号进行运算将结果显示在第一个文本框中
5、单击C时将两个文本框中的内容清空
重点:
1、声明一个bool类型的变量用于实现单击符号再次输入数字时第一次输入的数字清空显示第二次输入的数字
2、声明两个double类型的变量用于装第一次输入的数和装第二次输入的数
3、声明一个string类型的变量用于判断运算符号
界面布局:
具体代码如下:
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 Test_Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//声明三个变量
string type; //符号类型
double x;//装第一个数(按符号(+-×÷%)时textbox1中的数字)
double y;//装第二个数(按等号时textbox1中的数字)
bool c=false;
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen();//窗体居中显示
this.Text = "计算器";
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
textBox1.ReadOnly = true;//文本框只读
textBox2.TabIndex = 0;//光标焦点在textbox2中
}
private void button1_Click(object sender, EventArgs e)
{
if (c==true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "1";
textBox2.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "2";
textBox2.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "3";
textBox2.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "4";
textBox2.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "5";
textBox2.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "6";
textBox2.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "7";
textBox2.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "8";
textBox2.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "9";
textBox2.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
if (c == true)
{
c = false;
textBox1.Text = "";
}
textBox1.Text += "0";
textBox2.Text += "0";
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
textBox2.Text += ".";
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}
private void button13_Click(object sender, EventArgs e)
{
c = true;
type = "+";
textBox2.Text += "+";
x = double.Parse(textBox1.Text);
}
private void button14_Click(object sender, EventArgs e)
{
c = true;
type = "-";
textBox2.Text += "-";
x = double.Parse(textBox1.Text);
}
private void button15_Click(object sender, EventArgs e)
{
c = true;
type = "×";
textBox2.Text += "×";
x = double.Parse(textBox1.Text);
}
private void button16_Click(object sender, EventArgs e)
{
c = true;
type = "÷";
textBox2.Text += "÷";
x = double.Parse(textBox1.Text);
}
private void button18_Click(object sender, EventArgs e)
{
c = true;
type = "%";
textBox2.Text += "%";
x = double.Parse(textBox1.Text);
}
private void button17_Click(object sender, EventArgs e)
{
y = double.Parse(textBox1.Text);
//法一
while (type=="+")
{
textBox1.Text = (x + y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
while (type == "-")
{
textBox1.Text = (x - y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
while (type == "×")
{
textBox1.Text = (x * y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
while (type == "÷")
{
if (y!=0)
{
textBox1.Text = (x / y).ToString();
textBox2.Text += "=" + textBox1.Text;
}
else
{
MessageBox.Show("请重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
textBox1.Text = "";
textBox2.Text = "";
}
return;
}
while (type == "%")
{
textBox1.Text = (x % y).ToString();
textBox2.Text += "=" + textBox1.Text;
return;
}
//法二:
//if (type=="+")
//{
// textBox1.Text=(x + y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="-")
//{
// textBox1.Text = (x - y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="×")
//{
// textBox1.Text = (x * y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="÷")
//{
// textBox1.Text = (x / y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
//if (type=="%")
//{
// textBox1.Text = (x % y).ToString();
// textBox2.Text += "=" + textBox1.Text;
//}
}
}
}
效果图:
来源:https://blog.csdn.net/liu991029/article/details/105848935


猜你喜欢
- 引言对使用 lombok 还是有很多争议的,有些公司不建议使用,有些公司又大量使用。我们的想法是:可以使用,但是不要滥用。什么是 lombo
- 本文实例讲述了C# Console利用mspaint打开图像并保存的方法。分享给大家供大家参考,具体如下:调用画图板压缩图片System.D
- 前言若你的工程还没有进行基础配置,请查看我的博文Unity 之 ShaderGraph入门使用详解,按照步骤操作完成配置即可,还能顺便学习一
- Unsupported major.minor version 51.0解决办法今天偶然间同事遇到一个问题,也加深了自己对eclipse中b
- 概念异常处理的概念起源于早期的编程语言,如 LISP、PL/I 和 CLU。这些编程语言首次引入了异常处理机制,以便在程序执行过程中检测和处
- 引言: 最近公司在做一个教育培训学习及在线考试的项目,本人主要从事网络课程模块,主要做课程分类,课程,课件的创建及
- SpringBoot2.x过后static下的静态资源无法访问package com.example.thymeleaf.commons;i
- mybatis自动生成代码(实体类、Dao接口等)是很成熟的了,就是使用mybatis-generator插件。 它是一个开源的插件,使用m
- 1、加载节点SpringBoot启动时,会执行这个方法:SpringApplication#run,这个方法中会调prepareContex
- 关于链表链表是有序的列表链表是以节点的方式来存储每个节点包含data域,next域(指向下一个节点)分带头节点的链表和没有头节点的链表定义一
- 本文实例讲述了C#实现windows form拷贝内容到剪贴板的方法。分享给大家供大家参考。具体实现方法如下:using System;us
- 二进制、八进制和十六进制向十进制转换都是非常容易的,就是“按权相加”。所谓“权”,也即“位权”。例如,十进制第1位的位权为100=1,第2位
- 在Flutter开发过程中,我门有时候需要对一些数据进行本地的持久化存储,使用sp文件形式虽然也能解决问题,但是有时数据量较大的时候,显然我
- 一、引言在前面的文章中,我们是使用“锁”的方式实现了线程间的通信,这种通信方式比较笨重。除了锁之外,
- 结构:安装NuGet包:using SAP.Middleware.Connector;using System.Data;namespace
- 一、实现方式@ConfigurationProperties 注解(最好加上前缀prefix=“person”,标明是和配置文件中哪个开头的
- 使用RecyclerView越来越多了,基本可以不用listview了,但是这个新的控件谷歌官方似乎设计的没有想listview那样方便快捷
- 碎片的创建要使用碎片先要创建一个碎片,创建一个碎片很简单。1.新建一个碎片布局,fragment.xml<?xml version=&
- 目录1. 应用场景1.1. 保障线程安全1.2. 显示传递参数2. 实现原理3. 注意事项ThreadLocal是线程私有的局部变量存储容器
- 本文是针对AndBase框架学习整理的第二篇笔记,想要了解AndBase框架的朋友可以阅读本文,大家共同学习。使用AbActivity内部封