大家好,我正在尝试构建科学计算器,但是我的问题是我的“ =”仅返回2位数字的运算...我该怎么做堆栈工作并制定大公式?
例如: 2 + 4正常工作,但是 2 + 4 * 5-6无效,并且我的程序崩溃了。我知道switch语句仅适用于2位数字,但我想学习如何使用堆栈。 谢谢!
这是我的代码。
namespace clcProject
{
public partial class Form1 : Form
{
double num1;
double num2, result;
string operation;
string Multiple;
Stack<int> mystack = new Stack<int>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button18_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "8";
}
private void number9_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "9";
}
private void button34_Click(object sender, EventArgs e)
{
textBox1.Text += ")";
}
private void input(string a)
{
if (textBox1.Text == "0")
textBox1.Text = a;
else
textBox1.Text += a;
}
private void Clear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void button0_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "0";
}
private void number1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "1";
}
private void number2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "2";
}
private void number3_Click(object sender, EventArgs e)
{
if(textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "3";
}
private void number4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "4";
}
private void number5_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "5";
}
private void number6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "6";
}
private void number7_Click(object sender, EventArgs e)
{
if (textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
textBox1.Text += "7";
}
private void Backspace_Click(object sender, EventArgs e)
{
if(textBox1.Text == "First you have to input the number.")
{
textBox1.Text = String.Empty;
}
int textLenght = textBox1.Text.Length;
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Substring(0, textLenght - 1);
}
textBox1.Focus();
textBox1.SelectionStart = textLenght;
}
private void divide_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
operation = "/";
textBox1.Text = String.Empty;
}
private void plus_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
operation = "+";
textBox1.Text = String.Empty;
}
private void minus_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
operation = "-";
textBox1.Text = String.Empty;
}
private void multiple_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
operation = "*";
textBox1.Text = String.Empty;
}
private void equals_Click(object sender, EventArgs e)
{
num2 = double.Parse(textBox1.Text);
mystack.push(num1);
switch (operation)
{
case "+":
textBox1.Text = (num1 + num2).ToString();
break;
case "-":
textBox1.Text = (num1 - num2).ToString();
break;
case "*":
textBox1.Text = (num1 * num2).ToString();
break;
case "/":
if (num2 == 0)
textBox1.Text = "DIVIDED WITH 0";
else
textBox1.Text = (num1 / num2).ToString();
break;
case "x^y":
result = Math.Pow(Convert.ToDouble(num1), Convert.ToDouble(num2));
textBox1.Text = result.ToString();
break;
default:
break;
}
}
/* private void multiple_acts(object sender, EventArgs e)
{
double[] num3 = new double[100];
operation = "++";
}*/
private void CE_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
if (textBox1.Text.Length == 0)
{
textBox1.Text = "First you have to input the number.";
}
else
{
textBox1.Text = (Math.Sqrt(double.Parse(textBox1.Text))).ToString();
}
}
private void log_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "First you have to input the number.";
}
else
textBox1.Text = (Math.Log10(double.Parse(textBox1.Text))).ToString();
}
private void tan_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "First you have to input the number.";
}
else
textBox1.Text = (Math.Tan(double.Parse(textBox1.Text))).ToString();
}
private void cos_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "First you have to input the number.";
}
else
textBox1.Text = (Math.Cos(double.Parse(textBox1.Text))).ToString();
}
private void sin_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "First you have to input the number.";
}
else
textBox1.Text = (Math.Sin(double.Parse(textBox1.Text))).ToString();
}
private void upNumber_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
if (textBox1.Text.Length == 0)
{
textBox1.Text = "First you have to input the number.";
}
else
{
operation = "x^y";
textBox1.Text = String.Empty;
}
}
private void factorial_Click(object sender, EventArgs e)
{
long f = 1;
for (int i = 1; i <= long.Parse(textBox1.Text); i++)
f = f * i;
textBox1.Text = f.ToString();
}
private void Pi_Click(object sender, EventArgs e)
{
double pi = 3.14159265359;
textBox1.Text = pi.ToString();
}
private void x10_Click(object sender, EventArgs e)
{
textBox1.Text = (Math.Log(double.Parse(textBox1.Text))).ToString();
}
private void square_Click(object sender, EventArgs e)
{
result = Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox1.Text);
textBox1.Text = result.ToString();
}
private void left_Click(object sender, EventArgs e)
{
textBox1.Text += "(";
}
private void label1_Click(object sender, EventArgs e)
{
}
private void dot_Click(object sender, EventArgs e)
{
textBox1.Text += ",";
}
}
}
对不起,这个烂摊子!