很着急,下午要交作业了,

小白兔 发布于 2010/12/28 09:27
阅读 325
收藏 2

要求做题个简易计算器,编好后运行起来就一个框框,点击没反应,求大大们帮我看看

button1到18分别是1,2,3,4,5,6,7,8,9,0,.,=,+,-,*。/,作者
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        string strOper = "+";

        double sum = 0;
        bool blnClear = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }


            Button b1 = (Button)sender;

            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }

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


            Button b1 = (Button)sender;

            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;



        }


        private void button2_Click(object sender, EventArgs e)
        {



        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

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

            int n = textBox1.Text.IndexOf(".");
            if (n == -1)
                textBox1.Text = textBox1.Text + ".";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button13_Click(object sender, EventArgs e)
        {
            double dbSecond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSecond;
                        break;
                    case "-":
                        sum -= dbSecond;
                        break;
                    case "*":
                        sum *= dbSecond;
                        break;
                    case "/":
                        sum /= dbSecond;
                        break;

                }



            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            double dbSecond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSecond;
                        break;
                    case "-":
                        sum -= dbSecond;
                        break;

                }
            if (sender == button12)
                strOper = "+";
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            sum = 0;
            blnClear = false;
            strOper = "+";

        }

        private void button14_Click(object sender, EventArgs e)
        {
            double dbSecond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSecond;
                        break;
                    case "-":
                        sum -= dbSecond;
                        break;


                }
            if (sender == button14)
                strOper = "-";
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;

        }

        private void button13_Click_1(object sender, EventArgs e)
        {
            double dbSecond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSecond;
                        break;

                }
            if (sender == button13)
                strOper = "+";

            textBox1.Text = Convert.ToString(sum);
            blnClear = true;

        }

        private void button15_Click(object sender, EventArgs e)
        {
            double dbSecond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)

                switch (strOper)
                {
                    case "+":
                        sum += dbSecond;
                        break;

                    case "*":
                        sum *= dbSecond;
                        break;
                }
            if (sender == button15)
                strOper = "*";
            textBox1.Text = "=";
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;

        }

        private void button16_Click(object sender, EventArgs e)
        {
            double dbSecond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)

                switch (strOper)
                {
                    case "+":
                        sum += dbSecond;
                        break;

                    case "*":
                        sum *= dbSecond;
                        break;
                    case "/":
                        sum /= dbSecond;
                        break;
                }
            if (sender == button16)
                strOper = "/";
            textBox1.Text = "=";
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;

        }

        private void button18_Click(object sender, EventArgs e)
        {
            button18.Text = "金涛\n09927211";


        }

    }


 }
 
加载中
0
燃灯
燃灯

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace caculater
{
    class Program
    {
        public static void Main()
        {
            string s = getstring();
            List<double> ld = splitnum(s);
            List<char> lc = operato(s);
            cal(ref ld, '/', ref lc);
            cal(ref ld, '*',ref lc);
            cal(ref ld, '+',ref lc);
            cal(ref ld, '-',ref lc);

            end();
        }

        public static List <double> splitnum(string temp)
        {
            char[] opera= {'+','-','*','/'};
            string[] t1=temp.Split (opera );
            double[] t2 = new double[t1.Length];
            for (int i = 0; i < t1.Length; i++)
            {
                t2[i] = double .Parse(t1[i]);
            }
            return t2.ToList ();
        }
        public static List<char> operato(string temp)
        {

            return temp.ToCharArray().ToList().FindAll(compare);
        }
        public static bool compare(char c)
        {
            if (c == '+' || c == '-' || c == '/' || c == '*')
                return true;
            else
                return false;
        }

        public static double cal(double num1, char op, double num2)
        {
            double result = 0;
            switch (op)
            {
                case '+':
                    result = num1 + num2;
                    break;
                case '-':
                    result = num1 - num2;
                    break;
                case '*':
                    result = num1 * num2;
                    break;
                case '/':
                    result = num1 / num2;
                    break;
            }
            return  result;
        }
        public static void cal(ref List<double> ld, char ch,ref List<char> lc)
        {
            while (lc.Contains(ch))
            {
                int m = lc.IndexOf(ch);
                ld[m] = cal(ld[m], ch, ld[m + 1]);
                ld.RemoveAt(m + 1);
                lc.RemoveAt(m);
                show(ld, lc);
            }
        }

        public static void show(List<double> ld, List<char> lc)
        {
            for (int i = 0; i < lc.Count; i++)
            {
                Console.Write(ld[i] + lc[i].ToString());
            }
            Console.WriteLine(ld[lc.Count]);
        }
        
        public static string getstring()
        {
            Console.Write("请输入:");
            return Console.ReadLine();
        }
        public static void end()
        {
            Console.WriteLine("------------------------END------------------------");
            Console.ReadLine();
        }
    }

}

0
张林
张林

呀,看语法像C#,帮不了你了,要java的话我一早上能给你整出一个带图形界面的。

0
小白兔
小白兔

是C#啦,

0
ddatsh
ddatsh

要被打PP了

0
G.
G.

没事,老师不检查的.

0
小白兔
小白兔

引用来自#4楼“张林”的帖子

呀,看语法像C#,帮不了你了,要java的话我一早上能给你整出一个带图形界面的。

 心领了

0
小白兔
小白兔

哈哈,不过我整了一个多小时,都搞得差不多勒,遗憾的是乘除还是不行,谢谢各位了,我再鼓捣鼓捣

0
于忠达
于忠达

其他按钮的事件直接调用第一个按钮的事件方法呀

0
打酱油君
打酱油君

C#挺简单的,我也弄过几天C#的 。

返回顶部
顶部