I'm asked to make a calculator with similar Windows calculator. I'm wondering where is the problem. I can press Button1 + Button2, But I can't Press the Button "=". code behind there I Created a method for the "Equal_Click". But it seems not working. Is this because of the String couldn't be together with Bool? I kept pressing the "=" and it showed nothing. Did anyone see where the problem is?
**HERE IS MY FRONT CODE **
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 369px; width: 363px">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="result" runat="server" Text="0" Height="34px" Width="216px" ReadOnly="true"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="1" Height="36px" Width="41px" OnClick="Button_Click" />
<asp:Button ID="Button2" runat="server" Text="2" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button3" runat="server" Text="3" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button14" runat="server" Text="+" Height="36px" Width="41px" OnClick="Operator_Click"/>
<asp:Button ID="Button17" runat="server" Text="CE" Height="36px" Width="41px" OnClick="Button17_Click"/>
<br />
<asp:Button ID="Button4" runat="server" Text="4" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button5" runat="server" Text="5" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button6" runat="server" Text="6" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button13" runat="server" Text="-" Height="36px" Width="41px" OnClick="Operator_Click"/>
<asp:Button ID="Button16" runat="server" Text="C" Height="36px" Width="41px"/>
<br />
<asp:Button ID="Button7" runat="server" Text="7" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button8" runat="server" Text="8" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button9" runat="server" Text="9" Height="36px" Width="41px" OnClick="Button_Click"/>
<asp:Button ID="Button12" runat="server" Text="*" Height="36px" Width="41px" OnClick="Operator_Click"/>
<br />
<asp:Button ID="Button10" runat="server" Text="0" Height="36px" Width="86px" OnClick="Button_Click"/>
<asp:Button ID="Button11" runat="server" Text="." Height="36px" Width="41px" OnClick="Operator_Click"/>
<asp:Button ID="Button15" runat="server" Text="/" Height="36px" Width="41px" OnClick="Operator_Click"/>
<asp:Button ID="Equal" runat="server" Text="=" Height="36px" Width="41px" OnClick="Equal_Click"/>
</div>
</form>
</body>
</html>
AND HERE IS MY CODE-BEHIND is there any wrong logic here?
public partial class calculator : System.Web.UI.Page
{
Double value = 0;
string operation = "";
bool operation_press = false;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Equal_Click(object sender, EventArgs e)
{
operation_press = false;
switch (operation)
{
case "+":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
case "-":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
case "*":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
case "/":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
default:
break;
}
}
protected void Button_Click(object sender, EventArgs e)
{
if ((result.Text == "0") || (operation_press))
result.Text = string.Empty;
Button b = (Button)sender;
result.Text = result.Text + b.Text;
}
protected void Button17_Click(object sender, EventArgs e)
{
result.Text = "0";
}
protected void Operator_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
result.Text = result.Text + b.Text;
//value = Convert.ToDouble(Double.Parse(result.Text);
operation = b.Text;
operation_press = true;
}
}
It looks like you are trying to convert the entire value of the TextBox to an interger. That cannot work because the TextBox also holds the +
or *
etc. Furthermore the value of operation
will always be empty when calculating the total because you do not save it in a state in Operator_Click
.
protected void Equal_Click(object sender, EventArgs e)
{
//get the values for calculation
int[] results = Regex.Split(result.Text, @"-|\+|\*|\/").Select(x => Convert.ToInt32(x)).ToArray();
//get the operation
string operation = Regex.Split(result.Text, @"[0-9]+")[1];
//do the calculation
switch (operation)
{
case "+":
result.Text = (results[0] + results[1]).ToString();
break;
case "-":
result.Text = (results[0] - results[1]).ToString();
break;
case "*":
result.Text = (results[0] * results[1]).ToString();
break;
case "/":
result.Text = (results[0] / results[1]).ToString();
break;
default:
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.