Sunday, August 3, 2014

Calculater

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

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<fieldsetstyle=" width:300px">
<legend>Evaluate String arithmetic Expression</legend>
<asp:TextBoxID="txtEquation"runat="server"></asp:TextBox>
<asp:ButtonID="btnEvaluate"runat="server"Text="Evaluate"
onclick="btnEvaluate_Click"/>

<asp:ButtonID="btnReset"runat="server"Text="Reset"
onclick="btnReset_Click"/>
<br/>
<asp:LabelID="lblResult"runat="server"Text=""></asp:Label>
</fieldset>


</div>
</form>
</body>
</html>

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data;
usingSystem.Drawing;


publicpartialclass_Default : System.Web.UI.Page
{
protectedvoidPage_Load(object sender, EventArgs e)
    {

    }
privateobjectEvaluateExpression(stringeqn)
    {
DataTabledt = newDataTable();
var result = dt.Compute(eqn, string.Empty);
return result;
    }
protectedvoidbtnEvaluate_Click(object sender, EventArgs e)
    {
try
        {
string result = Convert.ToString(EvaluateExpression(txtEquation.Text.Trim()));
lblResult.Text = "Result: " + result;
lblResult.ForeColor = Color.Green;
        }
catch (Exception ex)
        {
lblResult.Text = "Oops!! erroroccured: " + ex.Message.ToString();
lblResult.ForeColor = Color.Red;
        }
    }

protectedvoidbtnReset_Click(object sender, EventArgs e)
    {
txtEquation.Text = string.Empty;
txtEquation.Focus();
lblResult.Text = string.Empty;
    }



}