储存出题改进

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;

namespace Calculate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)//写入题
        {
            string i = @"one";
            string ii = @"tow";
            string iii = @"three";
            string f = @"onefu";
            string f1 =@"towfu";
            Writ1 writ = new Writ1(i,ii,iii,f,f1);
            writ.X = textBox1.Text;
            writ.Y = textBox2.Text;
            writ.Z = textBox3.Text;
            writ.FF1 = comboBox1.Text;
            writ.FF2 = comboBox2.Text;
            writ.xie();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
          
        }

        private void button3_Click(object sender, EventArgs e)//清空题库
        {
            string i = @"one";
            string ii = @"tow";
            string iii = @"three";
            string f = @"onefu";
            string f1 = @"towfu";
            Writ1 writ = new Writ1(i, ii, iii, f, f1);
            writ.X = textBox1.Text;
            writ.Y = textBox2.Text;
            writ.Z = textBox3.Text;
            writ.FF1 = comboBox1.Text;
            writ.FF2 = comboBox2.Text;
            writ.qingkong();
        }

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

 计算类

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

namespace Calculate
{
    class Calculate1
    {
        private int a;
        private int b;
        private int c;

        private int rightanswer;
   

       private string operator1;
       private string operator2;
        public int A
        {
            set
            {
                    a = value;  
            }
        }
        public int B
        {
            set
            {             
                    b = value;          
            }
        }
        public int C
        {
            set
            {
                c = value;
            }
        }
        public int RightAnswer
        {
            get
            {
                return rightanswer;
            }
        }
    
        public string Operator1
        {
            set
            {
                operator1 = value;
            }
        }
        public string Operator2
        {
            set
            {
                operator2 = value;
            }
        }
        public int correct()
        {
            string operat = operator1 + operator2;
            switch (operat)
            {
                case "++":
                    rightanswer=a+b+c;
                    break;
                case "+-":
                    rightanswer=a+b-c;
                    break;
                case "+*":
                    rightanswer = a + b * c;
                    break;
                case "+/":
                    rightanswer = a + b / c;
                    break;
                case "-+":
                    rightanswer = a - b + c;
                    break;
                case "--":
                    rightanswer = a - b - c;
                    break;
                case "-*":
                    rightanswer = a - b * c;
                    break;
                case "-/":
                    rightanswer = a - b / c;
                    break;
                case "*+":
                    rightanswer = a * b + c;
                    break;
                case "*-":
                    rightanswer = a * b - c;
                    break;

                case "**":
                    rightanswer = a * b * c;
                    break;
                case "*/":
                    rightanswer = a * b / c;
                    break;
                case "/+":
                    rightanswer = a / b + c;
                    break;
                case "/-":
                    rightanswer = a / b - c;
                    break;
                case"/*":
                    rightanswer = a / b - c;
                    break;
                case "//":
                    rightanswer = a / b / c;
                    break;

            }
            return rightanswer;
        }
    }
}

 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;
using System.IO;

namespace Calculate
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private int i = 1;
        Calculate1 ca = new Calculate1();
        private void Form2_Load(object sender, EventArgs e)
        {
            butt();
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ca.A = int.Parse(textBox1.Text);
                ca.B = int.Parse(textBox2.Text);
                ca.C = int.Parse(textBox3.Text);
                ca.Operator1 = label1.Text;
                ca.Operator2 = label2.Text;

                ca.correct();
                
                if (textBox4.Text == ca.RightAnswer.ToString())
                {
                    MessageBox.Show("回答正取!");

                }
                else
                {
                    MessageBox.Show("回答错误!");
                }
                textBox4.Clear();
                butt();
 
            }
        }
        private void butt()
        {
            string[] line = File.ReadAllLines("one");
            if (i < line.Length)
            {
                textBox1.Text = line[i];
                string[] lines = File.ReadAllLines("tow");
                textBox2.Text = lines[i];
                string[] lin = File.ReadAllLines("three");
                textBox3.Text = lin[i];
                string[] linf = File.ReadAllLines("onefu");
                label1.Text = linf[i];
               string[] linf1 = File.ReadAllLines("towfu");
               label2.Text = linf1[i];

            }
            i++;
          


        }

      
    }
}

 写入类

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

namespace Calculate
{
    class Writ1
    {
       
       
        private string a;
        private string c;
        private string d;
        private string f1;
        private string f2;
        
        private string x;
        private string y;
        private string z;
        private string ff1;
        private string ff2;
        
     
        public Writ1(string A, string C, string D, string F1, string F2)
        {
            a = A;
            c = C;
            d = D;
            f1 = F1;
            f2 = F2;
        } 
        public string X
        {
            set
            {
                x = value;
            }
        }
        public string Y
        {
            set
            {
                y = value;
            }
        }
        public string Z
        {
            set
            {
                z = value;
            }
        }
        public string FF1
        {
            set
            {
                ff1 = value;
            }
        }
        public string FF2
        {
            set
            {
                ff2 = value;
            }
        }

        public void xie()
        {
            StreamWriter di=new StreamWriter(a,true);
            di.WriteLine(x);
            di.Close();
            StreamWriter er = new StreamWriter(c, true);
            er.WriteLine(y);
            er.Close();
            StreamWriter san = new StreamWriter(d, true);
            san.WriteLine(z);
            san.Close();
            StreamWriter fi = new StreamWriter(f1, true);
            fi.WriteLine(ff1);
            fi.Close();
            StreamWriter fii = new StreamWriter(f2, true);
            fii.WriteLine(ff2);
            fii.Close();
           
        }
        public void qingkong()
        {

            StreamWriter ddd = new StreamWriter(a);
            ddd.WriteLine(" ");
            ddd.Close();  
            StreamWriter aaa = new StreamWriter(c);
            aaa.WriteLine("");
            aaa.Close();
            StreamWriter fff = new StreamWriter(d);
            fff.WriteLine("");
            fff.Close();
            StreamWriter fff1 = new StreamWriter(f1);
            fff1.WriteLine("");
            fff1.Close();
            StreamWriter fff2 = new StreamWriter(f2);
            fff2.WriteLine("");
            fff2.Close();
        }
     
         


    }
}
原文地址:https://www.cnblogs.com/lizanqirxx/p/4984208.html