zoukankan      html  css  js  c++  java
  • 四则运算出题2

    1、题目要求:

          完成一个程序,自动出四则运算题,范围可以自定,包括加减乘除(有无乘除可以选择),有无括号,有无真分数、负数参与运算,两数除法有无余数,打印方式等均可自主选择。

    2、实现思路:

         程序自动出题,运算数和运算符号都是随机,有无括号,有无真分数、负数参与运算,两数除法有无余数,打印方式等均自主选择。。

          (1)先自动出一道题,重复过程所需次数即可;

          (2)出一道题,要分为很多部分:第一个数、运算符号、第二个数、运算符号、第三个数……最少两个数参与运算,根据需要循环产生  符号和数字,知道数字个数满足要求;

          (3)两个数可以直接用随机数产生,四个运算符可以用产生的随机数(1~4)来一一对应,用随机产生的1、2来决定有无乘除,若无乘除,只产生1~2决定加减,若有乘除,产生1~4决定加减乘除;

          (4)用随机产生的1、2来决定两个数的除法有无余数,若有余数,其他可任意出题,若无余数,出现的数值不符合要求,则重新出题,直至满足要求;

          (5)用随机产生的1、2 来决定产生的数是真分数或是整数,如果是真分数,可以用随机产生的两个整数来组成一个分数,将较小的数作为分子即可成为真分数;

          (6)用随机产生的1、2来决定是否有负数,若有负数,需要在输出数值之前输出一个负号,为了区别运算符和负号,需要将整个负数用括号括起来;

          (7)用随机产生的1、2来决定打印方式,即一行有几道题;

          (8)用随机产生的1、2来决定是否有括号,若有括号,随机数来决定左右括号位置;

    3、思路整理(实现步骤):

         (1)用随机产生的1、2 来决定第一个数是真分数或是整数,随机产生一个整数或是两个整数组成真分数作为第一个运算数,用随机产生的1、2 来决定第一个数是真分数或是整数,如果可以有负数,用随机数决定产生的数是否为负数,若为负数,将产生的数前加个负号,并用括号括起来,一个数产生

         (2)用产生的随机数1、2用产生的随机数(1~2或1~4)来决定产生的运算符号,分别对应(+-×÷)

         (3)同(1),产生第二个数

         (4)重复(2)(3),产生所需数量的数

         (5)随机产生1、2决定有无括号,再随机产生一个数,决定左右括号的位置

         (6)若除法无余数,重新出第二个数,直到满足为止

    4、源代码:

      1 //四则运算出题软件,可以控制有无余数,负数,乘除,括号,真分数,数的范围,打印方式等
      2 //王永维,2016、3、12
      3 
      4 using System;
      5 using System.Collections.Generic;
      6 using System.ComponentModel;
      7 using System.Data;
      8 using System.Drawing;
      9 using System.Linq;
     10 using System.Text;
     11 using System.Windows.Forms;
     12 using System.IO;
     13 
     14 namespace 出题程序
     15 {
     16     public partial class Form1 : Form
     17     {
     18         int youwuchengchu=2;//有无乘除,1 有,2 无
     19         int youwuyushu=1;//有无余数,1 有,2 无
     20         int youwufushu=2;//有无负数,1 有,2 无
     21         int youwukuohao=2;//有无括号,1 有,2 无
     22         int youwuzhenfenshu=2;//有无真分数,1 有,2 无
     23         int min=1, max=100;//数值范围
     24         int countti=10,countshu=2;//题数,数值数,初始为10,2
     25         int count = 1;//数数值个数
     26         int shu1, shu2;
     27         int temp;//用于交换shu1,shu2
     28         int yushu;//用于存储余数
     29         int fuhao;
     30         int dayin=1;//设置打印方式
     31         int randshu;//数值类型随机
     32         int randfushu;//正负符号随机
     33         int kuohaolocation;//左括号的随机位置
     34 
     35         Random A = new Random();//随机数产生
     36         void zhengshu()//产生随机数
     37         {
     38             if (youwuzhenfenshu == 1)// 含有真分数
     39             {
     40                 randshu = A.Next(1, 3);
     41                 if (randshu == 1)
     42                 {
     43                     shu1 = A.Next(min, max);
     44                     shu2 = A.Next(min, max);
     45                     if (shu1 < shu2)
     46                         richTextBox1.Text = richTextBox1.Text + shu1 + "/" + shu2;
     47                     else
     48                         richTextBox1.Text = richTextBox1.Text + shu2 + "/" + shu1;
     49                 }
     50                 else
     51                 {
     52                     shu1 = A.Next(min, max);
     53                     richTextBox1.Text = richTextBox1.Text + shu1;
     54                 }
     55             }
     56             if (youwuzhenfenshu == 2)// 全为整数
     57             {
     58                 shu1 = A.Next(min, max);
     59                 richTextBox1.Text = richTextBox1.Text + shu1;
     60             }           
     61         }
     62         void creatshu()
     63         {
     64             if (youwufushu == 1)//含有负数
     65             {
     66                 randfushu = A.Next(1, 3);
     67                 if (randfushu == 1)
     68                 {
     69                     if (youwuzhenfenshu == 1)// 含有真分数
     70                     {
     71                         randshu = A.Next(1, 3);
     72                         if (randshu == 1)
     73                         {
     74                             shu1 = A.Next(min, max);
     75                             shu2 = A.Next(min, max);
     76                             if (shu1 < shu2)
     77                                 richTextBox1.Text = richTextBox1.Text +"(-"+ shu1 + "/" + shu2+")";
     78                             else
     79                                 richTextBox1.Text = richTextBox1.Text +"(-"+ shu2 + "/" + shu1+")";
     80                         }
     81                         else
     82                         {
     83                             shu1 = A.Next(min, max);
     84                             richTextBox1.Text = richTextBox1.Text + shu1;
     85                         }
     86                     }
     87                     else// 全为整数
     88                     {
     89                         shu1 = A.Next(min, max);
     90                         richTextBox1.Text = richTextBox1.Text + "(-" + shu1 + ")";
     91                     }
     92                     
     93                 }
     94                 else
     95                 {
     96                     zhengshu();
     97                 }
     98             }
     99             else
    100             {
    101                 zhengshu();
    102             }
    103         }
    104         void creatfuhao()//产生运算符号
    105         {
    106             //if(youwukuohao==1)
    107             if (youwuchengchu == 1)
    108             {
    109                 fuhao = A.Next(1, 5);
    110                 if (fuhao == 1)
    111                     richTextBox1.Text = richTextBox1.Text + "";
    112                 else if (fuhao == 2)
    113                     richTextBox1.Text = richTextBox1.Text + "";
    114                 else if (fuhao == 3)
    115                     richTextBox1.Text = richTextBox1.Text + " × ";
    116                 else
    117                     richTextBox1.Text = richTextBox1.Text + " ÷ ";
    118 
    119             }
    120             if (youwuchengchu == 2)
    121             {
    122                 fuhao = A.Next(1, 3);
    123                 if (fuhao == 1)
    124                     richTextBox1.Text = richTextBox1.Text + "";
    125                 else
    126                     richTextBox1.Text = richTextBox1.Text + "";
    127             }
    128         }
    129 
    130         public Form1()
    131         {
    132             InitializeComponent();
    133         }
    134 
    135         private void Form1_Load(object sender, EventArgs e)
    136         {
    137 
    138         }
    139         private void textBox1_TextChanged(object sender, EventArgs e)
    140         {
    141             min = int .Parse(textBox1.Text);
    142         }
    143         private void textBox2_TextChanged(object sender, EventArgs e)
    144         {
    145             max = int.Parse (textBox2.Text);
    146         }
    147         private void textBox3_TextChanged(object sender, EventArgs e)
    148         {
    149             countti = int.Parse(textBox3.Text);
    150         }
    151         private void radioButton1_CheckedChanged(object sender, EventArgs e)
    152         {
    153             if (radioButton1.Checked == true)
    154                 youwuchengchu = 1;
    155         }
    156         private void radioButton2_CheckedChanged(object sender, EventArgs e)
    157         {
    158             if (radioButton2.Checked == true)
    159                 youwuchengchu = 2;
    160         }
    161         private void radioButton3_CheckedChanged(object sender, EventArgs e)
    162         {
    163             if (radioButton3.Checked == true)
    164                 youwufushu = 1;
    165         }
    166         private void radioButton4_CheckedChanged(object sender, EventArgs e)
    167         {
    168             if (radioButton4.Checked == true)
    169                 youwufushu = 2;
    170         }
    171         private void radioButton5_CheckedChanged(object sender, EventArgs e)
    172         {
    173             if (radioButton5.Checked == true)
    174                 youwuyushu = 1;
    175         }
    176         private void radioButton6_CheckedChanged(object sender, EventArgs e)
    177         {
    178             if (radioButton6.Checked == true)
    179                 youwuyushu = 2;
    180         }
    181         private void radioButton7_CheckedChanged(object sender, EventArgs e)
    182         {
    183             if (radioButton7.Checked == true)
    184                 youwuzhenfenshu = 1;
    185         }
    186         private void radioButton8_CheckedChanged(object sender, EventArgs e)
    187         {
    188             if (radioButton8.Checked == true)
    189                 youwuzhenfenshu = 2;
    190         }
    191         private void radioButton9_CheckedChanged(object sender, EventArgs e)
    192         {
    193             if (radioButton9.Checked == true)
    194                 dayin = 1;
    195         }
    196         private void radioButton10_CheckedChanged(object sender, EventArgs e)
    197         {
    198             if (radioButton10.Checked == true)
    199                 dayin = 2;
    200         }
    201         private void radioButton11_CheckedChanged(object sender, EventArgs e)
    202         {
    203             if (radioButton11.Checked == true)
    204                 youwukuohao = 2;
    205         }
    206         private void radioButton12_CheckedChanged(object sender, EventArgs e)
    207         {
    208             if (radioButton12.Checked == true)
    209                 youwukuohao = 1;
    210         }
    211         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    212         {
    213             countshu = int.Parse(comboBox1.Text);
    214         }
    215         private void button1_Click(object sender, EventArgs e)
    216         {
    217             richTextBox1.Text = "出题结果:
    ";
    218 
    219             if (youwuyushu==1)//可以有余数
    220             {
    221                 for (int i = 0; i < countti; i++)
    222                 {
    223 
    224                     if (youwukuohao==1)
    225                     {
    226                         int randnum = A.Next(1, 100);
    227                         if (randnum < 10)
    228                         {
    229                             youwukuohao = 2;
    230                         }
    231                         else
    232                             youwukuohao = 1;
    233                        
    234                     }
    235                     //产生括号位置
    236                     for (int j=0;j<10 ;j++ )//只循环十次,若有合适位置,则产生;若没有,则位置为1
    237                     {
    238                         kuohaolocation = A.Next(1, 10);
    239                         if (countshu > 2 && kuohaolocation < (countshu - 1))
    240                         {
    241                             break;
    242                         }
    243                         kuohaolocation = 1;
    244                     }
    245 
    246 
    247                     //输出题号
    248                     richTextBox1.Text = richTextBox1.Text + "(" + (i + 1) + ")	";
    249 
    250                     //产生第一个数
    251                     creatshu();
    252                     //产生运算符号
    253                     creatfuhao();
    254                     //第二个数前产生左括号
    255                     if (youwukuohao == 1 && kuohaolocation == 1 && countshu > 2 && kuohaolocation < (countshu - 1))
    256                     {
    257                         richTextBox1.Text = richTextBox1.Text + "(";
    258                     }                 
    259                     //产生第二个数
    260                     creatshu();
    261                     for (count = 2; count < 10; count++)
    262                     {
    263                         if (count == countshu)//判断数值个数够不够
    264                         {
    265                             break;
    266                         }
    267 
    268                         //产生运算符号
    269                         creatfuhao();
    270                         if (youwukuohao == 1 && kuohaolocation == count  && countshu > 2 && kuohaolocation < (countshu - 1))
    271                         {
    272                             richTextBox1.Text = richTextBox1.Text + "(";
    273                         }  
    274                         //产生第3……10个数
    275                         creatshu();
    276 
    277                     }
    278                     
    279                     //在末尾添加右括号
    280                     if (countshu > 2 && kuohaolocation < (countshu - 1) && youwukuohao == 1)
    281                     {
    282                         richTextBox1.Text = richTextBox1.Text + " )";
    283                     }
    284                     if (dayin == 1)//打印方式,一列或者两列
    285                     {
    286                         richTextBox1.Text = richTextBox1.Text + " =
    ";
    287                     }
    288                     else
    289                     {
    290                         if (i % 2 == 0)//打印方式,一列或者两列
    291                         {
    292                             richTextBox1.Text = richTextBox1.Text + " =				";
    293                         }
    294                         else
    295                         {
    296                             richTextBox1.Text = richTextBox1.Text + " =
    ";
    297                         }
    298 
    299                     }
    300                 }
    301             }
    302             else//不可以有余数
    303             {
    304                 for (int i = 0; i < countti; i++)
    305                 {
    306                     //输出题号
    307                     richTextBox1.Text = richTextBox1.Text + "(" + (i + 1) + ")	";
    308 
    309                     //产生第一个数
    310                     shu1 = A.Next(min, max);
    311 
    312                     //产生运算符号
    313                     fuhao = A.Next(1, 5);
    314 
    315                     if (fuhao==4)//如果是除法
    316                     {
    317                         for (; ; )//使两数相除没有余数,大数为 shu1
    318                         {
    319                             //产生第二个数
    320                             shu2 = A.Next(min, max);
    321                             if (shu1 < shu2)
    322                             {
    323                                 temp = shu2;
    324                                 shu2 = shu1;
    325                                 shu1 = temp;
    326                             }
    327                             yushu = shu1 % shu2;
    328                             if (yushu==0)
    329                             {
    330                                 break;
    331                             }
    332                         }
    333                     }
    334 
    335                     //输出第一个数
    336                     if (youwufushu == 1)//含有负数
    337                     {
    338                         randfushu = A.Next(1, 3);
    339                         if (randfushu == 1)
    340                         {
    341                             richTextBox1.Text = richTextBox1.Text + shu1;
    342                         }
    343                         else
    344                         {
    345                             richTextBox1.Text = richTextBox1.Text + "(-" + shu1 + ")";
    346                         }
    347                     }
    348                     else
    349                     {
    350                         richTextBox1.Text = richTextBox1.Text + shu1;
    351                     }
    352 
    353                     //输出运算符
    354                     if (youwuchengchu == 1)
    355                     {
    356                         if (fuhao == 1)
    357                             richTextBox1.Text = richTextBox1.Text + "";
    358                         else if (fuhao == 2)
    359                             richTextBox1.Text = richTextBox1.Text + "";
    360                         else if (fuhao == 3)
    361                             richTextBox1.Text = richTextBox1.Text + " × ";
    362                         else
    363                             richTextBox1.Text = richTextBox1.Text + " ÷ ";
    364                     }
    365                     if (youwuchengchu == 2)
    366                     {
    367                         if (fuhao == 1||fuhao==3)
    368                             richTextBox1.Text = richTextBox1.Text + "";
    369                         else
    370                             richTextBox1.Text = richTextBox1.Text + "";
    371                     }                  
    372 
    373                     //输出第二个数
    374                     if (youwufushu == 1)//含有负数
    375                     {
    376                         randfushu = A.Next(1, 3);
    377                         if (randfushu == 1)
    378                         {
    379                             richTextBox1.Text = richTextBox1.Text + shu2;
    380                         }
    381                         else
    382                         {
    383                             richTextBox1.Text = richTextBox1.Text + "(-" + shu2 + ")";
    384                         }
    385                     }
    386                     else
    387                     {
    388                         richTextBox1.Text = richTextBox1.Text + shu2;
    389                     }
    390                     //在末尾添加右括号
    391                     if (countshu>2&&kuohaolocation < (countshu - 2) && youwukuohao == 1)
    392                     {
    393                         richTextBox1.Text = richTextBox1.Text + " )";
    394                     }
    395 
    396                     if (dayin==1)//打印方式,一列或者两列
    397                     {
    398                         richTextBox1.Text = richTextBox1.Text + " =
    ";
    399                     }
    400                     else
    401                     {
    402                         
    403                         if (i%2 == 0)//打印方式,一列或者两列
    404                         {
    405                             richTextBox1.Text = richTextBox1.Text + " =			";
    406                         }
    407                         else
    408                         {
    409                             richTextBox1.Text = richTextBox1.Text + " =
    ";
    410                         }     
    411                     }
    412                 }
    413             }
    414         }
    415         private void button2_Click(object sender, EventArgs e)
    416         {
    417             //string s = richTextBox1.Text;
    418             //s = s.Replace("
    ", "
    ");
    419             //StreamWriter myStream;
    420             //saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    421             //saveFileDialog1.FilterIndex = 2;
    422             //saveFileDialog1.RestoreDirectory = true;
    423             //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    424             //{
    425             //    myStream = new StreamWriter(saveFileDialog1.FileName);
    426             //    myStream.Write(richTextBox1.Text); //写入
    427             //    myStream.Close();//关闭流
    428             //}
    429             //自动设置路径和名称,但txt文本中不能自动换行,不知何原因
    430             richTextBox1.SaveFile("出题.txt", RichTextBoxStreamType.PlainText);//保存在 bin 目录debug 下
    431         }
    432     }
    433 }

    5、运行结果

    项目计划日志(单位:h):

      听课 编写程序 阅读相关书籍 网上查找资料   日总计
    周一 2 4 1 0 7
    周二  0 2  0  0 2
    周三  0 3  1  0 4
    周四 2 2  0  0 4
    周五  0 6 2 0 8
    周六  0 4 0  0 4
    周日          
    周总计 4 21 0 4 29

    时间记录日志(单位:min):

    日期 开始时间 结束时间 中断时间 净时间 活动 备注
    星期一 14:00 15:50 10(课间) 100 听课 软件工程上课
      16:20 17:40 0 80 看一些资料,编程 自学delphi,利用delphi7完成了一个计算器简单程序
      19:30 21:40 0 130
    星期二 19:30 21:30 0 120 编程 四则运算2的程序
    星期三 14:00  15:00 0  60 编程 四则运算2的程序
      19:00 21:30 0 150 编程
    星期四 14:00 15:50 10(课间) 100 听课 软件工程上课
      19:30 21:50 0 100 编程 四则运算2的程序
    星期五 14:00 17:00 0 180 编程 四则运算2的程序
      18:30 21:50 0 150 编程 四则运算2的程序
    星期六 8:00 11:30 20(去同学宿舍学习交流) 200 修改,调试,发布 四则运算2程序进行修改、调试、写博客并发布
      14:00 15:40   100 修改博客  

     

    这次换用了C#编写此程序,随机数的定义方法有了改变,很多都是因为语言不通,语法不同导致的错误

     

    缺陷记录日志:

      出现的问题 修复所用时间(min)
        1     随机数产生的函数有问题,跟C++有所不同 10
    2 if 语句太多,逻辑关系出错 20
    3 break在for循环中的放置有问题 10
    4 最后的输出txt,txt文本中不能换行 30
  • 相关阅读:
    JS单例对象与构造函数对象的区别
    SVG系列
    Js极客之路
    Js极客之路
    iOS微信登录
    iOS HSV
    cocoa pods
    php中请求数据中文乱码
    付费中数字计算
    时间戳对应关系
  • 原文地址:https://www.cnblogs.com/wangyw/p/5267897.html
Copyright © 2011-2022 走看看