四则运算

一:题目

让程序能接受用户输入答案,并判定对错。最后给出总共对/错 的数量。

二:设计思路

在每种结果后添加判断语句如果正确则输出回答正确,如果错误则输出回答错误,并定义两个变量来控制正确与错误的个数

三:设计代码

#include<iostream.h>
   #include<stdlib.h>
   #include<time.h>
   void zccc()
   {
      double daan;//输出用户答案    
      int a,b,c; //两位操作数和一位操作符  
      int e;
   int c1=0,c2=0;//统计对错个数
      srand(time(NULL));
      int n;//输出题目数目
   int x;//取值范围
      int f;//加减有无负数
      int l;//用来输入行间距
   int y;//除法有无余数
   cout<<"需要列出多少道数学题?"<<endl;
      cin>>n; 
   cout<<"请规定数值取值范围"<<endl;
   cin>>x;
      cout<<"加减有无负数?(0代表有负数,1代表无负数)"<<endl;
      cin>>f;
   cout<<"除法有无余数?(0代表有余数,1代表无余数)"<<endl;
      cin>>y;
   cout<<"每行需要打印几个算数题?"<<endl;
      cin>>e;
      cout<<"打印时行间距是多少?"<<endl;
      cin>>l;
      
      cout<<"********************************"<<endl;
      for(int i=0;i<n;i++)
      {
          a=rand()%x;
          b=rand()%x;
          c=rand()%4;
          if(c==0){
              cout<<a<<"+"<<b<<"=";
    cin>>daan;
     if(daan==a+b){
      cout<<"回答正确!"<<endl;
       c1++;
     }
     else {
      cout<<"回答错误!"<<endl;
       c2++;
    }}
    
    if(c==1){
              if(f==1)
              {
              if(a<b)
              
     {cout<<b<<"-"<<a<<"=";
     
      cin>>daan;
      if(daan==b-a)
      {
       cout<<"回答正确!"<<endl;
       c1++;
      }
      else {
                      cout<<"回答错误!"<<endl;
           c2++;
      }
     }
     else
     {
      cout<<a<<"-"<<b<<"=";
      cin>>daan;
      if(daan==a-b){
       cout<<"回答正确!"<<endl;
        c1++;}
      else 
      {
       cout<<"回答错误!"<<endl;
        c2++;
      }

      
     }
     }
              
              else if(f==0)
              {
              cout<<a<<"-"<<b<<"=";
     cin>>daan;
     if(daan==a-b){
      cout<<"回答正确!"<<endl;
       c1++;
     }
     else {
      cout<<"回答错误!"<<endl;
       c2++;
     }
              }
    }
          if(c==2){
              cout<<a<<"*"<<b<<"=";
    cin>>daan;
    if(daan==a*b)
    {
     cout<<"回答正确!"<<endl;
      c1++;
    }
    else
    {
     cout<<"回答错误!"<<endl;
      c2++;
    }}
              
    if(c==3){
              if(b!=0)
              {
      while((y==1)&&(a%b)!=0)
      {
     a=rand()%100;
      }

              cout<<a<<"/"<<b<<"=";
     cin>>daan;
     if(daan==a/b)
     {
      cout<<"回答正确!"<<endl;
       c1++;
     }
     else {
      cout<<"回答错误!"<<endl;
       c2++;
     }
              }
              else
              {
                  i=i-1;
              }
    }
              
          if((i+1)%e==0)
          {
              for(int d=0;d<=l;d++)
              {
                  cout<<endl;
              }
          }
          if((i+1)==n)
          {
              cout<<endl;
          }
     }
  cout<<"答对题目个数为:"<<c1<<" "<<"答错题目个数为:"<<c2<<endl;
  }

  
  void bzccc()
  {
      double daan; //输出用户答案 
      int e;//输入每行算数题的个数
   int c1=0,c2=0;//统计对错个数
      int a,b,c;   //两位操作数和一位操作符
      srand(time(NULL));
      int n;//输出题目数目
   int x;//取值范围
      int f;//加减有无负数
      int l;//用来输入行间距
      cout<<"需要列出多少道数学题"<<endl;
      cin>>n;
      cout<<"请规定数值取值范围"<<endl;
   cin>>x;
      cout<<"加减有无负数?(0代表有负数,1代表无负数)"<<endl;
      cin>>f;
      cout<<"每行需要打印几个算数题?"<<endl;
    cin>>e;
     cout<<"打印时行间距是多少?"<<endl;
     cin>>l;
     cout<<"********************************"<<endl;
     for(int i=0;i<n;i++)
     {
         a=rand()%x;
         b=rand()%x;
        c=rand()%2;
  if(c==0){
             cout<<a<<"+"<<b<<"=";
    cin>>daan;
     if(daan==a+b){
      cout<<"回答正确!"<<endl;
       c1++;
     }
     else {
      cout<<"回答错误!"<<endl;
       c2++;
  }}
             if(c==1){
             if(f==1)
             {
             if(a<b)
             {cout<<b<<"-"<<a<<"=";
     cin>>daan;
      if(daan==b-a)
       {
       cout<<"回答正确!"<<endl;
      c1++;
      }
      else {
                      cout<<"回答错误!"<<endl;
           c2++;
      }}
    else {
     cout<<a<<"-"<<b<<"=";
      cin>>daan;
      if(daan==a-b)
      {
       cout<<"回答正确!"<<endl;
       c1++;
      }
      else {
                      cout<<"回答错误!"<<endl;
           c2++;
      }
    }
    }
     else if(f==0)
             {
                 cout<<a<<"-"<<b<<"=";
      cin>>daan;
      if(daan==a-b)
      {
       cout<<"回答正确!"<<endl;
       c1++;
      }
      else {
                      cout<<"回答错误!"<<endl;
           c2++;
      }
             }
    }
             
         if((i+1)%e==0)
         {
             for(int d=0;d<=l;d++)
            {            
                 cout<<endl;
             }
         }
         if((i+1)==n)
         {
             cout<<endl;
         }
     }
  cout<<"答对题目个数为:"<<c1<<" "<<"答错题目个数为:"<<c2<<endl;
 }


 int main()
 {

     char ch;
     cout<<"是否支持乘除?(Y/N)"<<endl;
     cin>>ch;
     if(ch=='Y'||ch=='y')
     {
         zccc();    
         cout<<"********************************"<<endl;
     }
     
     if(ch=='N'||ch=='n')
     {
         bzccc();
        cout<<"********************************"<<endl;
     }     
     return 0;
}

四,截图

五,感想及体会

本次扩展比较简单,但是每倒题目必须回车才能输出答案是否正确,这样就没办法每行输出固定的题目数,也就是说每行只能输出一道题目,多次尝试也没能修改这个问题。

原文地址:https://www.cnblogs.com/LJT666/p/4388606.html