无法执行多个操作 [英] Cannot perform Multiple Operations
本文介绍了无法执行多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我已经完成了这段代码,发现
Well, I've been through this code and found out that
using System;
using System.Windows;
//houssem.dellai@ieee.org
//+216 95 325 964
using System.Windows.Controls;
using Microsoft.Phone.Controls;
namespace CalculatorWP
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void Number_Click(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
tb.Text += b.Content.ToString();
}
private void Result_click(object sender, RoutedEventArgs e)
{
try
{
result();
}
catch (Exception exc)
{
tb.Text = "Error!";
}
}
private void result()
{
String op;
int iOp = 0;
if (tb.Text.Contains("+"))
{
iOp = tb.Text.IndexOf("+");
}
else if (tb.Text.Contains("-"))
{
iOp = tb.Text.IndexOf("-");
}
else if (tb.Text.Contains("*"))
{
iOp = tb.Text.IndexOf("*");
}
else if (tb.Text.Contains("/"))
{
iOp = tb.Text.IndexOf("/");
}
else
{
//error
}
op = tb.Text.Substring(iOp, 1);
double op1 = Convert.ToDouble(tb.Text.Substring(0, iOp));
double op2 = Convert.ToDouble(tb.Text.Substring(iOp + 1, tb.Text.Length - iOp - 1));
if (op == "+")
{
tb.Text += "=" + (op1 + op2);
}
else if (op == "-")
{
tb.Text += "=" + (op1 - op2);
}
else if (op == "*")
{
tb.Text += "=" + (op1 * op2);
}
else
{
tb.Text += "=" + (op1 / op2);
}
}
private void Del_Click(object sender, RoutedEventArgs e)
{
tb.Text = "";
}
private void R_Click(object sender, RoutedEventArgs e)
{
if (tb.Text.Length > 0)
{
tb.Text = tb.Text.Substring(0, tb.Text.Length - 1);
}
}
}
}
一切都很好,但主要问题是我不能像3 + 3 + 3那样执行操作,它显示错误信息。请解释我如何做到这一点
Everything is good but the major issue is that I cannot Perform Operations Like 3+3+3, it shows an error message. Please explain me how to do this
推荐答案
上面的代码只会解析表单的公式:A < operator> B。
Your code above will only parse formulas of the form: A <operator> B.
如果你想要一个通用的解析器,
here 是一个使用关键词的搜索:算术解析器c#
If you want a generic parser, here is a search using the key words: arithmetic parser c#
希望这会有所帮助,
Mark
Hope this helps,
Mark
这篇关于无法执行多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文