zoukankan      html  css  js  c++  java
  • 随机生成30道四则运算题目

      软件工程课上老师给我们提出了这么一个任务:编写一个程序,要求随机生成30道四则运算题目,包含真分数的运算,当时在课上老师讲了一种分解问题的方法,就是将一个看上去很难实现的问题进行分解,最后分解成为可以实现的一个个小问题,因为我的编程技术不太好,所以我就使用了老师的方法对这个问题进行分解,以下就是我当时分解后的结果:

    后来在实现的过程中我发现用数组实现运算符的随机生成实现对我来说有些困难,所以就简化了这一步的实现,具体代码如下:

     1 //实现随机生成30道小学四则运算题的程序,李青,2016/3/5
     2 #include<stdio.h>
     3 #include<iostream>
     4 #include<time.h>
     5 using namespace std;
     6 void main()
     7 {
     8     srand((int)time(NULL));//时间种子
     9     int i, j, k, numerator1, denominator1,numerator2,denominator2,exchange1,exchange2;
    10     char operation;
    11     for (i = 0; i < 30; i++)//循环输出
    12     {
    13         j = rand() % 100;
    14         k = rand() % 100;
    15         numerator1 = rand() % 100;//分子1
    16         denominator1 = rand() % 100;//分母1
    17         numerator2 = rand() % 100;//分子2
    18         denominator2 = rand() % 100;//分母2
    19         //以下两个是对真分数分母的非零判断及更正
    20         while (denominator1 == 0)
    21         {
    22             denominator1 = rand() % 100;
    23             break;
    24         }
    25         while (denominator2 == 0)
    26         {
    27             denominator2 = rand() % 100;
    28             break;
    29         }
    30         if (i % 2 == 0)//判断是整数运算
    31         {
    32             if (j % 4 == 0) operation = '+';
    33             else if (j % 4 == 1) operation = '-';
    34             else if (j % 4 == 2) operation = '*';
    35             else//如果是除法运算,对分母进行非零判断
    36             {
    37                 while (numerator2 == 0)
    38                 {
    39                     numerator2 = rand() % 100;
    40                     break;
    41                 }
    42                 operation = '/';
    43             }
    44             cout << numerator1 << "  " << operation << "  " << numerator2 << "   =" << endl;
    45         }
    46         else//是真分数运算
    47         //对分数的判断,将不是真分数的转换成真分数
    48         while (numerator1 > denominator1)
    49         {
    50             exchange1 = numerator1;
    51             numerator1 = denominator1;
    52             denominator1 = exchange1;
    53         }
    54         while (numerator2 > denominator2)
    55         {
    56             exchange2 = numerator2;
    57             numerator2 = denominator2;
    58             denominator2 = exchange2;
    59         }
    60         if (j % 4 == 0) operation = '+';
    61         else if (j % 4 == 1) operation = '-';
    62         else if (j % 4 == 2) operation = '*';
    63         else//如果是除法运算,对分母进行非零判断
    64         {
    65             while (numerator2 == 0)
    66             {
    67                 numerator2 = rand() % 100;
    68                 break;
    69             }
    70             operation = '/';
    71         }
    72         cout << "(" << numerator1 << "/" << denominator1 << ")" <<"  "<< operation << "  " << "(" << numerator2 << "/" << denominator2 << ")" << "   =" << endl;
    73     }
    74 }

    结果截图如下:

    这次的程序编写是我实现的为数不多的几个程序之一,感觉对自己以后编写更多更好的程序有很大帮助。

  • 相关阅读:
    1058 A+B in Hogwarts (20)
    1046 Shortest Distance (20)
    1061 Dating (20)
    1041 Be Unique (20)
    1015 Reversible Primes (20)(20 分)
    pat 1027 Colors in Mars (20)
    PAT 1008 Elevator (20)
    操作系统 死锁
    Ajax的get方式传值 避免& 与= 号
    让IE浏览器支持CSS3表现
  • 原文地址:https://www.cnblogs.com/liqing1/p/5247333.html
Copyright © 2011-2022 走看看