Tuesday, April 9, 2013

Assignment 1 calculator code

Calculator code at GitHub

For assignments you can post on get hub, or zip the file and post it to Google Drive and share with spconger@gmail.com

Here is the code we did in class

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="CalcStyle.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td colspan="3">
                <asp:TextBox ID="txtDisplay" runat="server" CssClass="display"></asp:TextBox></td>
            <td>
                <asp:Button ID="btnClear" runat="server" Text="C" OnClick="btnClear_Click" /></td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btn7" runat="server" Text="7"  OnClick="number_click"/>
            </td>
            <td>
                <asp:Button ID="btn8" runat="server" Text="8" OnClick="number_click" />
            </td>
            <td>
                <asp:Button ID="btn9" runat="server" Text="9"  OnClick="number_click"/>
            </td>
            <td>
                <asp:Button ID="btnPlus" runat="server" Text="+" OnClick="btnPlus_Click" />
            </td>
        </tr>

        <tr>
            <td>
                <asp:Button ID="btn4" runat="server" Text="4"  OnClick="number_click"/>
            </td>
            <td>
                <asp:Button ID="btn5" runat="server" Text="5" OnClick="number_click" />
            </td>
            <td>
                <asp:Button ID="btn6" runat="server" Text="6"  OnClick="number_click"/>
            </td>
            <td>
                <asp:Button ID="btnMinus" runat="server" Text="-" OnClick="btnMinus_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btn1" runat="server" Text="1"  OnClick="number_click"/>
            </td>
            <td>
                <asp:Button ID="btn2" runat="server" Text="2" OnClick="number_click" />
            </td>
            <td>
                <asp:Button ID="btn3" runat="server" Text="3"  OnClick="number_click"/>
            </td>
            <td>
                <asp:Button ID="btnMultiply" runat="server" Text="*" OnClick="btnMultiply_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btn0" runat="server" Text="0"  OnClick="number_click"/>
            </td>
            <td>
                <asp:Button ID="btnDecimal" runat="server" Text="." OnClick="number_click" />
            </td>
            <td>
                <asp:Button ID="btnEqual" runat="server" Text="=" OnClick="btnEqual_Click" style="height: 26px"  />
            </td>
            <td>
                <asp:Button ID="btnDivide" runat="server" Text="/" OnClick="btnDivide_Click" />
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>


Default.aspx.

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
{
    /// 
    /// This is the code behind for the calculator
    /// one method handles the number clicks
    /// and there is a method for each of the operators
    /// Steve Conger  4/9/2013
    /// 
    double number1;
    double number2;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void number_click(object sender, EventArgs e)
    {
        //This gets the button that was clicked
        //and writes the code to the textbox
        Button button = (Button)sender;
        txtDisplay.Text += button.Text;
    }
    protected void btnPlus_Click(object sender, EventArgs e)
    {
        //bool goodNumber = ValidNumber();

        if (ValidNumber())
        {
            Session["operator"] = "+";
            Session["answer"] = number1;
            txtDisplay.Text = "";
        }
    }
    protected void btnEqual_Click(object sender, EventArgs e)
    {
        if (Session["operator"] != null)
        {
            if (Session["answer"] != null)
            {
                string op = Session["operator"].ToString();
                number1=(double)Session["answer"];
                number2=double.Parse(txtDisplay.Text);
                Operations operations = new Operations();
                switch (op)
                {
                    case "+":
                        txtDisplay.Text = operations.Add(number1, number2).ToString();
                        break;
                    case "-":
                        txtDisplay.Text = operations.Subtract(number1, number2).ToString();
                        break;
                    case "*":
                        txtDisplay.Text = operations.Multiply(number1, number2).ToString();
                        break;
                    case "/":
                        txtDisplay.Text = operations.Divide(number1, number2).ToString();
                        break;
                }
            }
            Session["answer"] = null;
            Session["operator"] = null;
        }
    }
    protected void btnMinus_Click(object sender, EventArgs e)
    {
        if (ValidNumber())
        {
            Session["operator"] = "-";
            Session["answer"] = number1;
            txtDisplay.Text = "";
        }

    }

    protected bool ValidNumber()
    {
        bool isValid=false;
        bool IsNumber = double.TryParse(txtDisplay.Text, out number1);
        if (!IsNumber)
        {
            txtDisplay.Text = "";

        }
        else
        {
            isValid = true;
        }
        return isValid;
    }

    protected void btnMultiply_Click(object sender, EventArgs e)
    {
        if (ValidNumber())
        {
            Session["operator"] = "*";
            Session["answer"] = number1;
            txtDisplay.Text = "";
        }
    }
    protected void btnDivide_Click(object sender, EventArgs e)
    {
        if (ValidNumber())
        {
            Session["operator"] = "/";
            Session["answer"] = number1;
            txtDisplay.Text = "";
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        Session["answer"] = null;
        Session["operator"] = null;
        txtDisplay.Text = "";
    }
}

Operations.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for Operations
/// 
public class Operations
{
 public Operations()
 {
  //
  // TODO: Add constructor logic here
  //
 }

    public double Add(double num1, double num2)
    {
        return num1 + num2;
    }

    public double Subtract(double num1, double num2)
    {
        return num1 - num2;
    }

    public double Multiply(double num1, double num2)
    {
        return num1 * num2;
    }

    public double Divide(double num1, double num2)
    {
        return num1 / num2;
    }
}

CalcStyle.css

body {
}

.display {
    text-align:right;
    background-color:aliceblue;
}

No comments:

Post a Comment