计算器软件实现系列(三)代码封装

一  实现思路

1 用户编辑输入试题

2 用户编辑完试题后,可以进行保存,当然也可以直接做题

3 用户编辑完试题后,转到计算页面,输入计算时间,导入试题,就可以进行计算了

4 计算完毕后,可以根据自己情况是否参考正确答案,最后当计算时间结束或者试题计算完毕后,给出做题情况的相关统计

 

二 代码实现

1 form1代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace shuxuefudao
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string path = "E:
tf";
        public static int Count = 0; // 题目出的数量
        public static int zuode = 0; //做的题目数量
        public static int zhengque = 0;
        public static int lefttime;
        public static int time;
        public static int sum;
        int i = 0;
        private void open_Click(object sender, EventArgs e) //打开文件的方法
        {
            OpenFileDialog TxTOpenDialog = new OpenFileDialog();
            TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
            if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
            {
                path = TxTOpenDialog.FileName;
                this.richTextBox1.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                save.Enabled = false;
                open.Enabled = false;
                MessageBox.Show("读取成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void save_Click(object sender, EventArgs e) //保存文件的方法
        {
            SaveFileDialog TxtSaveDialog = new SaveFileDialog();
            TxtSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
            if (File.Exists(path))
            {

                this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.richTextBox1.Clear();
                save.Enabled = false;
            }
            else
            {
                if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
                {

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

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            save.Enabled = true;
            if (this.richTextBox1.Text == "" || this.richTextBox1.Text == null)
            {
                open.Enabled = true;
            }
        }
        private void open2_Click(object sender, EventArgs e) //打开试题的方法
        {
            OpenFileDialog TxTOpenDialog = new OpenFileDialog();
            TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
            if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
            {
                path = TxTOpenDialog.FileName;
                this.richTextBox2.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                save.Enabled = false;
                open.Enabled = false;
                MessageBox.Show("打开成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void daoru_Click(object sender, EventArgs e) //导入试题的方法
        {
            richTextBox2.Text = richTextBox1.Text;
        }

        private void daan1_Click(object sender, EventArgs e)
        {
            if (daan1.Text == "显示答案")
            {
                daan.PasswordChar = Convert.ToChar(0);
                daan1.Text = "隐藏答案";
            }
            else if (daan1.Text == "隐藏答案")
            {
                daan.PasswordChar = '.';
                daan1.Text = "显示答案";

            }
        }

        private void kaishi_Click(object sender, EventArgs e)
        {
            string[] left = new string[100];
            left = File.ReadAllLines("writer.txt");
            textBox1.Text = left[0];
            string[] zhong = new string[100];
            zhong = File.ReadAllLines("writer1.txt");
            textBox3.Text = zhong[0];
            string[] right = new string[100];
            right = File.ReadAllLines("writer2.txt");
            textBox6.Text = right[0];
            string[] fuhao1 = new string[100];
            fuhao1 = File.ReadAllLines("writer3.txt");
            textBox2.Text = fuhao1[0];
            string[] fuhao2 = new string[100];
            fuhao2 = File.ReadAllLines("writer4.txt");
            textBox5.Text = fuhao2[0];
            int minute;
            try
            {
                minute = int.Parse(this.shijian.Text);
            }
            catch (System.Exception ex)
            {
                this.shijian1.Text = "输入错误";
                return;
            }
            lefttime = minute;
            this.timer1.Interval = 1000;
            this.timer1.Enabled = true;
            this.timer1.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            time = Convert.ToInt32(shijian.Text);
            if (lefttime <= 0)
            {
                timer1.Enabled = false;
                MessageBox.Show("答题时间到!");
                textBox4.Enabled = false;
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
            this.shijian1.Text = "剩余时间" + lefttime.ToString() + "";
            lefttime--;  
        }

        private void jieshu_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e) //请编辑下道题的事件
        {
            Count++;
            ti.Text = Count.ToString();
            StreamWriter writer = File.AppendText("writer.txt");
            writer.WriteLine(left.Text);
            writer.Close();
            StreamWriter writer1 = File.AppendText("writer1.txt");
            writer1.WriteLine(zhong.Text);
            writer1.Close();
            StreamWriter writer2 = File.AppendText("writer2.txt");
            writer2.WriteLine(right.Text);
            writer2.Close();
            StreamWriter writer3 = File.AppendText("writer3.txt");
            writer3.WriteLine(fuhao.Text);
            writer3.Close();
            StreamWriter writer4 = File.AppendText("writer4.txt");
            writer4.WriteLine(fuhao1.Text);
            writer4.Close();
            richTextBox1.Text += left.Text + fuhao.Text +zhong.Text+fuhao1.Text+ right.Text + label2.Text + "" + "
";
            left.Clear();
            fuhao.Clear();
            right.Clear();
            zhong.Clear();
            fuhao1.Clear();
        }
        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                qita aaa = new qita();  //实例化qita类
                aaa.a = (int.Parse(textBox1.Text)); //为qita类里面的相关变量进行赋值
                aaa.b = (int.Parse(textBox3.Text));
                aaa.c = (int.Parse(textBox6.Text));
                qita.fh1 = textBox2.Text;
                qita.fh2 = textBox5.Text;
                aaa.jisuan(); //调用qita类里面的计算方法
                int result;
                if (e.KeyCode == Keys.Enter)
                {
                    if (int.TryParse(textBox4.Text, out result) == false)
                    {
                        MessageBox.Show("请输入数字");
                    }
                    daan.Text += sum + "
";
                    if (textBox4.Text == aaa.sum.ToString())
                    {
                        MessageBox.Show("回答正确");
                        zuode++;
                        zhengque++;
                    }
                    else
                    {
                        MessageBox.Show("回答错误");
                        zuode++;
                    }
                    i++;
                    textBox4.Clear();
                    string[] left = new string[100];
                    left = File.ReadAllLines("writer.txt");
                    textBox1.Text = left[i];
                    string[] zhong = new string[100];
                    zhong = File.ReadAllLines("writer1.txt");
                    textBox3.Text = zhong[i];
                    string[] right = new string[100];
                    right = File.ReadAllLines("writer2.txt");
                    textBox6.Text = right[i];
                    string[] fuhao1 = new string[100];
                    fuhao1 = File.ReadAllLines("writer3.txt");
                    textBox2.Text = fuhao1[i];
                    string[] fuhao2 = new string[100];
                    fuhao2 = File.ReadAllLines("writer4.txt");
                    textBox5.Text = fuhao2[i];
                }
            }
            catch(Exception ex)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
        }
        private void button2_Click(object sender, EventArgs e) //清空上次编辑试题的方法
        {
            qita aaa = new qita();
            aaa.qingkong();
        }
        private void button3_Click(object sender, EventArgs e) //退出程序的方法
        {
            Application.Exit();
        }
    }
}

form2 代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox6.Text = Form1.zuode.ToString();
            textBox1.Text = Form1.zhengque.ToString();
            textBox2.Text = ((Form1.zuode - (double)(Form1.zhengque))).ToString();
            textBox3.Text = ((Form1.zhengque / (double)(Form1.zuode)) * 100).ToString("f2") + "%";
        }

        
    }
}

qita类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace shuxuefudao
{
    class qita
    {
       public int a, b, c; //计算方法的变量的相关声明
       public  int sum;
       public static string fh1;
       public static string fh2;
       public void qingkong() //清空后台相关内容的方法
        {
            File.WriteAllText("writer.txt", string.Empty);
            File.WriteAllText("writer1.txt", string.Empty);
            File.WriteAllText("writer2.txt", string.Empty);
            File.WriteAllText("writer3.txt", string.Empty);
            File.WriteAllText("writer4.txt", string.Empty);
        }
        public void jisuan()  //计算方法的封装
        {
            string m =fh1+fh2 ;
            switch (m)
            {
                case "+-":
                    sum = a + b - c;
                    break;
                case "++":
                    sum = a + b + c;
                    break;
                case "+*":
                    sum = a + b * c;
                    break;
                case "+/":
                    sum = a + b * c;
                    break;
                case "--":
                    sum = a - b - c;
                    break;
                case "-+":
                    sum = a - b + c;
                    break;
                case "-*":
                    sum = a - b * c;
                    break;
                case "-/":
                    sum = a + b / c;
                    break;
                case "*-":
                    sum = a * b - c;
                    break;
                case "*+":
                    sum = a * b + c;
                    break;
                case "**":
                    sum = a * b * c;
                    break;
                case "*/":
                    sum = a * b / c;
                    break;
                case "/-":
                    sum = a / b - c;
                    break;
                case "/+":
                    sum = a / b + c;
                    break;
                case "/*":
                    sum = a / b * c;
                    break;
                case "//":
                    sum = a / b / c;
                    break;
                default:
                    break;
            }
        }
        
    }
}

三 代码封装

在from1代码中的引用封装好的代码

 private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                qita aaa = new qita();  //实例化qita类
                aaa.a = (int.Parse(textBox1.Text)); //为qita类里面的相关变量进行赋值
                aaa.b = (int.Parse(textBox3.Text));
                aaa.c = (int.Parse(textBox6.Text));
                qita.fh1 = textBox2.Text;
                qita.fh2 = textBox5.Text;
                aaa.jisuan(); //调用qita类里面的计算方法
                int result;
                if (e.KeyCode == Keys.Enter)
                {
                    if (int.TryParse(textBox4.Text, out result) == false)
                    {
                        MessageBox.Show("请输入数字");
                    }
                    daan.Text += sum + "
";
                    if (textBox4.Text == aaa.sum.ToString())
                    {
                        MessageBox.Show("回答正确");
                        zuode++;
                        zhengque++;
                    }
                    else
                    {
                        MessageBox.Show("回答错误");
                        zuode++;
                    }
                    i++;
                    textBox4.Clear();
                    string[] left = new string[100];
                    left = File.ReadAllLines("writer.txt");
                    textBox1.Text = left[i];
                    string[] zhong = new string[100];
                    zhong = File.ReadAllLines("writer1.txt");
                    textBox3.Text = zhong[i];
                    string[] right = new string[100];
                    right = File.ReadAllLines("writer2.txt");
                    textBox6.Text = right[i];
                    string[] fuhao1 = new string[100];
                    fuhao1 = File.ReadAllLines("writer3.txt");
                    textBox2.Text = fuhao1[i];
                    string[] fuhao2 = new string[100];
                    fuhao2 = File.ReadAllLines("writer4.txt");
                    textBox5.Text = fuhao2[i];
                }
            }
            catch(Exception ex)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
        }

设置一个类,名为qita,在类里面写一个计算类的方法

  public void jisuan()  //计算方法的封装
        {
            string m =fh1+fh2 ;
            switch (m)
            {
                case "+-":
                    sum = a + b - c;
                    break;
                case "++":
                    sum = a + b + c;
                    break;
                case "+*":
                    sum = a + b * c;
                    break;
                case "+/":
                    sum = a + b * c;
                    break;
                case "--":
                    sum = a - b - c;
                    break;
                case "-+":
                    sum = a - b + c;
                    break;
                case "-*":
                    sum = a - b * c;
                    break;
                case "-/":
                    sum = a + b / c;
                    break;
                case "*-":
                    sum = a * b - c;
                    break;
                case "*+":
                    sum = a * b + c;
                    break;
                case "**":
                    sum = a * b * c;
                    break;
                case "*/":
                    sum = a * b / c;
                    break;
                case "/-":
                    sum = a / b - c;
                    break;
                case "/+":
                    sum = a / b + c;
                    break;
                case "/*":
                    sum = a / b * c;
                    break;
                case "//":
                    sum = a / b / c;
                    break;
                default:
                    break;
            }
        }
        

然后在form1中实例化一个类,然后引用该方法

原文地址:https://www.cnblogs.com/wyh19941210/p/4985785.html