zoukankan      html  css  js  c++  java
  • 小学四则运算改进

          第一次写的太过简单,想做改进。本来的想法是做一个递归的生成混合运算,但是过于复杂。

          所以只随机生成一次算符,添加了真分数的内容,在算式必要的地方加了括号,数值范围控制在100以内。比较第一次,编程时低级错误少多了。

      第一周 第二周 第三周 第四周 第五周 第六周
    时间(包括上课) 5h 5h        
    代码量 100 150        
    博客量 1 1        
    了解的知识点 随机数生成 switch的用法        

    #include "iostream.h"
    #include "time.h"
    #include <stdlib.h>
    #define N 30


    void randop(int &a);
    void randnum(int &a,int &b);

    void main(){
    int opnum=0,i,a,b=0;
    srand((unsigned)time(NULL));
    for(i=N;i>1;i--){
    randop(opnum);
    switch(opnum){
    case 0: { randnum(a,b);
    if(b==0) cout<<a<<"+";
    if(b!=0) cout<<b<<"/"<<a<<"+";
    randnum(a,b);
    if(b==0) cout<<a<<"="<<endl;
    if(b!=0) cout<<b<<"/"<<a<<"="<<endl;;
    }; break;
    case 1:{ randnum(a,b);
    if(b==0) cout<<a<<"-";
    if(b!=0) cout<<b<<"/"<<a<<"-";
    randnum(a,b);
    if(b==0) cout<<a<<"="<<endl;;
    if(b!=0) cout<<b<<"/"<<a<<"="<<endl;;
    }; break;
    case 2:{
    randnum(a,b);
    if(b==0) cout<<a<<"*";
    if(b!=0) cout<<"("<<b<<"/"<<a<<")"<<"*";
    randnum(a,b);
    if(b==0) cout<<a<<"="<<endl;;
    if(b!=0) cout<<"("<<b<<"/"<<a<<")"<<"="<<endl;
    }break;
    case 3:{
    randnum(a,b);
    if(b==0) cout<<a<<"/";
    if(b!=0) cout<<"("<<b<<"/"<<a<<")"<<"*";
    randnum(a,b);
    if(b==0) cout<<a<<"="<<endl;
    if(b!=0) cout<<"("<<b<<"/"<<a<<")"<<"*"<<endl;
    }break;

    }
    }

    }

    void randop(int &a)
    {
    a=rand()%4;
    }

    void randnum(int &a,int &b){
    if(rand()%2==0)
    {
    a=rand()%100;
    b=rand()%a;
    }
    else{
    a=rand()%100;
    }
    }

  • 相关阅读:
    make -j 8参数的作用
    使用请求头认证来测试需要授权的 API 接口
    查看Linux系统的平均负载
    服务器负载均衡的基本功能和实现原理
    Oracle RAC学习笔记:基本概念及入门
    详解物化视图(汇总比较有用的资料)
    程序优化注意的一些点
    PR 审批界面增加显示项方法
    Most Common Solutions to FRM-41839 and .tmp Files Not Being Deleted
    APPCORE Routine APIs
  • 原文地址:https://www.cnblogs.com/wding/p/5276775.html
Copyright © 2011-2022 走看看