用C语言编程随机产生10道包含加减乘除的数学题,输入答案之后能够返回答案是否正确,10道题完成的时候能够得到错误几道正确几道并且从屏幕输出
// c_rand_num_add.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "iostream"
using namespace std;
#define BIG_NUM 10 //最大的整数
#define TITLE_NUM 10
int right_num;
void rand_num(int *a, int *b){
*a=abs(rand()%BIG_NUM);
*b=abs(rand()%BIG_NUM);
}
int rand_rule(){
/* 0=> +
1=> -
2=> *
3=> /
*/
int way;
way=abs(rand()%4);
return way;
}
void out(int a,int b,int rule){
int c;
switch(rule)
{
case 0:{
cout<<a<<" + "<<b<<" = ";
cin>>c;
if (c==(a+b))
right_num++;
break;
}
case 1:{
cout<<a<<" - "<<b<<" = ";
cin>>c;
if (c==(a-b))
right_num++;
break;
}
case 2:{
cout<<a<<" * "<<b<<" = ";
cin>>c;
if (c==(a*b))
right_num++;
break;
}
case 3:{
cout<<a<<" / "<<b<<" = ";
cin>>c;
if (c==(a/b))
right_num++;
break;
}
default:
cout<<"The rule""<<rule<< "" is wrong"<<endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a,b,rule,i;
right_num=0;
for(i=0;i<TITLE_NUM;i++){
rand_num(&a,&b);//产生随机整数
rule=rand_rule();//产生随机运算法则
out(a,b,rule);
}
cout<<"正确的数目:"<<right_num<<"n错误的数目:"<<TITLE_NUM-right_num<<endl;
system("pause");
return 0;
}
https://blog.sciencenet.cn/blog-464042-558533.html
上一篇:
C语言产生随机整数下一篇:
相当好用的PDF解密软件