C# 加减乘除求余

求 代码,要求计算加减乘除求余,从命令行输入计算,无需考虑括号。

当两个运算符连在一起 即 2 + - 1就报错。

多谢大家了呵呵

最后应该例如可以计算 Ass.exe 1 - 2 + 4这样的表达式在命令行下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private int jisuan;
private double d1;//记录点+号之前的数字
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "7";
}
textBox1.Text += "7";
}

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

private void button3_Click(object sender, EventArgs e)
{
jisuan = 1;
try
{
d1 = Convert.ToDouble(textBox1.Text);
textBox1.Text += "+";
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
}

private void button4_Click(object sender, EventArgs e)
{
int i = 1;
char[] ch ={ '+','-','*','/' };
string[]s=this.textBox1.Text.Split(ch);
double d2 =Convert.ToDouble(s[i]);
if (jisuan == 1)
{
textBox1.Text = (d1 + d2).ToString();
}
else if (jisuan == 2)
{
textBox1.Text = (d1 - d2).ToString();
}
else if (jisuan == 3)
{
textBox1.Text = (d1 * d2).ToString();
}
else if (jisuan == 4)
{
textBox1.Text = (d1 / d2).ToString();
}
}

private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "9";
}
textBox1.Text += "9";
}

private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "4";
}
textBox1.Text += "4";
}

private void button7_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "5";
}
textBox1.Text += "5";
}

private void button8_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "6";
}
textBox1.Text += "6";
}

private void button9_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "1";
}
textBox1.Text += "1";
}

private void button10_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "2";
}
textBox1.Text += "2";
}

private void button11_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = "";
textBox1.Text += "3";
}
textBox1.Text += "3";
}

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

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

private void button14_Click(object sender, EventArgs e)
{
jisuan = 2;
d1 = Convert.ToDouble(textBox1.Text);
textBox1.Text += "-";
}

private void button15_Click(object sender, EventArgs e)
{
jisuan = 3;
d1 = Convert.ToDouble(textBox1.Text);
textBox1.Text += "*";
}

private void button16_Click(object sender, EventArgs e)
{
jisuan = 4;
d1 = Convert.ToDouble(textBox1.Text);
textBox1.Text += "/";
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-05
何老师……作业要自己完成 不可以这样哒……本回答被提问者采纳
相似回答