c#计算器,但是一运行就死了 求解答
程序代码:
[local]1[/local]using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace caculator { public partial class Form1 : Form { Stack<char > opr = new Stack<char>(20); Stack<double> number = new Stack<double>(20); string num1; string[]opre=new string[20]; int z=0; public Form1() { InitializeComponent(); opr.Push('#'); number.Push(-10000); } private void Form1_Load(object sender, EventArgs e) { } private void button14_Click(object sender, EventArgs e) { textBox1.Text += button14.Text; num1 += textBox1.Text; } private void button13_Click(object sender, EventArgs e) { textBox1.Text += button13.Text; num1 += textBox1.Text; } private void button17_Click(object sender, EventArgs e) { textBox1.Text += button17.Text; num1 += textBox1.Text; } private void button19_Click(object sender, EventArgs e) { textBox1.Text += button19.Text; num1 += textBox1.Text; } private void button18_Click(object sender, EventArgs e) { textBox1.Text += button18.Text; num1 += textBox1.Text; } private void button16_Click(object sender, EventArgs e) { textBox1.Text += button16.Text; num1 += textBox1.Text; } private void button24_Click(object sender, EventArgs e) { textBox1.Text += button24.Text; num1 += textBox1.Text; } private void button28_Click(object sender, EventArgs e) { textBox1.Text += button28.Text; num1 += textBox1.Text; } private void button26_Click(object sender, EventArgs e) { textBox1.Text += button26.Text; num1 += textBox1.Text; } private void button27_Click(object sender, EventArgs e) { textBox1.Text = ""; } private void button8_Click(object sender, EventArgs e) { textBox1.Text += button8.Text; opre[z] = "+"; z++; } private void button23_Click(object sender, EventArgs e) { int a = textBox1.Text.Length; if(a>0) textBox1.Text= textBox1.Text.Remove(a-1); } private void button12_Click(object sender, EventArgs e) { string[] numbers1 = textBox1.Text.Split('+','-','*','/'); int i = 0, j = 0; double result = 0; number.Push(Convert.ToDouble(numbers1[j])); j++; while (i<z&&j<numbers1.Length) { if (disOpreIs(Convert.ToChar(opre[i])) == '0') { opr.Push(Convert.ToChar(opre[i])); i++; number.Push(Convert.ToDouble(numbers1[j])); j++; } if (disOpreIs(Convert.ToChar(opre[i])) != '0') { if (Convert.ToChar(opre[i]) == '*') { result = number.Pop() * Convert.ToDouble(numbers1[j]); number.Push(result); j++; i++; } if (Convert.ToChar(opre[i]) == '/') { result =( number.Pop()) /(Convert.ToDouble(numbers1[j])); number.Push(result); j++; i++; } } } while (opr.Peek() != '#') { switch (opr.Peek()) { case '+': { double num = number.Pop(); result= num + number.Pop(); opr.Pop(); number.Push(Convert.ToDouble(result)); }break; case '-': { double num = number.Pop(); result = num + number.Pop(); opr.Pop(); number.Push(Convert.ToDouble(result)); } break; case '*': { double num = number.Pop(); result = num + number.Pop(); opr.Pop(); number.Push(Convert.ToDouble(result)); } break; case '/': { double num = number.Pop(); result = num + number.Pop(); opr.Pop(); number.Push(Convert.ToDouble(result)); } break; } } textBox1.Text =Convert.ToString(result); } private void button2_Click(object sender, EventArgs e) { textBox1.Text += button2.Text; opre[z] = "-"; z++; } private void button4_Click(object sender, EventArgs e) { textBox1.Text += button4.Text; opre[z] = "*"; z++; } private void button6_Click(object sender, EventArgs e) { textBox1.Text += button6.Text; opre[z] = "/"; z++; } public char disOpreIs(char c) { char c1=new char(); if ((c == '+' || c == '-') && (Convert.ToChar(opr.Peek()) == '+' || Convert.ToChar(opr.Peek()) == '-'||Convert.ToChar(opr.Peek()) == '#')) { c1 = '0'; } if ((c == '*' || c == '/') && (Convert.ToChar(opr.Peek()) == '+' || Convert.ToChar(opr.Peek()) == '-'||Convert.ToChar(opr.Peek()) == '#')) { c1 = '0'; } if ((c == '*' || c == '/') && (Convert.ToChar(opr.Peek()) == '*' || Convert.ToChar(opr.Peek()) == '/'||Convert.ToChar(opr.Peek()) == '#')) { c1 = '0'; } if ((c == '+' || c == '-') && (Convert.ToChar(opr.Peek()) == '*' || Convert.ToChar(opr.Peek()) == '/'||Convert.ToChar(opr.Peek()) == '#')) { c1 = c; } return c1; } private void button11_Click(object sender, EventArgs e) { double temp = double.Parse(textBox1.Text); textBox1.Text = (temp * temp).ToString(); } private void button10_Click(object sender, EventArgs e) { double temp = double.Parse(textBox1.Text); textBox1.Text = (temp * temp * temp).ToString(); } } }
caculator.zip
(60.37 KB)