zoukankan      html  css  js  c++  java
  • 作业2(1)

    题目

    自动生成四则运算题目

    主要功能

    软件的功能是根据设计者的选择来生成四则运算题目。软件首先根据自己的需求输入自己想做的四则运算题的数目。之后根据需求,选择整数四则运算或者真分数四则运算。当输入1时,自动生成整数四则运算。相反则自动生成真分数四则运算。最后选择是否输出答案,当输入1时将会为你输出答案,相反则不会输出答案。如果使用者仍想继续使用,可以输入1,相反则可按任意键退出。

    设计思路

    程序主题分为三个部分:定义及调用的头文件、四则运算、随机数获得。其中加.减.乘.除四种运算的处理过程,通过主函数的switch开关语句和一个while循环来调用,该程序的关键是通过使用"rand()%10"来获取一个0到9的一位整数随机值或真分数值来为用户出题。

    源代码

    #include<iostream.h>
    #include<stdlib.h>
    #include<conio.h>
    
    void DealFenshu(int m, int a[][2])
    {
         for(int p=0;p<m;p++)
         {
            int i=(int)rand()%10;
            int j=(int)rand()%10;
            while(j==0||i>=j)
            {
                i=(int)rand()%10;
                j=(int)rand()%10;
            }
            int x=(int)rand()%10;
            int y=(int)rand()%10;
            while(y==0||x>=y)
            {
                x=(int)rand()%10;
                y=(int)rand()%10;
            }
            int k=(int)rand()%100/25;
            switch(k)
            {
                case 0:
                    cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"=";
                a[p][0]=i*y+x*j;
                a[p][1]=j*y;
                break;
                case 1:
                    cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"=";
                a[p][0]=i*y-x*j;
                a[p][1]=j*y;
                break;
                case 2:
                    cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"=";
                a[p][0]=i*x;
                a[p][1]=j*y;
                break;
                case 3:
                a[p][0]=i*y;
                a[p][1]=j*x;
                    cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"=";
                }
                
                if(p%5==4)
                {
                cout<<endl;
                }
                else
                {
                cout<<'	';
                }
        }
    
    }
    void DisplayFenshu(int a[][2],int w,int m)
    {
        if(w==1)
        {
            for(int q=0;q<m;q++)
            {
                if(a[q][0]==0)
                    cout<<"0"<<'	';
                else
                    cout<<a[q][0]<<"/"<<a[q][1]<<'	';
                if(q%5==4)
                {
                    cout<<endl;
                }
            }
        }
        
    }
    void DealInt(int m,int a[])
    {
            
        for(int p=0;p<m;p++)
        {
        int i=(int)rand()%10;
        int j=(int)rand()%10;
        int k=(int)rand()%100/25;
        switch(k)
        {
        case 0:
            cout<<i<<"+"<<j<<"=";
                a[p]=i+j;
            break;
        case 1:
            cout<<i<<"-"<<j<<"=";
            a[p]=i-j;
            break;
        case 2:
            cout<<i<<"*"<<j<<"=";
            a[p]=i*j;
            break;
        case 3:
            try
            {
            a[p]=i/j;
            cout<<i<<"/"<<j<<"=";
            }
            catch(...)
            {
                p--;
            }
            
    
        }
            
            if(p%5==4)
            {
                cout<<endl;
            }
            else
            {
                cout<<'	';
            }
        }
    }
    void DisplayInt(int a[],int w,int m)
    {
        if(w==1)
        {
            for(int q=0;q<m;q++)
            {
                cout<<a[q]<<'	';
                if(q%5==4)
                {
                    cout<<endl;
                }
            }
        }
        else
        {};
    }
    int main()
    {
        int p;
        do
        {
            system("cls");
            int a[1000],b[1000][2];
            int m,n,w;
            cout<<"请输入生成的四则运算题个数:";
            cin>>m;
            cout<<endl;
            cout<<"请输入要生成的四则运算种类(输入1为整数,否则为真分数):";
            cin>>n;
            cout<<endl;
            if(n==1)
            {
                DealInt(m,a);
                cout<<endl;
            }
            else
            {
                DealFenshu(m,b);
                cout<<endl;
            }
            cout<<"是否输出答案(输入1则输出答案否则不输出答案)"<<endl;
            cin>>w;
            if(n==1)
            {
                DisplayInt(a,w,m);        
            }
            else
            {
                DisplayFenshu(b,w,m);
            }
            cout<<endl;
            cout<<"是否继续生成运算题(输入1则生成否则不生成)"<<endl;
            cin>>p;
            cout<<endl;
        }while(1==p);
    
    } 
    
    

    程序运行截图



    总结

    经过这几天的坚持和努力,我终于自主完成了这个程序。因为本身的水平能力有限,一开始遇到了许许多多的困难。在同学们的细心帮助和自己一点点研究,终于将程序完成了
    在我看来,这个程序主要包括以下几个方面:1.如何设计四则运算的运算方式2.如何设计选取随机数字功能3.如何使用定义四则运算和头文件将几个方面衔接起来
    虽然该程序显得不是那么困难,但是开始制作后才发现,也不是想象中那么一帆风顺。那三个主要功能的实现都隐隐有些问题在里面。如何设计,如何衔接,如何解决选取随机数中的问题,如何解决四则运算中的返回结果出错的问题。都需要一步步解决。但是无论如何,程序都在磕磕碰碰中完成了,以后我会以更加坚决的意志去解决作业中将面对的困难。永不放弃,坚持努力,以求认真的态度完成未来的各种困难。

  • 相关阅读:
    Codeforces Round #251 (Div. 2) A
    topcoder SRM 623 DIV2 CatAndRat
    topcoder SRM 623 DIV2 CatchTheBeatEasy
    topcoder SRM 622 DIV2 FibonacciDiv2
    topcoder SRM 622 DIV2 BoxesDiv2
    Leetcode Linked List Cycle II
    leetcode Linked List Cycle
    Leetcode Search Insert Position
    关于vim插件
    Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones
  • 原文地址:https://www.cnblogs.com/l994/p/5284919.html
Copyright © 2011-2022 走看看