zoukankan      html  css  js  c++  java
  • 四则运算程序

    对于生成30个随机的四则运算,我一开始的思路是这样的:

    1、生成一个随机数。

    2、将该生成的随机数控制在合适范围内

    3、随机选择一个运算符

    4、随机生成第二个随机数

    5、将该生成的随机数控制在合适范围内

    6、打印输出

    这样写了一个初步的程序:

    //王文奇 四则运算

    #include<iostream>

    #include<string>

    #include<iomanip>

    #include <time.h>

    using namespace std;

     

    void main()

    {

          int first, second,c,i;

          string op;

          srand((unsigned)time(NULL));  //做一个时间种子

          for (i = 0; i < 30; i++)

          {

              

               first = rand() % 99;   //产生第一个0~99以内的随机整数

               c = 1 + rand() % 4;   //产生1~4以内的随机整数

               switch (c)

               {

               case 1:op = "+"; break;

               case 2:op = "-"; break;

               case 3:op = "×"; break;

               case 4:op = "÷"; break;

     

               default: break;

               }

               second = rand() % 99;   //产生第二个0~99以内的随机整数

               cout << setw(3) << first << setw(3) << op << setw(3) << second << setw(3) << " = " << endl;

          }

         

    }

    图片1是运行结果。这大约用了30分钟。

    为了实现真分数的运算,我将其分为4种情况:

    1、 两个数均为整数;

    2、第一个为真分数,第二个为整数;

    3、第一个为整数,第二个为真分数;

    4、两个数均为真分数。

    真分数的实现:生成一个0~99的随机数,生成一个1~99的随机数,比较大小,将小的放在前面,大的放在后面,形如:小/大。

    这样写出了完善后的程序:

    //王文奇 四则运算

    #include<iostream>

    #include<string>

    #include<iomanip>

    #include <time.h>

    using namespace std;

     

    string oper(string op)

    {

          int c; 

          c = 1 + rand() % 4;   //产生1~4以内的随机整数

          switch (c)

          {

               case 1:op = "+"; break;

               case 2:op = "-"; break;

               case 3:op = "×"; break;

               case 4:op = "÷"; break;

             default:     break;

          }

          return op;

    }

    void number1()//两个数均为整数

    {

          string op;

          int first, second;

          first = rand() % 99;   //产生第一个0~99以内的随机整数

          second = rand() % 99;   //产生第二个0~99以内的随机整数

          cout << setw(3) << first << setw(3) << oper(op) << setw(3) << second << setw(3) << " = " << endl;

    }

    void number2()//第一个为真分数,第二个为整数

    {

          string op;

          int first1, first2, second,temp;

          first1 = rand() % 99;   //产生一个0~99以内的随机整数

          first2 = 1+rand() % 99;   //产生一个1~99以内的随机整数

          if (first1>first2)

          {

               temp = first1;

               first1 = first2;

               first2 = temp;//交换,保证first1<first2,且first2不等于0

          }

          second = rand() % 99;   //产生第二个0~99以内的随机整数

          cout << setw(3) << first1<<"/"<<first2 << setw(3) << oper(op) << setw(3) << second << setw(3) << " = " << endl;

    }

    void number3()//第一个为整数,第二个为真分数

    {

          string op;

          int first, second1, second2, temp;

          first = rand() % 99;   //产生第一个0~99以内的随机整数

          second1 = rand() % 99;   //产生一个0~99以内的随机整数

          second2 = 1 + rand() % 99;   //产生一个1~99以内的随机整数

          if (second1>second2)

          {

               temp = second1;

               second1 = second2;

               second2 = temp;//交换,保证second1<second2,且second2不等于0

          }

          cout << setw(3) << first << setw(3) << oper(op) << setw(3) << second1 << "/" << second2 << setw(3) << " = " << endl;

    }

    void number4()//两个数均为真分数

    {

          string op;

          int  first1, first2, second1, second2, temp;

     

          first1 = rand() % 99;   //产生一个0~99以内的随机整数

          first2 = 1 + rand() % 99;   //产生一个1~99以内的随机整数

          second1 = rand() % 99;   //产生一个0~99以内的随机整数

          second2 = 1 + rand() % 99;   //产生一个1~99以内的随机整数

         

          if (first1>first2)

          {

               temp = first1;

               first1 = first2;

               first2 = temp;//交换,保证first1<first2,且first2不等于0

          }

          if (second1>second2)

          {

               temp = second1;

               second1 = second2;

               second2 = temp;//交换,保证second1<second2,且second2不等于0

          }

          cout << setw(3) << first1 << "/" << first2 << setw(3) << oper(op) << setw(3) << second1 << "/" << second2 << setw(3) << " = " << endl;

    }

     

    void main()

    {

          int z,i;    

          srand((unsigned)time(NULL));  //做一个时间种子

          for (i = 0; i < 30; i++)

          {   

               z = 1 + rand() % 4;   //产生1~4以内的随机整数

               cout << "第" << setw(2)<<i + 1 << "题:";

               switch (z)

               {

                   case 1:number1(); break;

                     case 2:number2(); break;

                     case 3:number3(); break;

                     case 4:number4(); break;

                     default: break;

               }         

          }

         

    }

    图片完善后为运行结果。

    此过程大约用了40分钟。                        

  • 相关阅读:
    VirtualBox 收缩 vdi镜像文件
    虚拟机安装Lubuntu
    做一个平均数,合计数的sql查询
    postgersql 中 字段名,表名,命名大小写问题(原创)
    你人生中的那口井挖了没有?(转潇湘隐者)
    【智能无线小车系列十一】智能小车一体化测试
    【智能无线小车系列二】车体的组装
    【智能无线小车系列一】物品采购
    【智能无线小车系列十】通过USB摄像头实现网络监控功能
    【智能无线小车系列九】在树莓派上使用USB摄像头
  • 原文地址:https://www.cnblogs.com/qwer111/p/5247504.html
Copyright © 2011-2022 走看看