Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Program Homework 5 You are to write a calculator program which has all 0-9 bu

ID: 3914444 • Letter: C

Question

C# Program

Homework 5 You are to write a calculator program which has all 0-9 buttons. the dot button for decimal numbers, the four operands which are (+, -, *, /), a clear button and an enter button. The button placements should be similar to the small number pad on the right side of most keyboards. It should allow the user to keep entering numbers separated by operands and still work. Use the video tutorial from the previous week on Forms to complete this assignment. I will try to break your program, so make sure it is break-proof. conditions I will try: Divide by zero doing " 9 + - 7" should be evaluated as "9 - 7". " 9 + - * 7" should be "9 * 7". Basically it should do the last operand input. Try on your calculator on the PC to see how it gives you the answer. "9+3" "enter" gives me 12, then I type "+ 2" "enter" should give 14. If I did not press the first "enter", and instead directly did "9 + 3 +" Right at the moment I press the plus, it did the math for the 9 + 3 and displays 12 on the screen and is waiting for you to enter the 2 next to complete the same problem and get our 14. should allow decimal numbers... but you cannot place two decimals in a number "27.11.1" does not work because it has 2 decimals. The calculator should not allow me to enter the 2nd decimal.

http://www.youtube.com/watch?v=qfH6Y7p4J7U

Explanation / Answer

Solution:

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

    <style type="text/css">

    .cal

    {

        position:absolute;

        top:50px;

        left:150px;

        right:400px;

        height:500px;

        bottom:100px;

        background-color:Teal;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div class="cal">

        <asp:Label ID="l" Text=" BASIC CALCULATOR" runat="server"

            Font-Bold="False" Font-Italic="False"></asp:Label>

        <asp:TextBox ID="t" runat="server"

            Width="335px" Height="41px"></asp:TextBox>

        <asp:Button ID="b1" Text="1" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b2" Text="2" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b3" Text="3" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="add" Text="+" runat="server" Height="37px" Width="57px" />

        <asp:Button ID="b4" Text="4" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b5" Text="5" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b6" Text="6" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="sub" Text="-" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b7" Text="7" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b8" Text="8" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b9" Text="9" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="mul" Text="*" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b0" runat="server" Text="0" Height="37px"

            Width="57px" />

        <asp:Button ID="clr" runat="server" Text="CLR" Height="37px"

            Width="57px" />

        <asp:Button ID="eql" runat="server" Text="=" Height="37px"

            Width="57px" />

        <asp:Button ID="div" Text="/" runat="server" Height="37px"

            Width="57px" />

    </div>

    </form>

</body>

</html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

    <style type="text/css">

    .cal

    {

        position:absolute;

        top:50px;

        left:150px;

        right:400px;

        height:500px;

        bottom:100px;

        background-color:Teal;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div class="cal">

        <asp:Label ID="l" Text=" BASIC CALCULATOR" runat="server"

            Font-Bold="False" Font-Italic="False"></asp:Label>

        <asp:TextBox ID="t" runat="server"

            Width="335px" Height="41px"></asp:TextBox>

        <asp:Button ID="b1" Text="1" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b2" Text="2" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b3" Text="3" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="add" Text="+" runat="server" Height="37px" Width="57px" />

        <asp:Button ID="b4" Text="4" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b5" Text="5" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b6" Text="6" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="sub" Text="-" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b7" Text="7" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b8" Text="8" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b9" Text="9" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="mul" Text="*" runat="server" Height="37px"

            Width="57px" />

        <asp:Button ID="b0" runat="server" Text="0" Height="37px"

            Width="57px" />

        <asp:Button ID="clr" runat="server" Text="CLR" Height="37px"

            Width="57px" />

        <asp:Button ID="eql" runat="server" Text="=" Height="37px"

            Width="57px" />

        <asp:Button ID="div" Text="/" runat="server" Height="37px"

            Width="57px" />

    </div>

    </form>

</body>

</html>

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

{

    static float a, c, d;

    static char b;

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void b1_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b1.Text;

        }

        else

            t.Text = t.Text + b1.Text;

    }

    protected void b2_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b2.Text;

        }

        else

            t.Text = t.Text + b2.Text;

    }

    protected void b3_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b3.Text;

        }

        else

            t.Text = t.Text + b3.Text;

    }

    protected void b4_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b4.Text;

        }

        else

            t.Text = t.Text + b4.Text;

    }

    protected void b5_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b5.Text;

        }

        else

            t.Text = t.Text + b5.Text;

    }

    protected void b6_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b6.Text;

        }

        else

            t.Text = t.Text + b5.Text;

    }

    protected void b7_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b7.Text;

        }

        else

            t.Text = t.Text + b7.Text;

    }

    protected void b8_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b8.Text;

        }

        else

            t.Text = t.Text + b8.Text;

    }

    protected void b9_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b9.Text;

        }

        else

            t.Text = t.Text + b9.Text;

    }

    protected void b0_Click(object sender, EventArgs e)

    {

        if ((t.Text == "+") || (t.Text == "-") || (t.Text == "*") || (t.Text == "/"))

        {

            t.Text = "";

            t.Text = t.Text + b0.Text;

        }

        else

            t.Text = t.Text + b0.Text;

    }

    protected void add_Click(object sender, EventArgs e)

    {

        a = Convert.ToInt32(t.Text);

        t.Text = "";

        b = '+';

        t.Text += b;

    }

    protected void sub_Click(object sender, EventArgs e)

    {

        a = Convert.ToInt32(t.Text);

        t.Text = "";

        b = '-';

        t.Text += b;

    }

    protected void mul_Click(object sender, EventArgs e)

    {

        a = Convert.ToInt32(t.Text);

        t.Text = "";

        b = '*';

        t.Text += b;

    }

    protected void div_Click(object sender, EventArgs e)

    {

        a = Convert.ToInt32(t.Text);

        t.Text = "";

        b = '/';

        t.Text += b;

    }

    protected void eql_Click(object sender, EventArgs e)

    {

        c = Convert.ToInt32(t.Text);

        t.Text = "";

        if (b == '/')

        {

            d = a / c;

            t.Text += d;

            a = d;

        }

        else if (b == '+')

        {

            d = a + c;

            t.Text += d;

            a = d;

        }

        else if (b == '-')

        {

            d = a - c;

            t.Text += d;

            a = d;

        }

        else

        {

            d = a * c;

            t.Text += d;

            a = d;

        }

    }

    protected void clr_Click(object sender, EventArgs e)

    {

        t.Text = "";

    }

}