c# asp.net 简单数字计算器(4)

想要自己写一个计算器应用程序,费了大概一天的时间,结果还是在高人的指点下才完成(在这里谢谢一直辅导我的哥哥),原来代码不是那么好些的,自己以后要尝试多写一些,大胆,不要怕错,细心调试,慢慢更改,我相信一定行的!加油!!!自己是最棒的!!!!
为什么说是简单数字计算器,应为这个计算器还不能够辨别数字和汉字,只能输入纯数字以进行加减乘除简单运算,随着学习的更进一步深入,会更加完善!
html代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="计算器._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
        <title>简单计算器</title>
</head>
<body>
        <form id="form1" runat="server">
        <div>
        <asp:TextBox ID="num1" runat="server" ></asp:TextBox>
        <asp:DropDownList ID="dropdownlist" runat="server">
                <asp:ListItem Value="加号" Selected="True">+</asp:ListItem>
                <asp:ListItem Value="减号">-</asp:ListItem>
                <asp:ListItem Value="乘">*</asp:ListItem>
                <asp:ListItem Value="除">/</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="num2" runat="server"></asp:TextBox><br/>
                <asp:Label ID="Label1" runat="server" BackColor="#339966" Text="结果是:"
                Width="200px"></asp:Label>
                <asp:Button ID="Button1" runat="server" Text="计算" Width="88px"
                        onclick="Button1_Click" />
        </div>
        </form>
</body>
</html>

后台c#代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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

                }

protected void Button1_Click(object sender, EventArgs e)
                {
int nu1 = int.Parse(this.num1.Text.ToString());//对nu1从string转为int型。
int nu2 = int.Parse(this.num2.Text.ToString());
int sum = 0;//对sum进行初始化
string n = this.dropdownlist.SelectedItem.Text.ToString();//选择下拉单选框中选择在aspx源中的+,-,*,/。

switch (n)
                        {
case ("+"):
                                        sum = nu1 + nu2;
break;
case "-":
                                     sum = nu1 - nu2;
break;
case "*":
                                     sum = nu1 * nu2;
break;
case "/":                                    
                                        sum = nu1 / nu2;
break;
default:
                                        Console.WriteLine("请正确输入数字");
break;
                        }
this.Label1.Text = sum.ToString();//在空间label中显示sum的值。

                }
        }
}

原文地址:https://www.cnblogs.com/shenzhoulong/p/1714780.html