用c#实现简易的计算器功能

由于今天在网上搜了一下c#写的计算器,发现大多都太繁琐了,很多没必要并且不容易理解的东西就专门写了这个博客

1.首先新建一个windows窗体应用的项目。执行文件-新建-项目-windows窗体应用

2.在工具箱中拖出一个textbox用于输入和显示,再拖出21个button按钮用来当计算器的按键,在textbox下面还有一个lable控件(我把它属性改成了空格所以看不到了),改一下按钮的text属性


3.双击数字按钮进入代码界面(数字只用一个事件即可,运算符也是用一个事件,其他每个按钮都需要双击添加事件)

4.代码呢已经准备好了,只要双击按钮进入代码界面,然后对应着粘上就行了(注意所有数字都是用的一个事件,都有标注,可以选择按钮,然后单击属性里的事件(闪电图标)查看click的事件)

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 计算器
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

//定义变量

  char oper;
    double num1;
    double num2;
    double result = 0;
    double memory=0.0;
    private void Button9_Click(object sender, EventArgs e)//数字按钮的功能实现
    {
        Button a = (Button)sender;//判断按下的是哪个按钮
        if (textBox1.Text == "0")
        {
            textBox1.Text = a.Text;
        }
        else
            textBox1.Text += a.Text;
    }

private void Button16_Click(object sender, EventArgs e)//运算符按钮的功能实现
{
    if (textBox1.Text != "")
    {
        num1 = double.Parse(textBox1.Text);
        oper = char.Parse(((Button)sender).Text);
        textBox1.Text = "";
    }
}

private void Button15_Click(object sender, EventArgs e)//C按钮的功能实现
{
    textBox1.Text = "";
    textBox1.Focus();
    num1 = 0;
    num2 = 0;
    oper = ' ';
}

private void Button14_Click(object sender, EventArgs e)//结果按钮的功能实现
{
    if (textBox1.Text != "")
    {
        num2 = double.Parse(textBox1.Text);
        switch (oper)
        {
            case '+': result = num1 + num2; break;
            case '-': result = num1 - num2; break;
            case '*': result = num1 * num2; break;
            case '÷': if(num2=0)
            {
            MessageBox.Show("0不能为除数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;   
        }
        result = num1 / num2; break;
        textBox1.Text = result.ToString();
    }
}

private void Button17_Click(object sender, EventArgs e)//小数点按钮的功能实现
{
    if (textBox1.Text != "")
    {
        textBox1.Text += ".";
    }
    else
    {
        textBox1.Text = "0.";
    }
}

private void Button18_Click(object sender, EventArgs e)//M+按钮的功能实现
{
    if(textBox1.Text!="")
    {
        label1.Text = "M";
        memory += double.Parse(textBox1.Text);
        textBox1.Text = " ";
    }
}

private void Button20_Click(object sender, EventArgs e)//MR按钮的功能实现
{
    textBox1.Text = memory.ToString();
}

private void Button21_Click(object sender, EventArgs e)//MC按钮的功能实现
{
    label1.Text = "";
    memory = 0;
}

private void Button19_Click(object sender, EventArgs e)//M-按钮的功能实现
{
    if (textBox1.Text != "")
    {
        label1.Text = "M";
        memory -= double.Parse(textBox1.Text);
        textBox1.Text = " ";
    }
}
}

}

链接:https://pan.baidu.com/s/13xivry-A3TAusficWG16yQ
提取码:fpp3

  • 40
    点赞
  • 357
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论
栅格计算器是一种常见的计算机应用程序,它允许用户在一个矩形的网格中输入数值,然后对这些数值进行各种计算。在本文中,我们将使用C#和AE(Adobe After Effects)来实现一个简单的栅格计算器功能。 首先,在AE中创建一个新的合成(Composition),并添加一个文本层(Text Layer)。将文本层的内容设置为“栅格计算器”,并将其放置在合成的顶部。 接下来,我们需要创建一个矩形网格,以便用户可以输入数值。我们可以使用AE中的形状层(Shape Layer)来创建一个矩形。在AE中,选择“Layer”->“New”->“Shape Layer”来创建一个新的形状层。选择“Rectangle Tool”(矩形工具),并在画布上绘制一个矩形。然后,选择矩形图层,打开“Add”->“Fill”属性,选择一种颜色填充矩形。 现在,我们需要将矩形网格分成若干个小格子,以便用户可以在其中输入数值。我们可以使用C#编写一个简单的脚本来完成这个任务。在Visual Studio中创建一个新的C#控制台应用程序,并将其命名为“GridCalculator”。 在GridCalculator项目中,我们需要添加一个引用,以便我们可以访问AE中的COM(Component Object Model)对象。选择“Project”->“Add Reference”,然后在“COM”选项卡中找到“Adobe After Effects XX.X Type Library”(其中XX.X表示你的AE版本号),并添加它。 接下来,我们需要编写一个C#脚本来创建网格。以下是一个示例脚本: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AE_COM; namespace GridCalculator { class Program { static void Main(string[] args) { // Create a new AE project var ae = new Application(); var proj = ae.NewProject(); // Create a new composition var comp = proj.Items.AddComp("Grid Calculator", 1920, 1080, 1.0, 10.0, 30); // Add a new shape layer var shapeLayer = comp.Layers.AddShape(); shapeLayer.Name = "Grid"; // Create a new rectangle shape var rect = new Shape(); var rectGroup = new ShapeGroup(); rectGroup.AddShape(rect); shapeLayer.Property("Contents").AddProperty("ADBE Vector Group").SetValue(rectGroup); // Set the rectangle size rect.Size = new PointF(400, 400); // Create the grid var rows = 10; var cols = 10; var cellSize = new PointF(rect.Size.X / cols, rect.Size.Y / rows); for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { // Create a new rectangle shape for each cell var cell = new Shape(); var cellGroup = new ShapeGroup(); cellGroup.AddShape(cell); shapeLayer.Property("Contents").AddProperty("ADBE Vector Group").SetValue(cellGroup); // Set the cell position and size var pos = new PointF(col * cellSize.X, row * cellSize.Y); cell.Position = pos; cell.Size = cellSize; } } // Save the project proj.Save(@"C:\GridCalculator.aep"); // Quit AE ae.Quit(); } } } ``` 在上面的脚本中,我们首先创建了一个新的AE项目和一个新的合成。然后,我们添加了一个新的形状层,并在其中创建了一个矩形。接下来,我们使用嵌套循环创建了一个网格,其中每个单元格都是一个独立的矩形。 最后,我们保存了AE项目,并退出AE应用程序。 现在,我们可以在AE中打开“GridCalculator.aep”文件,并将生成的网格图层拖放到我们之前创建的合成中。用户现在可以在每个单元格中输入数值,并使用AE中的表达式(Expressions)功能对这些数值进行计算。 总的来说,使用C#和AE来创建栅格计算器功能是非常简单的。通过利用AE的COM对象和C#的编程能力,我们可以轻松地创建一个自定义的栅格计算器工具,以满足我们的具体需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臨鱼羡上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值