• 软件工程个人作业02——第二版


    Note

    /*

    第一版为能自动生成30道小学四则运算题目,要求除了整数以外,还要支持假分数的四则运算。

    这个是先把第一版的面向过程改成了面向对象,然后设计的第二版。

    第二版的要求为:

    1、题目避免重复;_

    2、可定制(数量/打印方式);_

    3、可以控制下列参数:

            是否有乘除法;

            是否有括号(最多可以支持十个数参与计算)

            数值范围;

            加减有无负数;

            除法有无余数!

    第二个版本的限制为最多出2000道题,用的宏定义

    如果想要不重复,我需要把被除数和除数分别用数组表示,这样才能在后面用于比较是否重复。

    在判断是否重复的时候,用的方法不是一次性判断第一个数并且第二个数并且符号都同时相等,这样比较每次需要比较三个;

    我用的是,先判断第一个,如果和上面有重复的,那么比较第二个;如果第二个同时也重复了,那么比较符号。这样只有当上一个比较满足的时候,

    才会比较后面的;否则后面的根本不需要比较,觉得可以简化比较过程。

    关于这个程序的一个想法,不同于现在的编程思路:输出全部的条件,让用户选择。

    然后根据条件,来随机出现选定的条件。比如是否有乘除法,有的话,就随机出现;没有的话,就不用出现。

    大概是根据大一通讯录的添加,查询,删除之类的程序来的想法。

    这个程序的main实现了上面这个想法,比第一个第二版条理要清晰很多。但是依然只是实现了题目避免重复;可定制(数量,是否有乘除法;数值范围。

    */

    Main

    1. /* 底云飞 2016.3.36 这就是note中第二种想法的实现,四则运算第二个版本后来想法的实现 */
    2. #include <iostream>
    3. #include <time.h>
    4. #include <string>
    5. using namespace std;
    6. #define MAX 2000
    7.  
    8. //带有乘除法的四则运算
    9. void show_multiplication(float rand1,float rand2,float operate)
    10. {
    11.       if(operate==1)
    12.          cout<<rand1<<"+"<<rand2<<"="<<endl;
    13.       else if(operate==2)
    14.          cout<<rand1<<"-"<<rand2<<"="<<endl;
    15.       else if(operate==3)
    16.          cout<<rand1<<"*"<<rand2<<"="<<endl;
    17.       else if(operate==4)
    18.          cout<<rand1<<"/"<<rand2<<"="<<endl;
    19. }
    20.  
    21. //没有乘除,没有分数的四则运算
    22. void show_plus(float rand1,float rand2,float operate)
    23. {
    24.       if(operate==1)
    25.          cout<<rand1<<"+"<<rand2<<"="<<endl;
    26.       else if(operate==2)
    27.          cout<<rand1<<"-"<<rand2<<"="<<endl;
    28. }
    29.  
    30. //带假分数的四则运算
    31. void show_fraction(float rand1,float rand2,float rand3,float rand4,float operate)
    32. {
    33.       if(operate==1)
    34.          cout<<"("<<rand1<<"/"<<rand3<<")+"<<"("<<rand2<<"/"<<rand4<<")="<<endl;
    35.       else if(operate==2)
    36.          cout<<"("<<rand1<<"/"<<rand3<<")-"<<"("<<rand2<<"/"<<rand4<<")="<<endl;
    37.       else if(operate==3)
    38.          cout<<"("<<rand1<<"/"<<rand3<<")*"<<"("<<rand2<<"/"<<rand4<<")="<<endl;
    39.       else if(operate==4)
    40.          cout<<"("<<rand1<<"/"<<rand3<<")/"<<"("<<rand2<<"/"<<rand4<<")="<<endl;
    41. }
    42.  
    43. void main()
    44. {
    45.    srand((unsigned)time(0)); //随机数发生器,用时间做随机种子,保证为真随机数
    46.    int flag,fraction_flag,multiplication_flag,number=0,a=0,b=0,max_num=0;
    47.    //flag用来随机出现乘除运算和分数运算,fraction_flag判断是否需要有假分数,multiplication_flag判断是否需要有乘除法,
    48.    //number题目数量,a为加减乘除法数量,b为假分数运算数量,max_num数值上限
    49.    float rand1[MAX],rand2[MAX],rand3[MAX],rand4[MAX],operate[4]; //分别为第一二三四个随机数,运算符
    50.  
    51.    cout<<"请输入数值上限:"<<endl;
    52.    cin>>max_num;
    53.    cout<<"题目的数量为:"<<endl;
    54.    cin>>number;
    55.    cout<<"需要有乘除法吗?需要输入1,不需要输入0:"<<endl;
    56.    cin>>multiplication_flag;
    57.    cout<<"需要有分数吗?需要输入1,不需要输入0:"<<endl;
    58.    cin>>fraction_flag;
    59.  
    60.    for(int i=1;;i++)
    61.    {
    62. p: rand1[i] = rand()%max_num;
    63.       rand2[i] = rand()%max_num; //第一二个随机数
    64.       rand3[i] = rand()%max_num;
    65.       rand4[i] = rand()%max_num; //第三四个随机数
    66.       operate[i%4] = rand()%4+1; //生成随机运算符的代表数字
    67.  
    68.       if(multiplication_flag==1) //如果需要乘除法
    69.       {
    70.          flag = rand()%2;
    71.          if(flag == 1)
    72.          {
    73.             for(int j=1;j<=i-1;j++)
    74.                if(rand1[i]==rand1[j])
    75.                   if(rand2[i]==rand2[j])
    76.                      if(operate[i%4]==operate[j%4]) //判断是否重复
    77.                         goto p;
    78.             show_multiplication(rand1[i],rand2[i],operate[i%4]);
    79.             a++;
    80.          }
    81.       }
    82.       //在这加一个判断是因为:假设number等于5,如果当a=3,b=2的时候,这里如果没有判断,
    83.       //那么如果下面的flag=1之后,这时b就会加1,直接a+b=6,略过5了,就不能正确显示出5个运算了。
    84.       if((a+b) == number)
    85.          break;
    86.       if(fraction_flag==1) //如果需要分数
    87.       {
    88.          flag = rand()%2;
    89.          if(flag == 1)
    90.          {
    91.             for(int j=1;j<=i-1;j++)
    92.                if(rand1[i]==rand1[j])
    93.                   if(rand3[i]==rand3[j])
    94.                      if(rand2[i]==rand2[j])
    95.                         if(rand4[i]==rand4[j])
    96.                            if(operate[i%4]==operate[j%4]) //判断是否重复
    97.                               goto p;
    98.             show_fraction(rand1[i],rand2[i],rand3[i],rand4[i],operate[i%4]);
    99.             b++;
    100.          }
    101.       }
    102.       else if(multiplication_flag==0&&fraction_flag==0) //如果乘除法和分数都不需要
    103.       {
    104.          operate[i%4] = rand()%2+1; //生成随机运算符的代表数字,因为只有两种运算,所以需要重新生成运算符
    105.          for(int j=1;j<=i-1;j++)
    106.             if(rand1[i]==rand1[j])
    107.                if(rand2[i]==rand2[j])
    108.                   if(operate[i%4]==operate[j%4]) //判断是否重复
    109.                      goto p;
    110.          show_plus(rand1[i],rand2[i],operate[i%4]);
    111.          a++;
    112.       }
    113.       if((a+b) == number)
    114.          break;
    115.    }
    116. }
  • 相关阅读:
    传输层——UDP报文头介绍
    传输层——TCP报文头介绍
    网络层——IP报文头介绍
    数据链路层——以太网包头介绍
    POJ2752 (Seek the Name, Seek the Fame,kmp)
    POJ2406 Power Strings
    HNOI2008 玩具装箱toy (BZOJ1010,斜率dp)
    Covered Walkway(HDU4258,dp斜率优化)
    HDU3507 Print Article
    POJ1821 Fence
  • 原文地址:https://www.cnblogs.com/diyunfei/p/5325203.html
Copyright © 2020-2023  润新知