web中用c#设计一个简易计算器

设计并实现一个简易的计算器—使用页面上提供的基本数码(0-9)按键在两个文本框TextBox1、TextBox2中输入两个数然后按下算符按键(加法运算符按键btnADD、减法运算符按键btnSubtractor、乘法算符按键btnMultiplying、除法算符按键btnDivision),能做相应的的某个单种类运算,在显示结果的只读文本框TextBox3上能显示相应的运算的结果。

具体的操作步骤如下:
(1)、选择“网站|添加新项”命令,添加一个Web窗体页。
(2)、设计如下所示的界面,两个文本框TextBox1、TextBox2中输入两个数,以及Button按钮。具体的设计界面如下。
这里写图片描述
(3)、纯白文本框的enabled的属性在程序运行中被设置为true,灰色文本框的enabled的属性在程序运行中被设置为false;0~9按键的Click事件处理方法(也叫函数)可用同一方法button_Click其第一形参sender将携带被单击的数字按键的对象信息,只要注意到被单击的数字按键对象是由button类产生的,而button_Click其第一形参sender的数据类型是object, 在方法体内对形参sender做强制类型转换,使得强制转换为button类的对象,之后就可以获得button界面上的数码信息。(但在此例中我的办法比较蠢,我将10个Button全都进行插入编写了代码)
具体的代码为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    double op1, op2, result = 0;//定义三个操作数
    char op;//运算符
    protected void Button_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        if (TextBox1.Enabled)
            TextBox1.Text += btn.Text;
        else if (TextBox2.Enabled)
            TextBox2.Text += btn.Text;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        if (TextBox1.Enabled)
            TextBox1.Text += btn.Text;
        else if (TextBox2.Enabled)
            TextBox2.Text += btn.Text;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Label1.Text = "+";
        TextBox1.Enabled = false;
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        Label1.Text = "-";
        TextBox1.Enabled = false;
    }

    protected void Button12_Click(object sender, EventArgs e)
    {
        Label1.Text = "*";
        TextBox1.Enabled = false;
    }

    protected void Button16_Click(object sender, EventArgs e)
    {
        Label1.Text = "/";
        TextBox1.Enabled = false;
    }

    protected void Button15_Click(object sender, EventArgs e)
    {
        Label2.Text = "=";
        TextBox2.Enabled = false;
        op1 = double.Parse(TextBox1.Text);
        op2 = double.Parse(TextBox2.Text);
        op = Convert.ToChar(Label1.Text);
        switch(op)//操作符的几个形式
        {
            case '+':
                result = op1 + op2;
                TextBox3.Text = result.ToString();
                break;
            case '-':
                result = op1 - op2;
                TextBox3.Text = result.ToString();
                break;
            case '*':
                result = op1 * op2;
                TextBox3.Text = result.ToString();
                break;
            case '/':
                result = op1 / op2;
                TextBox3.Text = result.ToString();
                break;
            default:
                break;
        }
        }
    protected void Button17_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox1.Enabled = true;
    }
   protected void Button18_Click(object sender, EventArgs e)
    {
        TextBox2.Text = "";
        TextBox2.Enabled = true;
    }

   protected void Button19_Click(object sender, EventArgs e)
   {
       Label1.Text = "";
       Label2.Text = "";
       TextBox1.Enabled = true;
       TextBox2.Enabled = true;
       TextBox1.Text = "";
       TextBox2.Text = "";
       TextBox3.Text = "";
   }
   protected void TextBox1_TextChanged(object sender, EventArgs e)
   {

   }

   protected void TextBox2_TextChanged(object sender, EventArgs e)
   {

   }
   protected void Button3_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }

   protected void Button5_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }

   protected void Button6_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }

   protected void Button7_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }

   protected void Button9_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }

   protected void Button10_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }

   protected void Button11_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }

   protected void Button13_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       if (TextBox1.Enabled)
           TextBox1.Text += btn.Text;
       else if (TextBox2.Enabled)
           TextBox2.Text += btn.Text;
   }
}

设计部分的源代码为:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .auto-style1 {
            font-weight: bold;
        }
        .auto-style2 {
            background-color: #6666FF;
        }
        .auto-style3 {
            font-weight: bold;
            background-color: #6600FF;
        }
    </style>
</head>
<body style="color: #FFCCFF; margin-bottom: 19px">
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server" Width="87px" OnTextChanged="TextBox1_TextChanged" CssClass="auto-style1"></asp:TextBox>
        <b>
        <asp:Label ID="Label1" runat="server" Text="Label" CssClass="auto-style2"></asp:Label>
&nbsp;

        </b>

        <asp:TextBox ID="TextBox2" runat="server" Width="109px" OnTextChanged="TextBox2_TextChanged" CssClass="auto-style1"></asp:TextBox>
        <asp:Label ID="Label2" runat="server" Text="Label" CssClass="auto-style3"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server" Width="126px" style="margin-left: 5px" CssClass="auto-style1"></asp:TextBox>
        <b>
        <br />
        </b>
        <asp:Button ID="Button1" runat="server" Text="1" OnClick="Button_Click" CssClass="auto-style1" />
        <b>&nbsp;&nbsp;&nbsp; </b> <asp:Button ID="Button2" runat="server" Text="2" OnClick="Button2_Click" CssClass="auto-style1" />
        <b>&nbsp;&nbsp; </b> <asp:Button ID="Button3" runat="server" Text="3" CssClass="auto-style1" OnClick="Button3_Click" />
        <b>&nbsp;&nbsp; </b> <asp:Button ID="Button4" runat="server" Text="+" OnClick="Button4_Click" CssClass="auto-style1" />
        <b>
        <br />
        <br />
        </b>
        <asp:Button ID="Button5" runat="server" Text="4" CssClass="auto-style1" OnClick="Button5_Click" style="width: 23px" />
        <b>&nbsp;&nbsp;&nbsp; </b> <asp:Button ID="Button6" runat="server" Text="5" CssClass="auto-style1" OnClick="Button6_Click" />
        <b>&nbsp;&nbsp; </b> <asp:Button ID="Button7" runat="server" Text="6" CssClass="auto-style1" OnClick="Button7_Click" />
        <b>&nbsp;&nbsp; </b> <asp:Button ID="Button8" runat="server" Text="-" OnClick="Button8_Click" CssClass="auto-style1" />
        <b>
        <br />
        <br />
        </b>
        <asp:Button ID="Button9" runat="server" Text="7" CssClass="auto-style1" OnClick="Button9_Click" />
        <b>&nbsp;&nbsp;&nbsp; </b> <asp:Button ID="Button10" runat="server" Text="8" CssClass="auto-style1" OnClick="Button10_Click" style="height: 27px" />
        <b>&nbsp;&nbsp; </b> <asp:Button ID="Button11" runat="server" Text="9" CssClass="auto-style1" OnClick="Button11_Click" />
        <b>&nbsp;&nbsp; </b> <asp:Button ID="Button12" runat="server" Text="*" OnClick="Button12_Click" CssClass="auto-style1" />
        <b>
        <br />
        <br />
        </b>
        <asp:Button ID="Button13" runat="server" Text="0" CssClass="auto-style1" OnClick="Button13_Click" />
        <b>&nbsp;&nbsp;&nbsp; </b> <asp:Button ID="Button14" runat="server" Text="." CssClass="auto-style1" />
        <b>&nbsp;&nbsp; </b> <asp:Button ID="Button15" runat="server" Text="=" OnClick="Button15_Click" CssClass="auto-style1" />

        <b>&nbsp;&nbsp;
        </b>
        <asp:Button ID="Button16" runat="server" Text="/" OnClick="Button16_Click" CssClass="auto-style1" />


        <b>


        <br />
        <br />
        </b>
        <asp:Button ID="Button17" runat="server" Text="cls op1" OnClick="Button17_Click" CssClass="auto-style1" />
        <b>&nbsp;&nbsp;
        </b>
        <asp:Button ID="Button18" runat="server" Text="cls op2" OnClick="Button18_Click" CssClass="auto-style1" />
        <b>&nbsp;&nbsp;
        </b>
        <asp:Button ID="Button19" runat="server" Text="clear All" OnClick="Button19_Click" CssClass="auto-style1" />


        <b>


        <br />

        </b>

    </div>
    </form>
</body>
</html>

(4)、具体的运行界面为:(有点丑)
这里写图片描述

本人是个Web初学者,希望大家给予多多发表自己的意见。

  • 1
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
好的,我可以帮你写一个简易的发红包程序。我们可以使用Python的`random`模块来生成随机数,以及使用`math`模块来进行数学计算。以下是代码实现: ```python import random import math def send_red_envelope(total_money, num_people): # 判断红包是否合法 if total_money < num_people * 0.01: print("红包总金额不能少于每个人最少能抢到的金额!") return # 初始化红包列表 red_envelope = [] for i in range(num_people): red_envelope.append(0) # 发红包 for i in range(num_people): if i == num_people - 1: red_envelope[i] = round(total_money, 2) else: money = random.uniform(0.01, total_money - (num_people - i - 1) * 0.01) red_envelope[i] = round(money, 2) total_money -= money # 打印红包结果 print("发红包成功,红包总金额为{}元,共分给{}个人:".format(round(sum(red_envelope), 2), num_people)) for i in range(num_people): print("第{}个人抢到了{}元".format(i + 1, red_envelope[i])) # 测试程序 send_red_envelope(10, 5) ``` 在这个程序中,我们定义了一个名为`send_red_envelope`的函数,它接受两个参数:`total_money`表示红包的总金额,`num_people`表示要分给多少个人。程序会先判断红包是否合法(即红包总金额不能少于每个人最少能抢到的金额),然后初始化一个红包列表,将每个人的红包金额初始化为0。接下来,程序会使用`random.uniform()`函数来生成一个介于0.01元和剩余金额之间的随机数,将这个数作为当前人的红包金额,并更新剩余的金额。最后,程序会打印出红包的结果。 你可以在调用`send_red_envelope`函数时传入合适的参数来测试程序的功能。注意,这只是一个简单的示例程序,实际的红包应用可能需要更复杂的逻辑和安全措施。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值