c#计算后在网页上输出结果

相关代码,设计一个程序,计算后在网页上输出结果

计算器?这种直接前端js就能实现,不需要经过服务器,参考:https://c.runoob.com/codedemo/3654/
一定要C#,webform简单示例如下

img

x.aspx

 <%@ Page Language="C#" AutoEventWireup="true"%>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) {
            if (a.Text != "" && b.Text != "" && opr.Text != "")
            {
                double da = 0, db = 0, dc = 0;
                double.TryParse(a.Text, out da);
                double.TryParse(b.Text, out db);
                switch (opr.Text)
                {
                    case "+": dc = da + db; break;
                    case "-": dc = da - db; break;
                    case "*": dc = da * db; break;
                    case "/": dc = da / db; break;
                }
                c.Text = Math.Round(dc, 2).ToString();
            }
            else c.Text = "";
        }
    }
</script>
<!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>Asp.Net计算器</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:TextBox runat="server" ID="a"></asp:TextBox>
        <asp:DropDownList ID="opr" runat="server" AutoPostBack="true">
            <asp:ListItem Value="">请选择操作</asp:ListItem>
            <asp:ListItem Value="+">+</asp:ListItem>
            <asp:ListItem Value="-">-</asp:ListItem>
            <asp:ListItem Value="*">*</asp:ListItem>
            <asp:ListItem Value="/">/</asp:ListItem>
        </asp:DropDownList>
       <asp:TextBox runat="server" ID="b"></asp:TextBox>
        =
       <asp:TextBox runat="server" ID="c"></asp:TextBox>
    </form>
</body>
</html>
 


img


有其他问题可以继续交流~

利用asp.net
https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/introduction/getting-started

img

前台代码如下:

<div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>选择</asp:ListItem>
            <asp:ListItem>+</asp:ListItem>
            <asp:ListItem>-</asp:ListItem>
            <asp:ListItem>*</asp:ListItem>
            <asp:ListItem>/</asp:ListItem>
            <asp:ListItem>%</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>=<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />     
    </div>

后台代码

protected void Button1_Click(object sender, EventArgs e)
    {
        double a = Convert.ToDouble(TextBox1.Text);
        double b = Convert.ToDouble(TextBox2.Text);
        string c = DropDownList1.Text;
        double d = 0;
        d = Class1.Switch(a, b, c, d);
        TextBox3.Text = Convert.ToString(d);      
    }

调用Class1类中的Switch 方法:

public class Class1
{
    //四则运算
    public static double Switch(double a, double b, string c, double d)
    {
        switch (c)
{
            case "+":
                d = a + b;
                break;
            case "-":
                d = a - b;
                break;
            case "*":
                d = a * b;
                break;
            case "/":
                d = a / b;
                break;
            case "%":
                d = a % b;
                break;
            default:
                break;
        }
        return d;
    }
}