我正在制作一个简单的计算器。到目前为止,我已成功地在计算器中实现了一些基本功能。看看代码
public double num1 { get; set; }
public double num2 { get; set; }
public string op { get; set; }
// public bool checker { get; set; }
private void ButtonBase_OnClick1(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button1.Content.ToString();
ShowTextBlock.Text += Button1.Content.ToString();
}
private void ButtonBase_OnClick2(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button2.Content.ToString();
ShowTextBlock.Text += Button2.Content.ToString();
}
private void ButtonBase_OnClick3(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button3.Content.ToString();
ShowTextBlock.Text += Button3.Content.ToString();
}
private void ButtonBase_OnClick4(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button4.Content.ToString();
ShowTextBlock.Text += Button4.Content.ToString();
}
private void ButtonBase_OnClick5(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button5.Content.ToString();
ShowTextBlock.Text += Button5.Content.ToString();
}
private void ButtonBase_OnClick6(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button6.Content.ToString();
ShowTextBlock.Text += Button6.Content.ToString();
}
private void ButtonBase_OnClick7(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button7.Content.ToString();
ShowTextBlock.Text += Button7.Content.ToString();
}
private void ButtonBase_OnClick8(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button8.Content.ToString();
ShowTextBlock.Text += Button8.Content.ToString();
}
private void ButtonBase_OnClick9(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button9.Content.ToString();
ShowTextBlock.Text += Button9.Content.ToString();
}
private void ButtonBase_OnClick0(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Button0.Content.ToString();
ShowTextBlock.Text += Button0.Content.ToString();
}
private void ButtonBase_OnClickdot(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Buttondot.Content.ToString();
ShowTextBlock.Text += Buttondot.Content.ToString();
}
private void ButtonBase_OnClickobrac(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Buttonobrac.Content;
ShowTextBlock.Text += Buttonobrac.Content.ToString();
}
private void ButtonBase_OnClickcbrac(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Buttoncbrac.Content;
ShowTextBlock.Text += Buttoncbrac.Content.ToString();
}
//private void ButtonBase_OnClickinf(object sender, RoutedEventArgs e)
//{
// CalculateTextBlock.Text += ("0/0").ToString();
// ShowTextBlock.Text += "Inf.";
//}
private void ButtonBase_OnClickplus(object sender, RoutedEventArgs e)
{
//num1 += double.Parse(CalculateTextBlock.Text);
num1 = Convert.ToDouble(CalculateTextBlock.Text);
op = "plus";
CalculateTextBlock.Text = "";
ShowTextBlock.Text += "+";
}
private void ButtonBase_OnClickminus(object sender, RoutedEventArgs e)
{
num1 += double.Parse(CalculateTextBlock.Text);
op = "sub";
CalculateTextBlock.Text = "";
ShowTextBlock.Text += "-";
}
private void ButtonBase_OnClickmul(object sender, RoutedEventArgs e)
{
num1 += double.Parse(CalculateTextBlock.Text);
op = "mul";
CalculateTextBlock.Text = "";
ShowTextBlock.Text += "*";
}
private void ButtonBase_OnClickdiv(object sender, RoutedEventArgs e)
{
num1 += double.Parse(CalculateTextBlock.Text);
op = "div";
CalculateTextBlock.Text = "";
ShowTextBlock.Text += "/";
}
private void ButtonBase_OnClickequal(object sender, RoutedEventArgs e)
{
switch (op)
{
case "plus" :
//num2 = num1 + double.Parse(CalculateTextBlock.Text);
num2 = num1 + Convert.ToDouble(CalculateTextBlock.Text);
break;
case "sub":
num2 = num1 - double.Parse(CalculateTextBlock.Text);
break;
case "mul":
num2 = num1*double.Parse(CalculateTextBlock.Text);
break;
case "div":
num2 = num1/double.Parse(CalculateTextBlock.Text);
break;
}
CalculateTextBlock.Text = num2.ToString();
num1 = 0;
ShowTextBlock.Text = "";
}
private void ButtonBase_OnClickclear(object sender, RoutedEventArgs e)
{
num1 = 0;
num2 = 0;
ShowTextBlock.Text = "";
CalculateTextBlock.Text = "";
}
当我将括号插入calculatetextbox时会出现问题。它在解析时抛出FormatException。我有什么方法可以在这里实施BIDMAS吗?
注意:我已尝试解析和转换方法{int.parse和convert}
感谢任何帮助。感谢
答案 0 :(得分:1)
如果需要实现BIDMAS规则和括号,则应首先解析输入并为每个表达式生成标记。获得每个令牌后,按照规则进行计算。
除此之外,这个问题在这里得到解答:dynamic expression evaluation
关于堆栈溢出的另一篇文章也引用了这个
答案 1 :(得分:0)
似乎只是缺少.ToString()转换你的Bottonobrac.Content,你在Buttondot.Content中有它。
private void ButtonBase_OnClickdot(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Buttondot.Content.ToString(); //You have it here
ShowTextBlock.Text += Buttondot.Content.ToString();
}
private void ButtonBase_OnClickobrac(object sender, RoutedEventArgs e)
{
CalculateTextBlock.Text += Buttonobrac.Content; //Add .ToString()
ShowTextBlock.Text += Buttonobrac.Content.ToString();
}
这应该可以解决您的问题。同时,您始终可以使用compile and execute c# online来测试代码中缺少的内容。
答案 2 :(得分:0)
试试这个: -
<script type="text/javascript">
function Calculate() {
var expression = document.getElementById('<%=this.TextBox1.ClientID%>').value;
var result = eval(expression);
var hidden = document.getElementById('<%=this.hdfResult.ClientID%>');
hidden.value = result;
}
</script>
并在按钮单击事件上写下此OnClientClick="Calculate();
,即调用脚本。
有关详细信息,请查看此Link
或强>
试试这个
<form id="Calc" runat="server">
<div>
<table border= "4">
<tr>
<td>
<input type="text" name="Input" size="16" \>
<br />
</td>
</tr>
<tr>
<td>
<input type="button" name="one" value=" 1 " onclick ="Calc.Input.value += '1'" \>
<input type="button" name="two" value=" 2 " onclick="Calc.Input.value += '2'" \>
<input type="button" name="three" value=" 3 " onclick="Calc.Input.value += '3'" \>
<input type="button" name="plus" value=" + " onclick="Calc.Input.value += ' + '" \>
<br />
<input type="button" name="four" value=" 4 " onclick="Calc.Input.value += '4'" \>
<input type="button" name="five" value=" 5 " onclick="Calc.Input.value += '5'" \>
<input type="button" name="six" value=" 6 " onclick="Calc.Input.value += '6'" \>
<input type="button" name="minus" value=" - " onclick="Calc.Input.value += ' - '" \>
<br />
<input type="button" name="seven" value=" 7 " onclick="Calc.Input.value += '7'" \>
<input type="button" name="eight" value=" 8 " onclick="Calc.Input.value += '8'" \>
<input type="button" name="nine" value=" 9 " onclick="Calc.Input.value += '9'" \>
<input type="button" name="times" value=" x " onclick="Calc.Input.value += ' * '" \>
<br />
<input type="button" name="clear" value=" c " onclick="Calc.Input.value = ''" \>
<input type="button" name="zero" value=" 0 " onclick="Calc.Input.value += '0'" \>
<input type="button" name="DoIt" value=" = " onclick="Calc.Input.value = eval(Calc.Input.value)" \>
<input type="button" name="div" value=" / " onclick="Calc.Input.value += ' / '" \>
<br />
</td>
</tr>
</table> </div>
</form>
答案 3 :(得分:0)
@Prajjwal我还在Microsoft Visual Studio Express 2013 for Web上制作了一个简单的计算器。这是我的代码。看一看!希望它有所帮助:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void addButton_Click(object sender, EventArgs e)
{
double firstValue = double.Parse(firstTextBox.Text);
double secondValue = double.Parse(secondTextBox.Text);
double addValue = firstValue + secondValue;
resultLabel.Text = addValue.ToString();
}
protected void subtractButton_Click(object sender, EventArgs e)
{
double firstValue = double.Parse(firstTextBox.Text);
double secondValue = double.Parse(secondTextBox.Text);
double addValue = firstValue - secondValue;
resultLabel.Text = addValue.ToString();
}
protected void multiplicationButton_Click(object sender, EventArgs e)
{
double firstValue = double.Parse(firstTextBox.Text);
double secondValue = double.Parse(secondTextBox.Text);
double addValue = firstValue * secondValue;
resultLabel.Text = addValue.ToString();
}
protected void divideButton_Click(object sender, EventArgs e)
{
double firstValue = double.Parse(firstTextBox.Text);
double secondValue = double.Parse(secondTextBox.Text);
double addValue = firstValue / secondValue;
resultLabel.Text = addValue.ToString();
}
}