zoukankan      html  css  js  c++  java
  • Homework_4 四则运算

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

    结对同伴: 姓名:胡仕辉   学号:130201225   博客地址:http://www.cnblogs.com/hushihui666/

    实现功能:

      1) 题目的数量(个人项目的要求)

      2) 数值的范围(个人项目的要求)

      3) 题目中最多几个运算符

      4) 题目中或运算过程中有无有分数(比如进行整数除法的时候不能除尽)

      5) 题目中是否有乘除法

      6) 题目中是否有括号

     我负责程序构建的算法设计,胡仕辉同学负责窗体和代码生成

    主要在之前完成的作业算法上进行改进,附上博客地址:http://www.cnblogs.com/SaltWu/p/5281184.html

    合作优点在于可以互相讨论,每个人的想法不一样,有时候卡在一个问题上,同伴一个意见就能帮我拐弯。

    合作时的照片:

          

    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("
    ");
                    input("
    ");
                }
            }
            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();
            }
        }
    }

    程序执行截图:

  • 相关阅读:
    hdu1828(线段树——矩形周长并)
    hdu1255(线段树——矩形面积交)
    用jQuery获取到一个类名获取到的是一个数组 ,如果对数组中的每个进行相应的操作可以这样进行
    CSS3向外扩散的圆
    鼠标放上去图片会放大
    Django分页
    Django使用富文本编辑器
    Django日志配置
    Linux中的文件类型
    Linux压缩和解压缩
  • 原文地址:https://www.cnblogs.com/SaltWu/p/5361650.html
Copyright © 2011-2022 走看看