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

    1、思路

    先编写两个0~99的随机整数的四则运算,然后再将两个整数变成随机生成的真分数和整数,0~3分别代表+-*/,然后输出。

    2、用时

    1小时20分钟

    3、运行截图

    4、代码

    #include<iostream>
    #include<cmath>
    using namespace std;
    void main()
    {
     int i, j, a, m, n,l,k;
     for (i = 0; i < 30; i++)
     {
      l = rand() % (100);
      k = l % 2;
      if (k == 0)
      {
       m = rand() % (100);
       n = rand() % (100);
       if (m < n)
        cout << "("<<m << "/" << n<<")";
       if (m>n)
        cout <<"("<< n << "/" << m<<")";
       if (m == n)
       {
        if (m == 0) 
         cout << "("<<m << "/" << ((rand() % 100) + 20) / 2<<")";
        else
            cout << "("<<m / 2 << "/" << n<<")";
       }
      }
      else
      {
       a = rand() % (100);
       cout << a;
      }
      
      j = rand()%4;
      if (j == 0) cout << "+";
      else if (j == 1) cout << "-";
      else if (j == 2) cout << "*";
      else cout << "/";

      l = rand() % (100);
      k = l % 2;
      if (k == 0)
      {
       m = rand() % (100);
       n = rand() % (100);
       if (m < n)
        cout << "("<<m << "/" << n<<")";
       if (m>n)
        cout <<"("<< n << "/" << m<<")";
       if (m == n)
       {
        if (m == 0)
         cout << "("<<m << "/" << ((rand() % 100) + 20) / 2<<")";
        else
         cout << "("<<m / 2 << "/" << n<<")";
       }
      }
      else
      {
       a = rand() % (100);
       cout << a;
      }
      cout <<"="<< endl;
      
     }

    }

    5、代码行数

    66行

  • 相关阅读:
    博客备份小工具3
    博客转发小工具1
    04-属性选择器
    05-伪类选择器
    03-高级选择器
    02-css的选择器
    01-css的引入方式
    函数进阶-(命名空间、作用域、函数嵌套、作用域链、函数名本质、闭包)
    函数基础-(引子、定义函数、调用函数、函数返回值、函数参数)
    Python之路【第08章】:Python函数
  • 原文地址:https://www.cnblogs.com/lq897897/p/5247565.html
Copyright © 2011-2022 走看看