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

10以内的数字的四则运算

时间:2021-04-28 12:11:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:main   srand   pac   names   out   cti   bre   cas   switch   

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int num1,num2,op,result1,result2; //num1,num2:操作数;op:运算符;result1,result2:结果
do
{
srand(time(NULL));
num1=rand()*10/(RAND_MAX+1);
num2=rand()*10/(RAND_MAX+1); //生成运算数
op=rand()*4/(RAND_MAX+1); //生成运算符 0-- +,1-- -,2-- *,3-- /

switch (op)
{
case 0:
cout<<num1<<"+"<<num2<<"=? ";
cin>>result1;
if (num1+num2 == result1)
cout<<"you are right"<<endl;
else
cout<<"you are wrong"<<endl;
break;
case 1:
cout<<num1<<"-"<<num2<<"=? ";
cin>>result1;
if (num1-num2 == result1)
cout<<"you are right"<<endl;
else
cout<<"you are wrong"<<endl;
break;
case 2:
cout<<num1<<"*"<<num2<<"=? ";
cin>>result1;
if (num1*num2 == result1)
cout<<"you are right"<<endl;
else
cout<<"you are wrong"<<endl;
break;
case 3:
cout<<num1<<"/"<<num2<<"=? ";
cin>>result1;
cout<<"余数为=? ";
cin>>result2;
if ((num1/num2 == result1)&&(num1%num2==result2))
cout<<"you are right"<<endl;
else
cout<<"you are wrong"<<endl;
break;
}
}while(1);
return 0;

}

10以内的数字的四则运算

标签:main   srand   pac   names   out   cti   bre   cas   switch   

原文地址:https://www.cnblogs.com/davidhurq/p/14711558.html

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