两位数的加减法

来源:互联网 发布:铜陵网络台美食来啦 编辑:程序博客网 时间:2024/04/29 15:25

 #include "iostream.h"
#include "stdlib.h";
#include "time.h";

int main()
{
 int a,b,num_good=0,num_bad=0;
 char yn;
 srand(time(NULL));
 cout<<"***欢迎你来做两位数的加减法***"<<endl;
 do
 {
  int temp=rand()%2;
  a=rand()%100;
  b=rand()%100;
  cout<<temp<<"  (0表示加1表示减)"<<endl;
  switch (temp)
  {
  case 0:
   {
    int ans1;
    cout<<a<<"+"<<b<<"=";
    cin>>ans1;
    if(a+b==ans1)
    {
     num_good++;
     cout<<"恭喜,答对了!"<<endl;
    }
    else
    {
     cout<<"答错了!"<<endl;
     num_bad++;
    }
    break;
   }
  case 1:
   {
    int ans;
    cout<<a<<"-"<<b<<"=";
    cin>>ans;
    if(a-b==ans)
    {
     cout<<"恭喜,答对了!"<<endl;
     num_good++;
    }
    else
    {
     cout<<"答错了!"<<endl;
     num_bad++;
    }
   }
  }
  cout<<"你还要再做一题吗?(N退出)"<<endl;
  cin>>yn;
 }while(yn!='n' && yn!='N');

 cout<<"你答题的正确率为"<<float(num_good)/(num_bad+num_good)*100<<"%"<<endl;
 cout<<num_good<<"          "<<num_bad<<endl;
 return 0;
}

 

==========================

本例中,重点需要理解DO-WHILE;SWITCH-CASE的用法;了解怎么产生随机数(使用求模的方法);

原创粉丝点击