C#写一个可以进行四则混合运算的程序,包括一级括号。期待您的回答,谢谢

我想要代码!谢谢... 我想要代码!谢谢 展开
 我来答
匿名用户
2011-11-03
展开全部
public partial class Form1 : Form
{
private bool isFirst = true;
private Operate o = null;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 1)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
else
{
textBox1.Text = "0";
}

}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
}

private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
}

private void button4_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "1";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "1";
}
else
{
textBox1.Text = textBox1.Text + "1";
}
}
this.isFirst = false;
}

private void button5_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "2";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "2";
}
else
{
textBox1.Text = textBox1.Text + "2";
}
}
this.isFirst = false;
}

private void button6_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "3";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "3";
}
else
{
textBox1.Text = textBox1.Text + "3";
}
}
this.isFirst = false;
}

private void button8_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "4";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "4";
}
else
{
textBox1.Text = textBox1.Text + "4";
}
}
this.isFirst = false;
}

private void button9_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "5";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "5";
}
else
{
textBox1.Text = textBox1.Text + "5";
}
}
this.isFirst = false;
}

private void button10_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "6";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "6";
}
else
{
textBox1.Text = textBox1.Text + "6";
}
}
this.isFirst = false;
}

private void button12_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "7";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "7";
}
else
{
textBox1.Text = textBox1.Text + "7";
}
}
this.isFirst = false;
}

private void button13_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "8";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "8";
}
else
{
textBox1.Text = textBox1.Text + "8";
}
}
this.isFirst = false;
}

private void button14_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "9";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "9";
}
else
{
textBox1.Text = textBox1.Text + "9";
}
}
this.isFirst = false;
}

private void button17_Click(object sender, EventArgs e)
{
if (isFirst)
{
textBox1.Text = "0";
}
else
{
if (textBox1.Text == "0")
{
textBox1.Text = "0";
}
else
{
textBox1.Text = textBox1.Text + "0";
}
}
this.isFirst = false;
}

private void button7_Click(object sender, EventArgs e)
{
if (o == null)
{
o = Factory.getOperator("+");
o.OpA = int.Parse(textBox1.Text);
}
else
{
o.OpB = int.Parse(textBox1.Text);
textBox1.Text = o.getResult().ToString();
o = Factory.getOperator("+");
o.OpA = int.Parse(textBox1.Text);
}
this.isFirst = true;
}

private void button11_Click(object sender, EventArgs e)
{
if (o == null)
{
o = Factory.getOperator("-");
o.OpA = int.Parse(textBox1.Text);
}
else
{
o.OpB = int.Parse(textBox1.Text);
textBox1.Text = o.getResult().ToString();
o = Factory.getOperator("-");
o.OpA = int.Parse(textBox1.Text);
}
this.isFirst = true;

}

private void button15_Click(object sender, EventArgs e)
{
if (o == null)
{
o = Factory.getOperator("*");
o.OpA = int.Parse(textBox1.Text);
}
else
{
o.OpB = int.Parse(textBox1.Text);
textBox1.Text = o.getResult().ToString();
o = Factory.getOperator("*");
o.OpA = int.Parse(textBox1.Text);
}
this.isFirst = true;

}

private void button19_Click(object sender, EventArgs e)
{
if (o == null)
{
o = Factory.getOperator("/");
o.OpA = int.Parse(textBox1.Text);

}
else
{
o.OpB = int.Parse(textBox1.Text);
textBox1.Text = o.getResult().ToString();
o = Factory.getOperator("/");
o.OpA = int.Parse(textBox1.Text);
}
this.isFirst = true;

}

private void button16_Click(object sender, EventArgs e)
{
if (o != null)
{
o.OpB = int.Parse(textBox1.Text);
textBox1.Text = o.getResult().ToString();
o = null;
}
this.isFirst = true;
}
}
NotigerKuiKui
2011-10-25 · TA获得超过2802个赞
知道大有可为答主
回答量:1592
采纳率:76%
帮助的人:463万
展开全部
建立运算符优先表,在用栈来做,两个栈,一个操作数栈,一个运算符栈,
数据结构讲过吧,规则就是优先表建立好,N级括号都不怕,还能检测不匹配的括号...
和递归撤不上关系吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wangyu20052005
2011-10-25 · TA获得超过436个赞
知道小有建树答主
回答量:348
采纳率:0%
帮助的人:112万
展开全部
要是我,我会用递归的方法做
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式