天天看點

新四則運算 合作完成

題目要求 :http://www.cnblogs.com/gdfhp/p/5311937.html

結對同伴: 姓名:吳阿平  學号:130201234   部落格位址:http://www.cnblogs.com/SaltWu/p/5361650.html

實作功能:

  1) 題目的數量(個人項目的要求)

  2) 數值的範圍(個人項目的要求)

  3) 題目中最多幾個運算符

  4) 題目中或運算過程中有無有分數(比如進行整數除法的時候不能除盡)

  5) 題目中是否有乘除法

  6) 題目中是否有括号

 吳阿平同學負責程式建構的算法設計,我負責窗體和代碼生成

合作優點在于可以互相讨論,互相學習,有問題可以及時發現,一起改正。

合作時的照片:

      

新四則運算 合作完成
新四則運算 合作完成

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


namespace 新四則運算
{
    public partial class 新四則運算 : Form
    {
        
        public 新四則運算()
        {
            InitializeComponent();
        }
        char[] ysf = { '+', '-', '*', '%'};
        static int GetRandomSeed()  //随機數種子,解決随機數一緻問題
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
        private void btn_1_Click(object sender, EventArgs e)
        {
            int n = Convert.ToInt32(this.tbox_n.Text);  //擷取生成題目數
            int r = Convert.ToInt32(this.tbox_r.Text);  //擷取生成數範圍
            int ysf = Convert.ToInt32(this.cbox_ysf.Text.ToString());  //擷取運算符個數
            for(int i = 0; i < n; i++) //生成n道題
            {
                if (ysf == 1)  //一個運算符
                {
                    ysf1(r); 
                }
                else if (ysf == 2)  // 兩個運算符
                {
                    ysf2(r);
                }
                else    //三個運算符
                {
                    ysf3(r);
                }
                input(" = ");
                input("\n");
                input("\n");
            }
        }
        public void ysf1(int r)   //一個運算符
        {
            if(cbox_fs.SelectedItem.ToString() == "是")   //包含分數
            {
                Random rd = new Random(GetRandomSeed());
                randnum1(r);  //插入随機數,可以是分數
                input_ysf();  //插入運算符
                randnum1(r);
            }
            else
            {
                Random rd = new Random(GetRandomSeed());
                randnum(r); //插入整數随機數
                input_ysf();
                randnum(r);
            }
            
        }
        public void ysf2(int r)
        {
            Random rd = new Random(GetRandomSeed());
            int t = 0;
            t = rd.Next(2); //随機運算符數量,1或2,對零時變量t随機,結果為0,跳至ysf1,結果為1則為ysf2
            if (t == 0)
            {
                ysf1(r);
            }
            else
            {
                
                if(cbox_kh.SelectedItem.ToString() == "是")  //包含括号
                {
                    input_ysf2_kh(r);  
                }
                else
                {
                    input_ysf2(r); 
                }
                    
               
            }
        }
        public void ysf3(int r)
        {
            Random rd = new Random(GetRandomSeed());
            int t = 0;
            t = rd.Next(2); //随機運算符數量,對零時變量t随機,結果為0,跳至ysf2,結果為1則為ysf3
            if (t == 0)
            {
                ysf2(r);
            }
            else
            {
                if (cbox_kh.SelectedItem.ToString() == "是")  //包含括号
                {
                    int m = 0;
                    m = rd.Next(3); //對加括号的形式進行随機
                    if(m == 0)
                    {
                        input_ysf3_kh1(r);  //括号形式1
                    }
                    else if(m == 1)
                    {
                        input_ysf3_kh2(r);   //括号形式2
                    }
                    else
                    {
                        input_ysf3_kh3(r);  //括号形式3
                    }                       
                }
                else
                {
                    input_ysf3(r);   //不含括号
                }
            }

        }
        public void input_ysf()
        {
            input(" "); //運算符前後空格
            Random rd = new Random(GetRandomSeed());
            if (cbox_ccf.SelectedItem.ToString() == "是") 
            {
                input(ysf[rd.Next(4)].ToString());//包含乘除法
            }
            else
            {
                input(ysf[rd.Next(2)].ToString());//不含乘除法
            }
            input(" ");
        }
        public void input_ysf2(int r)
        {
            if(cbox_fs.SelectedItem.ToString() == "是")  //判斷是否含分數
            {
                randnum1(r);   
                input_ysf();
                randnum1(r);
                input_ysf();
                randnum1(r);
            }
            else
            {
                randnum(r);  
                input_ysf();
                randnum(r);
                input_ysf();
                randnum(r);
            }
            
        }
        public void input_ysf2_kh(int r)
        {
            if (cbox_fs.SelectedItem.ToString() == "是") //含分數
            {
                input("(");
                randnum1(r);
                input_ysf();
                randnum1(r);
                input(")");
                input_ysf();
                randnum1(r);
            }
            else
            {
                input("(");
                randnum(r);
                input_ysf();
                randnum(r);
                input(")");
                input_ysf();
                randnum(r);
            }
                
        }
        public void input_ysf3(int r)
        {
            if (cbox_fs.SelectedItem.ToString() == "是")  //含分數
            {
                randnum1(r);
                input_ysf();
                randnum1(r);
                input_ysf();
                randnum1(r);
                input_ysf();
                randnum1(r);
            }
            else
            {
                randnum(r);
                input_ysf();
                randnum(r);
                input_ysf();
                randnum(r);
                input_ysf();
                randnum(r);
            }
                
        }
        public void input_ysf3_kh1(int r)  //帶括号形式1
        {
            if (cbox_fs.SelectedItem.ToString() == "是")
            {
                input("[");
                input("(");
                randnum1(r);
                input_ysf();
                randnum1(r);
                input(")");
                input_ysf();
                randnum1(r);
                input("]");
                input_ysf();
                randnum1(r);
            }
            else
            {
                input("[");
                input("(");
                randnum(r);
                input_ysf();
                randnum(r);
                input(")");
                input_ysf();
                randnum(r);
                input("]");
                input_ysf();
                randnum(r);
            }
                

        }
        public void input_ysf3_kh2(int r)  //帶括号形式2
        {
            if (cbox_fs.SelectedItem.ToString() == "是")
            {
                randnum1(r);
                input_ysf();
                input("(");
                randnum1(r);
                input_ysf();
                randnum1(r);
                input(")");
                input_ysf();
                randnum1(r);
            }
            else
            {
                randnum(r);
                input_ysf();
                input("(");
                randnum(r);
                input_ysf();
                randnum(r);
                input(")");
                input_ysf();
                randnum(r);
            }
                
        }
        public void input_ysf3_kh3(int r)  //帶括号形式3
        {
            if (cbox_fs.SelectedItem.ToString() == "是")
            {
                input("(");
                randnum1(r);
                input_ysf();
                randnum1(r);
                input(")");
                input_ysf();
                input("(");
                randnum1(r);
                input_ysf();
                randnum1(r);
                input(")");
            }
            else
            {
                input("(");
                randnum(r);
                input_ysf();
                randnum(r);
                input(")");
                input_ysf();
                input("(");
                randnum(r);
                input_ysf();
                randnum(r);
                input(")");
            }
                
        }
        public void randnum(int r) //對數進行随機,隻能是整數
        {
            Random rd = new Random(GetRandomSeed());
            int num;
            do
            {
                num = rd.Next(r + 1);
            } while (num == 0); // 随機整數不為 0

            input(num.ToString());

        }
        public void randnum1(int r) //對數進行随機,可為分數
        {
            int t = 0;
            Random rd = new Random(GetRandomSeed());
            t = rd.Next(2); //對零時變量t随機,結果為0,這個數為整數,結果為1則為分數
            if (t == 0)
            {
                int num;
                do
                {
                    num = rd.Next(r + 1);
                } while (num == 0); // 随機整數不為 0

                input(num.ToString());

            }
            else
            {
                randnum2(r);
            }
       
        }

        public void randnum2(int r) //分數随機
        {
            Random rd = new Random(GetRandomSeed());
            int x, y; //x為分子,y為分母
            do
            {
                x = rd.Next(r + 1);
            } while (x == 0); //分子不為0
            do
            {
                y = rd.Next(r + 1);
            } while (y == 0 || y == x); //分母不為0,且不等于分子

            if (x > y) //如果分子比分母大,則對調分子分母位置
            {
                int t = x;
                x = y;
                y = t;
            }
            input(x.ToString());
            input("/");
            input(y.ToString());

            
        }

        public void input(string t)
        {
            tbox_shuchu.AppendText(t);
        }

        private void btn_clc_Click(object sender, EventArgs e)
        {
            tbox_shuchu.Clear();
        }
    }
}      
新四則運算 合作完成

程式執行截圖:

新四則運算 合作完成