Thursday, January 10, 2013

Calculator code 1

This is my original version. The names differ a little from your version, It is not commented. I will add comments to it sometime in the very near future


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></title>
    <link href="CalculatorStyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Simple Calculator</h1>
    <table>
    <tr>
    <td colspan="3">
        <asp:TextBox ID="TextBox1" runat="server" CssClass="entryText" ></asp:TextBox></td>
        <td><asp:Button ID="btnMinus" runat="server" Text="-" CssClass="btnWidth" 
            onclick="btnMinus_Click"/></td>
    </tr>
    <tr>
    <td>
        <asp:Button ID="Button1" runat="server" Text="7" OnClick="number_click"  CssClass="btnWidth" /></td>
    <td><asp:Button ID="Button2" runat="server" Text="8" OnClick="number_click" CssClass="btnWidth" /></td>
    <td><asp:Button ID="Button3" runat="server" Text="9" OnClick="number_click" CssClass="btnWidth" /></td>
    <td><asp:Button ID="btnPlus" runat="server" Text="+" onclick="btnPlus_Click" CssClass="btnWidth" /></td>
    </tr>
      <tr>
    <td>
        <asp:Button ID="Button4" runat="server" Text="4" OnClick="number_click" CssClass="btnWidth" /></td>
    <td><asp:Button ID="Button5" runat="server" Text="5" OnClick="number_click" CssClass="btnWidth" /></td>
    <td><asp:Button ID="Button6" runat="server" Text="6" OnClick="number_click" CssClass="btnWidth" /></td>
     <td><asp:Button ID="btnMultiply" runat="server" Text="*" CssClass="btnWidth" 
            onclick="btnMultiply_Click"  /></td>
    </tr>
      <tr>
      <td>
        <asp:Button ID="Button8" runat="server" Text="1" OnClick="number_click" CssClass="btnWidth" /></td>
    <td><asp:Button ID="Button9" runat="server" Text="2" OnClick="number_click" CssClass="btnWidth" /></td>
    <td><asp:Button ID="Button10" runat="server" Text="3" OnClick="number_click" CssClass="btnWidth" /></td>
    <td>
        <asp:Button ID="btnDivide" runat="server" Text="/"  CssClass="btnWidth" 
            onclick="btnDivide_Click"/></td>
    </tr>
      <tr>
         <td>
           <asp:Button ID="Button11" runat="server" Text="0" OnClick="number_click" CssClass="btnWidth" /> </td>
    <td>
        <asp:Button ID="Button7" runat="server" Text="." OnClick="number_click"  CssClass="btnWidth" />
        </td>
    <td >
        <asp:Button ID="btnCalcu" runat="server" Text="=" onclick="btnCalcu_Click" CssClass="btnWidth" /></td>
    
    <td>
        <asp:Button ID="btnClear" runat="server" Text="CLR" CssClass="btnWidth" 
            onclick="btnClear_Click" /></td>
    
    </tr>
    
    </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
{
    double answer=0;
    double number1=0;
    double number2=0;
    int sessionCheck = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //    Session["Answer"] = 0;

    }
    protected void number_click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        TextBox1.Text += b.Text;
    }

  
    protected void btnPlus_Click(object sender, EventArgs e)
    {
        GetNumbers();

        Session["Answer"] = number1 + number2;
        Session["Operator"] = "+";
    }

    protected void btnCalcu_Click(object sender, EventArgs e)
    {
        number1 = (double)Session["Answer"];
        number2 = double.Parse(TextBox1.Text);
        string op = Session["Operator"].ToString();

        switch (op)
        {
            case "+":
            answer = number1 + number2;
            break;
            case "-":
             answer = number1 - number2;
            break;
            case "*":
             answer = number1 * number2;
            break;
            case "/":
             answer = number1 / number2;
            break;

        }
       
        TextBox1.Text = answer.ToString();
        Session["Answer"] = null;
    }

    protected void GetNumbers()
    {
        if (Session["Answer"] == null)
        {
            sessionCheck = 0; ;
        }
        else
        {
            number1 = (double)Session["Answer"];
        }
        bool isNumber = double.TryParse(TextBox1.Text, out number2);
        if (!isNumber)
        {
            TextBox1.Text = "";
            return;
        }
        TextBox1.Text = "";
    }

    protected void btnMinus_Click(object sender, EventArgs e)
    {
        GetNumbers();
        if (sessionCheck == 0)
        {
            Session["Answer"] = number2;
        }
        else
        {
            Session["Answer"] = number1 - number2;
        }

        
        Session["Operator"] = "-";
    }
    protected void btnMultiply_Click(object sender, EventArgs e)
    {
        GetNumbers();
        if (sessionCheck == 0)
        {
            Session["Answer"]=number2;
        }
         else
        {
             Session["Answer"] = number1 * number2;
        }
       
        Session["Operator"] = "*";
    }
    protected void btnDivide_Click(object sender, EventArgs e)
    {
        GetNumbers();

        if (sessionCheck == 0)
        {
            Session["Answer"] = number2;
        }
        else
        {
            Session["Answer"] = number1 / number2;
        }
        Session["Operator"] = "/";
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        Session["Answer"] = null;
        Session["Operator"] = null;
    }
}

And here is the stylesheet CalculatorStyle.css, such as it is

body {
}

.entryText
{
 text-align:right;
 width:120px;
}

table
{
 background-color:Teal;
}

.btnWidth
{
 width:40px;
}


No comments:

Post a Comment