| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 609 人关注过本帖
标题:c#计算器,但是一运行就死了 求解答
只看楼主 加入收藏
qwop12358
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
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)
搜索更多相关主题的帖子: 计算器 public color 
2012-12-23 16:49
qwop12358
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-4
收藏
得分:0 
希望大神帮帮忙   调了一个下午了
2012-12-23 16:57
枫叶离开树
Rank: 2
等 级:论坛游民
帖 子:17
专家分:72
注 册:2012-10-1
收藏
得分:14 
算结果的时候提示有一个是Null,所以调试一下吧。
2012-12-23 17:00
qwop12358
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-4
收藏
得分:0 
回复 3楼 枫叶离开树
对啊    我调了好久了     搞得头都晕死  
2012-12-23 17:04
枫叶离开树
Rank: 2
等 级:论坛游民
帖 子:17
专家分:72
注 册:2012-10-1
收藏
得分:0 
目前电脑没办法运行vs,刚才运行看到是null电脑就重启了。所以你看下那个东西是不是传值得时候没传过去。看看他是在哪里获取值得。
2012-12-23 17:29
快速回复:c#计算器,但是一运行就死了 求解答
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061628 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved