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

四则运算3

时间:2015-03-21 22:53:36      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

题目:在四则运算2的基础上添加如下功能:让程序能接受用户输入答案,并判定对错。最后给出总共对/错的数量。

一、设计思路

在四则运算2的基础上,添加了接收用户输入答案的函数,存入数组,当遇到运算是除法时,若用户选择有余数,则除法运算需要接收两个答案,一个为商,一个为余数。

当商和余数都和答案相同时,此题才判断为对。

二、源代码

#include<iostream.h>
#include<stdlib.h>
int a[100],b[100],c[100];             //两个数和运算符
int answer[100]={0};                  //答案
int huida[100]={0};                   //用户输入的答案
int huiday[100]={0};                  //用户输入的余数
int y[100]={0};                       //正确的余数
int i;
void Input(int timu,int yushu)        //用户输入函数
{
     int k;
     int rcount=0;                     //正确题数
     int wcount=0;                     //错误题数
     cout<<"请输入答案:"<<endl;
     for(k=0;k<timu;k++)
     {
          if(c[k]==3&&yushu==0)         //有余数除法则先输入商再输入余数
          {
               cout<<k+1<<". 商为:";
               cin>>huida[k];
               cout<<"余数为:";
               cin>>huiday[k];
          }
          else
      {
               cout<<k+1<<". ";
               cin>>huida[k];
          }
     }
     for(k=0;k<timu;k++)
     {
          if(c[k]==3&&yushu==0)        //有余数除法,商和余数都正确才正确
          {
               if(huida[k]==answer[k]&&huiday[k]==y[k])
               {
                   rcount++;
               }
               else
               {
                   cout<<"第"<<k+1<<"道题出错;"<<endl;
                   wcount++;
               }
          }
          else
          {
               if(huida[k]==answer[k])
               {
                   rcount++;
               }
               else
               {
                   cout<<"第"<<k+1<<"道题出错;"<<endl;
                   wcount++;
               }
          }
     }
     cout<<"答对"<<rcount<<"道题,"<<"答错"<<wcount<<"道题。"<<endl;
}
void Display(int f,int yushu)                               //输出题目的函数
{
     if(f==0)                                                //判断打印要求
     {
          if(c[i]==0)
          {
              cout<<i+1<<". "<<a[i]<<‘+‘<<b[i]<<‘=‘<<endl;
              answer[i]=a[i]+b[i];
          }
          if(c[i]==1)
          {
              cout<<i+1<<". "<<a[i]<<‘-‘<<b[i]<<‘=‘<<endl;
              answer[i]=a[i]-b[i];
          }
          if(c[i]==2)
          {
              cout<<i+1<<". "<<a[i]<<‘*‘<<b[i]<<‘=‘<<endl;
              answer[i]=a[i]*b[i];
           }
           if(c[i]==3)
           {
               cout<<i+1<<". "<<a[i]<<‘/‘<<b[i]<<‘=‘<<endl;
               answer[i]=a[i]/b[i];
               if(yushu==1)
               {         
                   y[i]=a[i]%b[i];
               }
            }

     }
     else
     { 
          if(c[i]==0)
          {
              cout<<i+1<<". "<<a[i]<<‘+‘<<b[i]<<‘=‘<<‘\t‘;
              answer[i]=a[i]+b[i];
          }
          if(c[i]==1)
          {
              cout<<i+1<<". "<<a[i]<<‘-‘<<b[i]<<‘=‘<<‘\t‘;
              answer[i]=a[i]-b[i];
          }
          if(c[i]==2)
          {
              cout<<i+1<<". "<<a[i]<<‘*‘<<b[i]<<‘=‘<<‘\t‘;
              answer[i]=a[i]*b[i];
          }
          if(c[i]==3)
          {
              cout<<i+1<<". "<<a[i]<<‘/‘<<b[i]<<‘=‘<<‘\t‘;
              answer[i]=a[i]/b[i];
              if(yushu==1)
              {
                  y[i]=a[i]%b[i];
              }
          }
          if((i+1)%f==0)
              cout<<endl;
     }
}
void Carry()                                             //主要执行函数
{
     int j,t;
     int cc,fw,zf,ys,tm,dy,an;
     cout<<"请选择是否有乘除法:(0为是,1为否)"<<endl;
     cin>>cc;
     if(cc!=0&&cc!=1)
     {
         cout<<"输入错误,请重新输入!"<<endl;
         Carry();
         exit(0);
     }
     cout<<"请输入取值范围:(0-?)"<<endl;
     cin>>fw;
     cout<<"请选择加减有无负数:(0为有,1为无)"<<endl;
     cin>>zf;
     if(zf!=0&&zf!=1)
     {
          cout<<"输入错误,请重新输入!"<<endl;
          Carry();
          exit(0);
     }  
     if(cc==0)
     {
          cout<<"请选择除法有无余数:(0为有,1为无)"<<endl;
          cin>>ys;
          if(ys!=0&&ys!=1)
          {
               cout<<"输入错误,请重新输入!"<<endl;
               Carry();
               exit(0);
          }
     }
     cout<<"请输入题目个数:"<<endl;
     cin>>tm;
     if(tm<=0)
     {
          cout<<"输入错误,请重新输入!"<<endl;
          Carry();
          exit(0);
     }
     cout<<"请选择打印方式:(0为逐行打印,输入其他数字则为该行几道题)"<<endl;
     cin>>dy;
     if(dy<0)
     {
          cout<<"输入错误,请重新输入!"<<endl;
          Carry();
          exit(0);
     }
     for(i=0;i<tm;i++)
     {
          a[i]=rand()%fw;
          b[i]=rand()%fw;
          c[i]=rand()%4;
          for(j=0;j<i;j++)                               //避免重复
               if(a[j]==a[i]&&b[j]==b[i]&&c[j]==c[i])
                    a[i]++;
          if(c[i]==3&&b[i]==0)
               b[i]++;
          if(cc==0&&zf==0&&ys==0)
          {
              Display(dy,ys);
          }

          else if(cc==1)                                 //无乘除法
         {
              if(c[i]>=2)
              c[i]=c[i]-2;
              Display(dy,ys);
         }
         else if(cc==0&&zf==1&&ys==0)
         {      
              if(a[i]<b[i])                              //无负数
              {
                  t=a[i];
                  a[i]=b[i];
                  b[i]=t;
              }
              Display(dy,ys);
          }
          else
         {
              if(c[i]==3)
              {
                  if(a[i]<b[i])
                  {
                      t=a[i];
                      a[i]=b[i];
                      b[i]=t;
                  }
                  if(a[i]%b[i]!=0)                       //无余数,能够整除
                  {
                       a[i]=b[i]*(rand()%10);
                  }
              }
              Display(dy,ys);
          }
     }
     cout<<"请输入是否要输入答案:(0为是,其他为否)"<<endl;
     cin>>an;
     if(an==0)
         Input(tm,ys);
     else
         exit(0);
}
void main()
{
     Carry();
}

三、运行结果

技术分享技术分享

四、遇到错误

      相同类型,相同参数的函数不能有两个,除法运算有余数时需要有两个答案,一个为商,一个为余数,没有余数时,则只需输入一个答案。

四则运算3

标签:

原文地址:http://www.cnblogs.com/benboerba/p/4356359.html

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