简单四则运算

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行

原文地址:https://www.cnblogs.com/lq897897/p/5247565.html