码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算题目生成程序(基于控制台)

时间:2017-03-06 14:26:44      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:images   standard   srand   ems   lan   时间   实现   统计   operator   

 源代码地址:https://git.coding.net/c_xx/02.git

题目描述:

  1. 除了整数以外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24
  2. 运算符为 +, ?, ×, ÷
  3. 并且要求能处理用户的输入,并判断对错,打分统计正确率。
  4. 要求能处理用户输入的真分数, 如 1/2, 5/12 等
  5. 使用 -n 参数控制生成题目的个数,例如执行下面命令将生成10个题目:Myapp.exe -n 10

需求分析:

  1. 支持整数及真分数的四则运算;
  2. 能够处理用户的输入并判断对错从而得出正确率;
  3. 使用-num参数控制生成题目的个数。

功能设计:

  1. 随机产生四则运算符;
  2. 随机产生整数或真分数四则运算;
  3. 对分数结果实现约分运算;
  4. 根据用户需求确定出题数目;
  5. 处理用户输入并判断正误,计算正确率。

代码说明:

为了实现需求,共设计了4个功能函数,分别是:

char Operator()            //随机选取运算符
int maxNumber(int k, int l)            //辗转相除法求最大公约数函数
void integer()        //整数运算
void fraction()            //真分数运算
char Operator()            //随机选取运算符
{ 
    char op;
    srand(time(0));
    int i=rand()%4+1; 
    switch(i) 
    { 
        case 1: return +; 
        case 2: return -; 
        case 3: return *; 
        case 4: return /; 
    } 
}
int maxNumber(int k, int l)            //辗转相除法求最大公约数函数
{
    while(k%l!=0){
        int t=k%l;
        k=l;
        l=t;
    }
    return l;
}
void integer()        //整数运算
{ 
    int g;
    int x,y; 
    char op; 
    srand(time(0));
    x=rand()%100+1; 
    y=rand()%100+1; 
    op=Operator(); 
    cout<<x<<op<<y<<"="<<endl;
    cout<<"请输入运算结果:";
    if(op==+)            //整数“+”运算
    {        
        cin>>g;
        if(g==x+y)
        {
            cout<<"恭喜你答对了!"<<endl<<endl;
            correct++;
        }
        else
        {
            cout<<"很遗憾你答错了,正确答案是:"<<x+y<<endl<<endl;
            mistake++;
        }
    }
    else 
        if(op==-)            //整数“-”运算
    {        
        cin>>g;
        if(g==x-y)
        {
            cout<<"恭喜你答对了!"<<endl<<endl;
            correct++;
        }
        else
        {
            cout<<"很遗憾你答错了,正确答案是:"<<x-y<<endl<<endl;
            mistake++;
        }
    }
        else 
            if(op==*)            //整数“*”运算
    {        
        cin>>g;
        if(g==x*y)
        {
            cout<<"恭喜你答对了!"<<endl<<endl;
            correct++;
        }
        else
        {
            cout<<"很遗憾你答错了,正确答案是:"<<x*y<<endl<<endl;
            mistake++;
        }
    }
        else 
            if(op==/)            //整数“/”运算
    {        
        cin>>result;
        element=x;
        denominator=y;
        maxNum=maxNumber(element,denominator);
        element=element/maxNum;
        denominator=denominator/maxNum;
        if(denominator!=1)
        {

            sprintf_s(answer, "%d/%d",element ,denominator);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
        else
        {
            sprintf_s(answer, "%d",element);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
    }
} 
void fraction()            //真分数运算
{ 
    srand(time(NULL));
    memset(result,0, sizeof(result));
    memset(answer,0, sizeof(answer));
    int a,b,c,d; 
    a=rand()%100+1; 
    b=rand()%100+1; 
    c=rand()%100+1; 
    d=rand()%100+1; 
    char op; 
    while(a<=b||c<=d) 
    { 
        a=rand()%100+1; 
        b=rand()%100+1; 
        c=rand()%100+1; 
        d=rand()%100+1; 
    } 
    op=Operator(); 
    cout<<"("<<b<<"/"<<a<<")"<<op<<"("<<d<<"/"<<c<<")"<<"="<<endl;
    cout<<"请输入运算结果:";
    cin>>result;
    if(op==+)            //分数“+”运算
    {        
        element=(b*c)+(a*d);
        denominator=a*c;
        maxNum=maxNumber(element,denominator);
        element=element/maxNum;
        denominator=denominator/maxNum;
        if(denominator!=1)
        {
            sprintf_s(answer, "%d/%d",element ,denominator);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
        else
        {
            sprintf_s(answer, "%d",element);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
    }
    else 
        if(op==-)            //分数“-”运算
    {        
        element=(b*c)-(a*d);
        denominator=a*c;
        maxNum=maxNumber(element,denominator);
        element=element/maxNum;
        denominator=denominator/maxNum;
        if(denominator!=1)
        {

            sprintf_s(answer, "%d/%d",element ,denominator);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
        else
        {
            sprintf_s(answer, "%d",element);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
    }
        else 
            if(op==*)            //分数“*”运算
    {        
        element=b*d;
        denominator=a*c;
        maxNum=maxNumber(element,denominator);
        element=element/maxNum;
        denominator=denominator/maxNum;
        if(denominator!=1)
        {
            sprintf_s(answer, "%d/%d",element ,denominator);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
        else
        {
            sprintf_s(answer, "%d",element);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
    }
        else 
            if(op==/)            //分数“/”运算
    {    
        element=b*c;
        denominator=a*d;
        maxNum=maxNumber(element,denominator);
        element=element/maxNum;
        denominator=denominator/maxNum;
        if(denominator!=1)
        {

            sprintf_s(answer, "%d/%d",element ,denominator);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
        else
        {
            sprintf_s(answer, "%d",element);
            if (strcmp(answer, result) == 0)
            {
                cout<<"恭喜你答对了!"<<endl<<endl;
                correct++;
            }
            else
            {
                cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl;
                mistake++;
            }
        }
    }
} 

测试运行:

技术分享

 

PSP展示:

 |||||
|:--|:--|:--|:--|
|**PSP2.1**|**Personal Software Process Stages**|**Time (%) Senior Student(/hour)**|**Time (%)(/hour)**|
|· Planning|计划|2|1.5|
|· Estimate|估计这个任务需要多少时间|38|49|
|· Analysis|需求分析 (包括学习新技术)|2|2.5|
|· Coding Standard|代码规范|0.5|0.5|
|· Design|具体设计|1.5|2|
|· Coding|具体编码|30|40|
|· Test|测试(自我测试,修改代码,提交修改)|1|1|
|Reporting|报告|1|1.5| 

 

四则运算题目生成程序(基于控制台)

标签:images   standard   srand   ems   lan   时间   实现   统计   operator   

原文地址:http://www.cnblogs.com/cxx111/p/6509436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!