作业二(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<<'\t';
            }
    }

}
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"<<'\t';
            else
                cout<<a[q][0]<<"/"<<a[q][1]<<'\t';
            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<<'\t';
        }
    }
}
void DisplayInt(int a[],int w,int m)
{
    if(w==1)
    {
        for(int q=0;q<m;q++)
        {
            cout<<a[q]<<'\t';
            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用定义四则运算和头文件将几个方面衔接起来就可以了。

这个程序看起来比较简单,但是要做到更好还是比较困难的,首先要考虑选取随机数中的一些问题,然后还要注意四则运算中的返回结果不要出错。不管怎么样在今后我会更加努力的学习,认真的完成老师留下的每一份作业,希望在老师的教导下可以取得满意的成绩。

posted @ 2016-03-15 11:36  哦路  阅读(115)  评论(1编辑  收藏  举报