Thursday, January 12, 2012

Calculator with Addition

Here is the calculator. I want to mention that when you do multiplication and division you will have to use a somewhat different pattern. There is only a zero in the session variable when you click the operator. That results in the final answer also equaling zero or infinity. Try to figure out how to do these, if you can and we will look at it again on Tuesday.

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="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>Calculator</title>
    <link href="calculator.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tbody>
    <tr>
    <td colspan="4">
        <asp:TextBox ID="txtDisplay" runat="server"></asp:TextBox></td>
    
    </tr>
    <tr>
    <td>
        <asp:Button ID="Button1" runat="server" Text="7" 
CssClass="buttonstyle" OnClick="InputNumber_Click" /></td>
    <td>
        <asp:Button ID="Button2" runat="server" Text="8" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="Button3" runat="server" Text="9" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="btnAdd" runat="server" Text="+" 
CssClass="buttonstyle" OnClick="Operator_Click"/></td>
    </tr>
    <tr>
    <td>
        <asp:Button ID="Button5" runat="server" Text="4" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="Button6" runat="server" Text="5" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="Button7" runat="server" Text="6" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="btnSubtract" runat="server" Text="-" 
CssClass="buttonstyle" OnClick="Operator_Click" /></td>
    </tr>
    <tr>
    <td>
        <asp:Button ID="Button9" runat="server" Text="1" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="Button10" runat="server" Text="2" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="Button11" runat="server" Text="3" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="btnMult" runat="server" Text="*" 
CssClass="buttonstyle" OnClick="Operator_Click"/></td>
    </tr>
    <tr>
    <td>
        <asp:Button ID="btnDel" runat="server" Text="Del" 
CssClass="buttonstyle"/></td>
    <td>
        <asp:Button ID="Button14" runat="server" Text="0" 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="Button15" runat="server" Text="." 
CssClass="buttonstyle" OnClick="InputNumber_Click"/></td>
    <td>
        <asp:Button ID="btnDiv" runat="server" Text="/" 
CssClass="buttonstyle" OnClick="Operator_Click"/></td>
    </tr>
    <tr>
   
    <td colspan="4">
        <asp:Button ID="btnEqual" runat="server" Text="=" 
CssClass="buttonstyle" OnClick="Operator_Click"/></td>
    </tr>
    </tbody>
    </table>
    </div>
    </form>
</body>
</html>



Default.aspx.cs
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)
    {
        if (!IsPostBack)
        {
            Session["result"] = 0.0;
        }

    }
    protected void InputNumber_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        txtDisplay.Text += b.Text;
    }
    protected void Operator_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;

        if (b.ID.Equals("btnAdd"))
        {
            Addition();
            Session["operator"] = "+";
        }
        if(b.ID.Equals("btnEqual"))
        {
            Calculate();
        }
    }

    protected void Addition()
    {
       
        try
        {
            double num = GetNumber();
            double numResult = (double)Session["result"];
            Session["result"] = num + numResult;
            txtDisplay.Text = "";
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

    protected double GetNumber()
    {
       
        double number =0;
        bool isDouble = double.TryParse(txtDisplay.Text, out number);
        if (isDouble == false)
        {
            txtDisplay.Text = "";
            Exception ex = new Exception("Only enter valid numbers");
            throw ex;
        }
        return number;
    }

    protected void Calculate()
    {
        double number = (double)Session["result"];
        double number2 = GetNumber();
        if (Session["operator"].ToString().Equals("+"))
        {
            txtDisplay.Text = (number + number2).ToString();
        }
    }
}

Calculator.css
body {
}

.buttonstyle
{
    width:50px;
    
}

#txtDisplay
{
    text-align:right;
}

No comments:

Post a Comment