<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style=" width:300px">
<legend>Evaluate String Arithmetic Expression</legend>
<asp:TextBox ID="TxtEquation"
runat="server"></asp:TextBox>
<asp:Button ID="BtnCalculate"
runat="server"
Text="Calculate"
onclick="btnCalculate_Click"
/>
<asp:Button ID="btnReset"
runat="server"
Text="Reset"
onclick="btnReset_Click"
/>
<br />
<asp:Label ID="lblResult"
runat="server"
Text=""></asp:Label>
</fieldset>
</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;
using System.Data;
using System.Drawing;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
private object
EvaluateExpression(string Equation)
{
DataTable dt = new
DataTable();
var Result = dt.Compute(Equation, string.Empty);
return Result;
}
protected void btnReset_Click(object sender, EventArgs
e)
{
TxtEquation.Text = "";
TxtEquation.Focus();
}
protected void
btnCalculate_Click(object sender, EventArgs e)
{
try
{
var Result = Convert.ToString(EvaluateExpression(TxtEquation.Text.Trim()));
lblResult.Text = " Your Result"
+ Result;
lblResult.ForeColor = Color.Blue;
}
catch (Exception
ex)
{
lblResult.Text = "Ooops!" +
ex.Message.ToString();
lblResult.ForeColor = Color.Red;
}
}
}
No comments:
Post a Comment