17074211张昊 计算与软件工程作业2

作业要求#

  • 分别能够实现小学一、二、三、四、五年级的四则运算要求, 逐步实现各个年级的难度
  • 要求能够通过输入来选择不同年级,每个年级还得区分难,中,易三个等级
  • 对于三、四、五年级需要支持括号与多个运算符
  • 程序支持判断对错及累计得分与时间
  • 一次可以出100道题目,而且不能重复(比如2+3 与 3+2 算重复的)
  • 充分发挥想象增加满足小学生数学检测需要的功能

代码#

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include <string.h>
#include<cstdlib>
using namespace std;
int a;
int ri = 0;//计算回答的正确率,定义全局变量 ri 来计算回答正确的题数
//divisor (int a,int b) 函数通过辗转相除法求分子和分母的最大公约数
int divisor (int a,int b)
{
  int  temp;
  if(a<b)
  {
      temp=a;
      a=b;
      b=temp;
    }
   while(b!=0)
   {
      temp=a%b;
      a=b;
      b=temp;
    }
  return a;
}
//CheckResult(char result[]) 函数来判断输入的答案是否正确
int CheckResult(char result[])
{
    char answer[10] ;
    cin>>answer;
    if(strcmp(answer,result)==0)
    {
        cout<<"回答正确"<<endl<<endl;
        ri++;
    }
    else
    {
        cout<<"回答错误,正确答案为"<<result<<endl<<endl;
    }
    return 0;
}
//integer() 函数来生成整数的四则运算
void integer()
{
    int x,y,z,temp;
    char result[10];
    srand(time(NULL));
    x = rand() % 10;
    y = rand() % 10+1;
    z = rand()%4;
    switch(z)
    {
        case 0 :
            cout<<x<<" + "<<y<<"=";
            temp = x + y;
            sprintf(result, "%d", temp);
            break;
        case 1 :
            if(x<y)
            {
                temp=x;
                x=y;
                y=temp;
            }
            cout<<x<<" - "<<y<<"=";
            temp = x - y;
            sprintf(result, "%d", temp);
            break;
        case 2 :
            cout<<x<<" * "<<y<<"=";
            temp = x * y;
            sprintf(result, "%d", temp);
            break;
        case 3 :
            cout<<x<<" ÷"<<y<<"=";
            if(x%y==0)
            {
                temp = x / y;
                sprintf(result, "%d", temp);
            }
            else
            {
                temp = divisor(x,y);
                x = x/temp;
                y = y/temp;
                sprintf(result, "%d/%d", x, y);
            }
            break;
    }
    CheckResult(result) ;
}
// fraction() 函数来生成分数的四则运算
void fraction()
{
    int a,b,c,d,x,y,z,temp;
    char result[10];
    srand(time(NULL));
    a = rand() % 10;
    b = rand() % 10+1;
    c = rand() % 10;
    d = rand() % 10+1;
    z = rand() % 4;
    if(a>b)
    {
        temp=a;
        a=b;
        b=temp;
    }
    if(c>d)
    {
        temp=c;
        c=d;
        d=temp;
    }
    switch(z)
    {
        case 0 :
            cout<<a<<"/"<<b<<" + "<<c<<"/"<<d<<"=";
            x = a*d+b*c;
            y = b*d;
            temp = divisor(x,y);
            x = x/temp;
            y = y/temp;
            if(y==1)
            {
                sprintf(result, "%d", x);
            }
            else
            {
                sprintf(result, "%d/%d", x, y);
            }
            break;
        case 1 :
            if(a*d<b*c)
            {
                temp = a;
                a = c;
                c = temp;
                temp = b;
                b = d;
                d = temp;
            }
            cout<<a<<"/"<<b<<" - "<<c<<"/"<<d<<"=";
            x = a*d-b*c;
            y = b*d;
            temp = divisor(x,y);
            x = x/temp;
            y = y/temp;
            sprintf(result, "%d/%d", x, y);
            break;
        case 2 :
            cout<<a<<"/"<<b<<" * "<<c<<"/"<<d<<"=";
            if(a==0||c==0)
            {
                temp = 0;
                sprintf(result, "%d", temp);
            }
            else
            {
                x = a*c;
                y = b*d;
                temp = divisor(x,y);
                x = x/temp;
                y = y/temp;
                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;
                temp = x / y;
                sprintf(result, "%d", temp);
            }
            else
            {
                x = a*d;
                y = b*c;
                temp = divisor(x,y);
                x = x/temp;
                y = y/temp;
                if(y==1)
                {
                    sprintf(result, "%d", x);
                }
                else
                {
                    sprintf(result, "%d/%d", x, y);
                }
            }
            break;
    }
    CheckResult(result);
}
int main()
{
    int n,q;
    cout<<"输入要生成的题数:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int a = rand()%2;
        switch(a)
        {
            case 0: integer();break;
            case 1: fraction();break;
        }
    }
    q=(ri*100)/n;
    cout<<"回答对了"<<ri<<"题"<<endl;
    cout<<"正确率为:"<<q<<"%"<<endl<<endl;
     clock_t start,finish;
    start=clock();
     
    cout << "花费的时间 " << endl;
 
    finish=clock();
 
    cout << finish-start   << "/" << CLOCKS_PER_SEC  << " (s) "<< endl;
    system ("pause");
}

代码:https://gitee.com/zhang_hao17074211/learngit/blob/master/17074211zuoye2

代码运行:#

posted @ 2020-02-29 14:03  17数31张昊  阅读(129)  评论(1编辑  收藏  举报