想要自己写一个计算器应用程序,费了大概一天的时间,结果还是在高人的指点下才完成(在这里谢谢一直辅导我的哥哥),原来代码不是那么好些的,自己以后要尝试多写一些,大胆,不要怕错,细心调试,慢慢更改,我相信一定行的!加油!!!自己是最棒的!!!!
为什么说是简单数字计算器,应为这个计算器还不能够辨别数字和汉字,只能输入纯数字以进行加减乘除简单运算,随着学习的更进一步深入,会更加完善!
html代码:
InBlock.gif<%@ Page Language= "C#" AutoEventWireup= "true" CodeBehind= "Default.aspx.cs" Inherits= "计算器._Default" %>
InBlock.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
InBlock.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
InBlock.gif<head runat="server">
InBlock.gif        <title>简单计算器</title>
InBlock.gif</head>
InBlock.gif<body>
InBlock.gif        <form id="form1" runat="server">
InBlock.gif        <div>
InBlock.gif        <asp:TextBox ID="num1" runat="server" ></asp:TextBox>
InBlock.gif        <asp:DropDownList ID="dropdownlist" runat="server">
InBlock.gif                <asp:ListItem Value="加号" Selected="True">+</asp:ListItem>
InBlock.gif                <asp:ListItem Value="减号">-</asp:ListItem>
InBlock.gif                <asp:ListItem Value="乘">*</asp:ListItem>
InBlock.gif                <asp:ListItem Value="除">/</asp:ListItem>
InBlock.gif        </asp:DropDownList>
InBlock.gif        <asp:TextBox ID="num2" runat="server"></asp:TextBox><br/>
InBlock.gif                <asp:Label ID="Label1" runat="server" BackColor="#339966" Text="结果是:"
InBlock.gif                Width="200px"></asp:Label>
InBlock.gif                <asp:Button ID="Button1" runat="server" Text="计算" Width="88px"
InBlock.gif                        οnclick="Button1_Click" />
InBlock.gif        </div>
InBlock.gif        </form>
InBlock.gif</body>
InBlock.gif</html>

后台c#代码:
InBlock.gif using System;
InBlock.gif using System.Collections;
InBlock.gif using System.Configuration;
InBlock.gif using System.Data;
InBlock.gif using System.Linq;
InBlock.gif using System.Web;
InBlock.gif using System.Web.Security;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI.WebControls.WebParts;
InBlock.gif using System.Xml.Linq;
InBlock.gif
namespace 计算器
InBlock.gif{
InBlock.gif         public partial class _Default : System.Web.UI.Page
InBlock.gif        {
InBlock.gif                 protected void Page_Load( object sender, EventArgs e)
InBlock.gif                {
InBlock.gif
                }
InBlock.gif
                 protected void Button1_Click( object sender, EventArgs e)
InBlock.gif                {
InBlock.gif                         int nu1 = int.Parse( this.num1.Text.ToString()); //对nu1从string转为int型。
InBlock.gif                         int nu2 = int.Parse( this.num2.Text.ToString());
InBlock.gif                         int sum = 0; //对sum进行初始化
InBlock.gif                         string n = this.dropdownlist.SelectedItem.Text.ToString(); //选择下拉单选框中选择在aspx源中的+,-,*,/。
InBlock.gif                                            
InBlock.gif                         switch (n)
InBlock.gif                        {
InBlock.gif                                 case ( "+"):
InBlock.gif                                        sum = nu1 + nu2;
InBlock.gif                                         break;
InBlock.gif                                 case "-":
InBlock.gif                                     sum = nu1 - nu2;
InBlock.gif                                         break;
InBlock.gif                                 case "*":
InBlock.gif                                     sum = nu1 * nu2;
InBlock.gif                                         break;
InBlock.gif                                 case "/":                                    
InBlock.gif                                        sum = nu1 / nu2;
InBlock.gif                                         break;
InBlock.gif                                 default:
InBlock.gif                                        Console.WriteLine( "请正确输入数字");
InBlock.gif                                         break;
InBlock.gif                        }
InBlock.gif                         this.Label1.Text = sum.ToString(); //在空间label中显示sum的值。
InBlock.gif                    
InBlock.gif                }
InBlock.gif        }
InBlock.gif}