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

实验1-四则运算

时间:2015-04-09 19:38:01      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<time.h>
int main()
{
 int a,b,ans,res,c1=0,c2=0,c3=0,c4=0,t,i,N;
 int p;
 srand((unsigned)time(NULL));
 while(1)
 {
  cout<<"\n--小学生四则运算练习题--\n";
  cout<<"--1.加法运算--\n";
        cout<<"--2.减法运算--\n";
        cout<<"--3.乘法运算--\n";
        cout<<"--4.除法运算--\n";
        cout<<"--5.退出练习--\n";
        cout<<"--请输入数字1-5:--\n";
     cin>>p;
  switch(p)
  {
  case 1:
      cout<<"--请进行加法运算--\n";
      cout<<"--请输入题目个数--\n";
   cin>>N;
      for(i=1;i<=N;i++)
   {
    a=rand()%99+1;// rand()%a的结果最大为a-1*
       b=rand()%99+1;
       res=a+b;
          cout<<a<<"+"<<b<<"=";
          cin>>ans;
        if(ans==res)
    {
    cout<<"你答对了!\n";
    c1++;
    }
       else
    {
      cout<<"你答错了!\n";
      cout<<"正确答案是:"<<res<<endl;
    }
   }
  
   break;
       case 2:
     cout<<"--请进行减法运算--\n";
     cout<<"--请输入题目个数--\n";
     cin>>N;
     for(i=1;i<=N;i++)
     {
      a=rand()%99+1;
      b=rand()%99+1;
      if(a<b)
 
      {
       t=a;a=b;b=t;
      }
        res=a-b;
       cout<<a<<"-"<<b<<"=";
          cin>>ans;
       if(ans==res)
    {
     cout<<"你答对了!\n";
     c2++;
    }
    else
    {
     cout<<"你答错了!"<<endl;
     cout<<"正确答案是:"<<res<<endl;
    }
     }
           
     break;
    case 3:
      cout<<"--请进行乘法运算--\n";
      cout<<"--请输入题目个数--\n";
         cin>>N;
     for(i=1;i<=N;i++)
     {
      a=rand()%99+1;
      b=rand()%99+1;
      res=a*b;
      cout<<a<<"*"<<b<<"=";
      cin>>ans;
      if(ans==res)
      {
       cout<<"你答对了!\n";
       c3++;
      }
      else
    {
     cout<<"你答错了!"<<endl;
     cout<<"正确答案是:"<<res<<endl;
    }
     }
      
     break;
    case 4:
      cout<<"--请进行除法运算--\n";
      cout<<"--请输入题目个数--\n";
      cin>>N;
     for(i=1;i<=N;i++)
     {
      a=rand()%99+1;
      b=rand()%99+1;
      res=a/b;
      cout<<a<<"/"<<b<<"=";
      cin>>ans;
      if(ans==res)
      {
       cout<<"你答对了!\n";
       c4++;
      }
      else
    {
     cout<<"你答错了!"<<endl;
     cout<<"正确答案是:"<<res<<endl;
    }
     }
    
     break;
    case 5:goto END;
       }
 }
END:cout<<"再见!\n";
return 0;
}

 

实验1-四则运算

标签:

原文地址:http://www.cnblogs.com/10Watson/p/4410476.html

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