老陈,小石头


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 小石头和老陈
{
     
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }
        public static int Count = 0;//题目总数
        public static int right = 0;//正确的题目总数
        public static int connt = 0;//错误题目数
        public static int time;
        public static int tim;
        public static string  ty = "";
        public static int re=0;
        public int t = 60;
        int j = 0;
        string  a;
        string  b;
        int  c;
        private void label3_Click(object sender, EventArgs e)
        {
         
                tb4.Text = "+";
        }

        private void label4_Click(object sender, EventArgs e)
        {
           
                tb4.Text = "-";
          
        }

        private void label5_Click(object sender, EventArgs e)
        {
           
            
                tb4.Text = "*";
            
        }
        private void label6_Click(object sender, EventArgs e)
        {
           
                tb4.Text = "/";
           
        }

        private void button3_Click(object sender, EventArgs e)
        {

            En en=null;
           
            a = textBox6.Text;
            b = textBox8.Text;
            string str = textBox7.Text;
            switch (str)
            {
                case "+":
                    en = new En(new Add()); //策略模式的引用
                  
                    break;
                case "-":
                    en = new En(new Sub());

                    break;
                case "*":
                    en = new En(new Mul());

                    break;
                case "/":
                    en = new En(new Div());

                    break;
                default:
                    break;
            }
            
            if (textBox8.Text == "" || textBox6.Text == "")//当没有导入题时不能记到总做的题数;
            {
                MessageBox.Show("不能为空");
                Count--;

            }
            else if (textBox3.Text == c.ToString())
            {
                right++;
                MessageBox.Show("回答正确!");
            }
            else if (textBox3.Text != c.ToString())
            {

                connt++;
                MessageBox.Show("回答错误!");
            }

            textBox6.Clear();
            textBox7.Clear();
            textBox8.Clear();
            textBox3.Clear();
            textBox1.Text = Count.ToString();
            
        }
           
        private void button2_Click(object sender, EventArgs e)
        {
               Form2 form = new Form2();
                form.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //保存家长出的数学题
            StreamWriter wr = File.AppendText("g1.txt");
            wr.WriteLine(tb1.Text );
            wr.Close();
            StreamWriter wr1 = File.AppendText("g2.txt");
            wr1.WriteLine(tb2.Text);
            wr1.Close();
            StreamWriter wr2 = File.AppendText("g3.txt");
            wr2.WriteLine(tb4.Text);
            wr2.Close();
            tb1.Clear();
            tb2.Clear();
            tb4.Clear();
          
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try//防止出现异常,当题做完直接跳转到Form2窗体,,,,
            {
                j++;
                string[] jg = new string[100];
                jg = File.ReadAllLines("g1.txt");
                textBox6.Text = jg[j];
                string[] jg2 = new string[100];
                jg2 = File.ReadAllLines("g3.txt");
                textBox7.Text = jg2[j];
                string[] jg3 = new string[100];
                jg3 = File.ReadAllLines("g2.txt");
                textBox8.Text = jg3[j];
                string[] jg4 = new string[100];
                jg4 = File.ReadAllLines("g4.txt");
                if (jg2[j] =="/"&& jg3 [j]=="0")
                {
                    MessageBox.Show("分母不能为零");//判断分母是否为零,
                    j++;
                    textBox6.Clear();
                    textBox7.Clear();
                    textBox8.Clear();
                }
            }
           
            catch
            {
                Form2 form = new Form2();
                form.ShowDialog();
            }
            if( textBox2 .Text =="")
            {
                MessageBox.Show("请输入时间");
            }
            int minse = int.Parse(textBox2.Text);
            time = minse;
            this.timer1.Interval = 1000;
            this.timer1.Enabled = true;
            this.timer1.Start();
            time--;
            Count++;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.ShowDialog();
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            
        }

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

 
       
    







定义一个Class1类


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

namespace 小石头和老陈
{
    public interface Calculator 
    {
        double Cl(double a, double b);
    }
    public class Add : Calculator
    {
        public double Cl(double a, double b)//加法运算
        {
            double result = 0;
            result = a + b;
            return result;
        }
    }
    public class Sub : Calculator //减法运算
    {
        public double Cl(double a, double b)
        {
            double result = 0;
            result = a - b;
            return result;
        }
    }
    public class Mul : Calculator//乘法运算
    {
        public double Cl(double a, double b)
        {
            double result = 0;
            result = a * b;
            return result;
        }
    }
    public class Div : Calculator //除法运算
    {
        public double Cl(double a, double b)
        {
            double result = 0;
            result = a / b;
            return result;
        }
    }
    public class En       //定义那个需要动态改变算法的对象   
    {
        private Calculator cal;
        public En(Calculator calculate)
        {
            this.cal = calculate;
        }
        public double Cal(double a, double b, String m) //返回运算结果
        {
            return this.cal.Cl(a, b);
        }
    }
}









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 小石头和老陈
{
    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();//正确题目数目
            textBox3.Text = Form1.connt.ToString();
            textBox4.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";//题目正确率
        } 
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}



PSP消耗








原文地址:https://www.cnblogs.com/mengq/p/5008186.html