计算器的封装

一、代码实现

Form1代码如下:

namespace szys
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
        public static int right = 0;
        public static int Count = 0;
        private int t;
        string path = ".\text1.txt";
        int a = 0;
        private void button5_Click(object sender, EventArgs e)
        {
            int a = int.Parse(textBox1.Text.Trim());
            int b = int.Parse(textBox2.Text.Trim());
            Char c = Convert.ToChar(textBox4.Text.Trim());
            Lei con = new Lei();
            con.Max(a,b,c);
            Count++;
            StreamWriter baocun1 = File.AppendText("baocun1.txt");
            baocun1.WriteLine(textBox1.Text);
            baocun1.Close();
            StreamWriter baocun2 = File.AppendText("baocun2.txt");
            baocun2.WriteLine(textBox4.Text);
            baocun2.Close();
            StreamWriter baocun3 = File.AppendText("baocun3.txt");
            baocun3.WriteLine(textBox2.Text);
            baocun3.Close();
            richTextBox1.Text += textBox1.Text + textBox4.Text + textBox2.Text + label2.Text + textBox3.Text + "\n";
            textBox1.Clear();
            textBox4.Clear();
            textBox2.Clear();

        }
        private void btnsave_Click(object sender, EventArgs e)//保存已出试题;
        {
            SaveFileDialog TxtSaveDialog = new SaveFileDialog();
            TxtSaveDialog.Filter = "文本文档(*.txt)|*.txt";
            if (File.Exists(path))
            {

                this.richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
                MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.richTextBox1.Clear();
                btnsave.Enabled = false;

            }
            else
            {
                if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
                {

                    this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.PlainText);
                    MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Clear();
                    btnsave.Enabled = false;
                    button1.Visible = true;
                }

            }
        }
        private void button1_Click(object sender, EventArgs e)//打开试题;
        {
           

            OpenFileDialog TxTOpenDialog = new OpenFileDialog();
            TxTOpenDialog.Filter = "文本文档(*.txt)|*.txt";
            if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
            {
                path = TxTOpenDialog.FileName;
                this.richTextBox2.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.PlainText);
                btnsave.Enabled = false;
                button1.Enabled = false;
                MessageBox.Show("打开成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);


            }

        }
        private void button2_Click(object sender, EventArgs e)//添加算式题;
        {
           
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox4.Enabled = false;
            textBox1.BackColor = Color.Yellow;
            textBox4.BackColor = Color.Yellow;
            textBox2.BackColor = Color.Yellow;
            textBox3.BackColor = Color.Yellow;
            string[] m = new string[100];
            m = File.ReadAllLines("baocun1.txt");
            textBox1.Text = m[a];
            string[] n = new string[100];
            n = File.ReadAllLines("baocun2.txt");
            textBox4.Text = n[a];
            string[] v = new string[100];
            v = File.ReadAllLines("baocun3.txt");
            textBox2.Text = v[a];
            a++;

        }

        private void textBox3_MouseClick(object sender, MouseEventArgs e)//计时;
        {
            label3.Text = t.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            t = t + 1;
            label3.Text = t.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("一共用时" + label3.Text + "");
            Form2 frm2 = new Form2();
            frm2.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //调用;
            int a = int.Parse(textBox1.Text.Trim());
            int b = int.Parse(textBox2.Text.Trim());
            Char c = Convert.ToChar(textBox4.Text.Trim());
            Lei con = new Lei();
            con.Max(a,b,c);
           if (con.Answer == int.Parse(textBox3.Text.Trim()))
                {
                    MessageBox.Show("恭喜你,小朋友!回答正确!");
                    right++;
                    Count++;
                }

                else
                {

                    MessageBox.Show("回答错误,继续加油!小朋友");
                    Count++;
                    
                }
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();

            }

       
        }
    }

Form2代码如下:

namespace szys
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form1.Count.ToString();
            textBox2.Text = Form1.right.ToString();
            textBox4.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";

        }

     }
 }

封装代码:

namespace szys
{
    class Lei
    {
        public int Sum;
        
        public int Answer
        {
            get { return Sum; }

        }
        public int Max(int n1,int n2,char yunsuanfu)
        {
            if (yunsuanfu == '+')
            {
                return Sum = n1 + n2;
            }
            else if (yunsuanfu == '-')
            {
                if (n1 > n2)
                {
                    return Sum = n1 - n2;
                }
                else
                {
                    MessageBox.Show("第一个数要大于第二个数!");
                    
                }
            }
            else if (yunsuanfu == '/')
            {
                if (n2 == 0 || n2 < 0)
                {
                    MessageBox.Show("分母不能为0且大于0!");
                }
                else
                {
                    return Sum = n1 / n2;
                }
            }
            else if (yunsuanfu == '*')
            {
                return Sum = n1 * n2;

            }
            return Sum;


        }

    }
}

 

二、测试

 

posted @ 2015-11-25 20:27  twinkle缘  阅读(206)  评论(1编辑  收藏  举报