个人作业1-四则运算题目生成程序

时间:2022-05-09 21:56:52

题目描述:

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

需求分析:

1.自动生成四则运算题目,支持整数,真分数运算
2.支持真分数化简
3.能判断答案对错,给出正确率

Coding代码地址

https://coding.net/u/zjp201421123057/p/zjp/git/blob/master/四则运算.cpp

程序设计:

本程序基于C++程序语言实现,通过定义Int()(整数),Fraction()(真分数),Divisor()(化简)来实现运算,通过srand((unsigned)time(NULL)来随机生成题目,Check()检查答案对错,给出正确率来实现本程序。

源代码

最大公约数化简:

int Divisor (int a,int b)    
{
  int  t;          
  if(a<b)             
  { 
      t=a;
      a=b;
      b=t;
    } 
   while(b!=0)           
   {
      t=a%b;
      a=b;              
      b=t;
    }
  return a;             
}

运算:

void Int()             
{
    int a,b,c,t;
    char result[10];
    srand((unsigned)time(NULL));
    a = rand() % 10;
    b = rand() % 10+1;
    c = rand()%4;
    switch(c)
    {
        case 0 :
            cout<<a<<" + "<<b<<"=";
            t = a + b;
            sprintf(result, "%d", t);
            break;              
        case 1 :
            cout<<a<<" - "<<b<<"=";
            t = a - b;
            sprintf(result, "%d", t);
            break;          
        case 2 :
            cout<<a<<" * "<<b<<"=";
            t = a * b;
            sprintf(result, "%d", t);
            break;          
        case 3 :
            cout<<a<<" ÷"<<b<<"=";
            if(a%b==0)
            {
                t = a / b;
                sprintf(result, "%d", t);
            }
            else
            {
                t = Divisor(a,b);
                a = a/t;
                b = b/t;
                sprintf(result, "%d/%d", a, b);
            }
            break;              
    }
    Check(result) ;     
}
void Fraction()             
{   
    int a,b,c,d,x,y,z,t;
    char result[10];
    srand((unsigned)time(NULL));
    a = rand() % 10;
    b = rand() % 10+1;
    c = rand() % 10;
    d = rand() % 10+1;
    z = rand() % 4;
    if(a>b)
    {
        t=a;
        a=b;
        b=t;
    }    
    if(c>d)
    {
        t=c;
        c=d;
        d=t;
    }    
    switch(z)
    {
        case 0 :
            cout<<a<<"/"<<b<<" + "<<c<<"/"<<d<<"=";
            x = a*d+b*c;
            y = b*d;
            t = Divisor(x,y);
            x = x/t;
            y = y/t;
            if(y==1)
            {
                sprintf(result, "%d", x);
            }
            else
            { 
                sprintf(result, "%d/%d", x, y);   
            }               
            break;
        case 1:     
            cout<<a<<"/"<<b<<" - "<<c<<"/"<<d<<"=";
            if(a*d<b*c)
            {
                t = a;
                a = c;
                c = t;
                t = b;
                b = d;
                d = t;
            }
            x = a*d-b*c;
            y = b*d;
            t = Divisor(x,y);
            x = x/t;
            y = y/t;
            sprintf(result, "%d/%d", x, y);                                 
            break;
        case 2 :
            cout<<a<<"/"<<b<<" * "<<c<<"/"<<d<<"=";
            if(a==0||c==0)
            {
                t = 0;
                sprintf(result, "%d", t);
            }
            else
            {
                x = a*c;
                y = b*d;
                t = Divisor(x,y);
                x = x/t;
                y = y/t;
                sprintf(result, "%d/%d", x, y);
            }                                   
            break;
        case 3 :
            cout<<a<<"/"<<b<<" ÷"<<c<<"/"<<d<<"=";
            if((a*d)%(b*c)==0)
            {
                x = a*d;
                y = b*c;
                t = x/y; 
                sprintf(result, "%d", t);
            }
            else
            {
                x = a*d;
                y = b*c;
                t = Divisor(x,y);
                x = x/t;
                y = y/t;
                if(y==1)
                {
                    sprintf(result, "%d", x);
                }
                else
                { 
                    sprintf(result, "%d/%d", x, y);   
                }
            }                                   
            break;
    }   
    Check(result);
}

程序实现:

个人作业1-四则运算题目生成程序

PSP:

个人作业1-四则运算题目生成程序

个人小结:

通过本程序的编写,反映出本人的编程能力实在太弱。平时写代码也写得少,思路不清晰,不知从哪里下手。本次的编程也是在同学的帮助指导下完成得马马虎虎。我意识到了自己的编程量太少,以后要多加练习写代码,提高编程能力。