• 四则运算《《《代码封装


    设计思路:

           因为TabControl可以设置不同页的选项卡。所以我用它来分页,进行出题,答题设置。然后用savefiledialog保存所出题目。设置两个RichTextBox保存所出题目和出好题后做题时显示的题目。用Count计算做题总数,Right计算做正确的数目。点击结束时弹出Form对话框显示做题情况。

    具体实现代码:

    Form1.cs

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.IO;
     10 
     11 namespace sum
     12 {
     13     public partial class Form1 : Form
     14     {
     15         public Form1()
     16         {
     17             InitializeComponent();
     18         }
     19      //   bool bFileName = false;                    //标记文本框内容是否被命名
     20         string fileName;                           //存储当前文件的名字
     21         public static int Count = 0;               //计算所做的题数
     22         public static int Right = 0;               //回答正确的题数
     23         public static double answer;
     24         int i;
     25         public void SaveTofile()                   //保存所出题目的方法
     26         {
     27             sfd.InitialDirectory = "C\";               //设置保存默认目录
     28             sfd.Filter = "txt files(*.txt)|*.txt|all files(*.*)|*.*";
     29             sfd.FilterIndex = 1;                       //默认保存类型为txt
     30             sfd.RestoreDirectory = true;
     31             if (sfd.ShowDialog() == DialogResult.OK)
     32             {
     33                 richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
     34                fileName = sfd.FileName;
     35              //  bFileName = true;
     36             }
     37         }
     38        
     39         private void button1_Click(object sender, EventArgs e)  //把所出题目保存在txt文档中
     40         {
     41             StreamWriter stw1 = File.AppendText("number1.txt");
     42             stw1.WriteLine(textBox1.Text);
     43             stw1.Close();
     44             StreamWriter stw2 = File.AppendText("number2.txt");
     45             stw2.WriteLine(textBox2.Text);
     46             stw2.Close();
     47             StreamWriter stw3 = File.AppendText("number3.txt");
     48             stw3.WriteLine(textBox3.Text);
     49             stw3.Close();
     50             richTextBox1.Text += textBox1.Text + textBox2.Text + textBox3.Text + textBox4.Text + "" + "
    ";
     51             textBox1.Clear();
     52             textBox2.Clear();
     53             textBox3.Clear();
     54         }
     55 
     56         private void save_Click(object sender, EventArgs e) //保存
     57         {
     58             SaveTofile();
     59             MessageBox.Show("保存成功!");
     60            
     61         }
     62 
     63         private void button2_Click(object sender, EventArgs e)  //打开
     64         {
     65 
     66             richTextBox2.Text = richTextBox1.Text;
     67         }
     68 
     69         private void write_Click(object sender, EventArgs e)
     70         {
     71             string[] one = new string[100];
     72             one = File.ReadAllLines("number1.txt");
     73             textBox5.Text = one[0];
     74             string[] two = new string[100];
     75            two = File.ReadAllLines("number2.txt");
     76             textBox6.Text =two[0];
     77             string[] three = new string[100];
     78             three = File.ReadAllLines("number3.txt");
     79             textBox7.Text = three[0];
     80            
     81         }
     82 
     83         private void textBox8_KeyDown(object sender, KeyEventArgs e)
     84         {
     85             Class1 cs = new Class1();
     86             cs.X = double.Parse(textBox5.Text);
     87             cs.Y = double.Parse(textBox7.Text);
     88             cs.result = answer;
     89             string opera = textBox6.Text;
     90             cs.opera = opera;
     91             cs.Add();                         //调用Class1的Add方法;
     92             cs.Min();                        //调用Class1的Min方法;
     93             cs.Mul();                        //调用Class1的Mul方法;             
     94             cs.Div();                        //调用Class1的Div方法;
     95             if (e.KeyCode == Keys.Enter)
     96             {
     97                 if (textBox8.Text == cs.result.ToString())
     98                 {
     99                     MessageBox.Show("回答正确");
    100                     Count++;
    101                     Right++;
    102                 }
    103                 else
    104                 {
    105                     MessageBox.Show("回答错误");
    106                     Count++;
    107                 }
    108                 i++;
    109                 textBox8.Clear();
    110                 string[] one = new string[100];
    111                 one = File.ReadAllLines("number1.txt");
    112                 textBox5.Text = one[i];
    113                 string[] two = new string[100];
    114                 two = File.ReadAllLines("number2.txt");
    115                 textBox6.Text = two[i];
    116                 string[] three = new string[100];
    117                 three = File.ReadAllLines("number3.txt");
    118                 textBox7.Text = three[i];
    119                 if (one.Count() == i)
    120                 {
    121                     Form2 frm = new Form2();
    122                     frm.ShowDialog();
    123                 }
    124             }
    125         }
    126         
    127 
    128         private void button4_Click(object sender, EventArgs e)
    129         {
    130             Form2 frm2 = new Form2();
    131             frm2.ShowDialog();
    132         }
    133 
    134         private void button3_Click(object sender, EventArgs e)   //退出程序
    135         {
    136             Application.Exit();
    137         }
    138 
    139             }
    140         }
    141 
    142 
    143       

    Form2.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace sum
    11 {
    12     public partial class Form2 : Form
    13     {
    14         public Form2()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         private void Form2_Load(object sender, EventArgs e)
    20         {
    21              textBox1.Text = Form1.Count.ToString();
    22             textBox2.Text = Form1.Right.ToString();
    23             textBox3.Text = ((Form1.Right / (double)(Form1.Count)) * 100).ToString() + "%";
    24         }
    25 
    26     }
    27 }

    代码封装:Class1.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace sum
     7 {
     8     class Class1
     9     {
    10         private double x;   //定义x变量;
    11        
    12         public double X  //封装字段
    13         {
    14             get { return x; } 
    15             set { x = value; }
    16 
    17         }
    18         private double y;  //定义y变量
    19 
    20 
    21         public double Y        //封装字段
    22         {
    23             get { return y; }
    24             set { y = value; }
    25 
    26         }
    27         public string opera = "";  //运算符号
    28         public double result;      //计算结果
    29         public void Add()          //加法
    30         {
    31             if (opera == "+")
    32             {
    33                 result = X + Y;
    34             }
    35         }
    36         public void Min()              //减法
    37         {
    38             if (opera == "-")
    39             {
    40                 result = X - Y;
    41             }
    42         }
    43         public void Mul()             //乘法
    44         {
    45             if (opera == "*")
    46             {
    47                 result = X * Y;
    48             }
    49         }
    50         public void Div()          //除法
    51         {
    52             if (opera == "/")
    53             {
    54                 result = X / Y;
    55             }
    56         }
    57        
    58     }
    59 }

    Form1.cs调用封装代码:

     1  private void textBox8_KeyDown(object sender, KeyEventArgs e)
     2         {
     3             Class1 cs = new Class1();
     4             cs.X = double.Parse(textBox5.Text);
     5             cs.Y = double.Parse(textBox7.Text);
     6             cs.result = answer;
     7             string opera = textBox6.Text;
     8             cs.opera = opera;
     9             cs.Add();                         //调用Class1的Add方法;
    10             cs.Min();                        //调用Class1的Min方法;
    11             cs.Mul();                        //调用Class1的Mul方法;             
    12             cs.Div();                        //调用Class1的Div方法;
    13             if (e.KeyCode == Keys.Enter)
    14             {
    15                 if (textBox8.Text == cs.result.ToString())
    16                 {
    17                     MessageBox.Show("回答正确");
    18                     Count++;
    19                     Right++;
    20                 }
    21                 else
    22                 {
    23                     MessageBox.Show("回答错误");
    24                     Count++;
    25                 }
    26                 i++;
    27                 textBox8.Clear();
    28                 string[] one = new string[100];
    29                 one = File.ReadAllLines("number1.txt");
    30                 textBox5.Text = one[i];
    31                 string[] two = new string[100];
    32                 two = File.ReadAllLines("number2.txt");
    33                 textBox6.Text = two[i];
    34                 string[] three = new string[100];
    35                 three = File.ReadAllLines("number3.txt");
    36                 textBox7.Text = three[i];
    37                 if (one.Count() == i)
    38                 {
    39                     Form2 frm = new Form2();
    40                     frm.ShowDialog();
    41                 }
    42             }
    43         }
    44         

    运行过程:

    保存所出题目

    保存成功

    运算

    结束运算

    总结:

           这次做题虽然遇到很多困难,欣慰的是我还是坚持把它做完了。经过这次编程深感自己平时练习太少,知识面不广,所以我以后要多加练习,多多敲代码。

  • 相关阅读:
    笔记35 跨重定向请求传递数
    判断邮箱的正则表达式
    按钮
    async await 的用法
    笔记34 Spring MVC的高级技术——处理multipart形式的数据
    Convert Sorted Array to Binary Search Tree
    Binary Tree Zigzag Level Order Traversal
    Unique Binary Search Trees,Unique Binary Search Trees II
    Validate Binary Search Tree
    Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
  • 原文地址:https://www.cnblogs.com/thinking-star/p/5002783.html
Copyright © 2020-2023  润新知