//420128040112xxx
//完成日期:2022/05/09
//小学生四则运算练习系统
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int Add(); //声明加法函数
int Sub(); //声明减法函数
int Mul(); //声明乘法函数
int Div(); //声明除法函数
void eval(int right); //声明评价函数
int main() //主菜单
{
int c;
while(1)
{
cout << "============================" << endl;
cout << "1.加法\t\t2.减法\n" << endl;
cout << "3.乘法\t\t4.除法\n" << endl;
cout << "0.退出" << endl;
cout << "============================" << endl;
cout << "请选择:" << endl;
cin >> c;
if(!c) break; //c为0程序直接结束
switch(c) //能一直循环,除非c为0
{
case 1:Add(); break;//c为1选择加法运算
case 2:Sub(); break;//c为2选择减法运算
case 3:Mul(); break;//c为3选择乘法运算
case 4:Div(); break;//c为4选择除法运算
}
}
}
int Add() //加法运算
{
int x,y,answer,right = 0; //right是做对的题目数
srand(time(NULL)); //与rand()函数配套使用
for(int i = 0; i < 10; i++)
{
x = rand()%10 + 1; //rand()函数产生的是伪随机数,是按照公式算出来的
y = rand()%10 + 1; //生成1到10的随机数
cout << x << " + " << y << " = ";
cin >> answer;
if(answer == (x + y))
right++;
}
eval(right); //调用评价函数
}
int Sub() //减法运算
{
int x,y,answer,right = 0;
srand(time(NULL));
for(int i = 0; i < 10; i++)
{
x = rand()%10 + 1;
y = rand()%10 + 1;
if(x > y) //确保x与y相减的结果是正数
{
cout << x << " - " << y << " = ";
cin >> answer;
if(answer == (x - y))
right++;
}
else
{
cout << y << " - " << x << " = ";
cin >> answer;
if(answer == (y - x))
right++;
}
}
eval(right);
}
int Mul() //乘法运算
{
int x,y,answer,right = 0;
srand(time(NULL));
for(int i = 0; i < 10; i++)
{
x = rand()%10 + 1;
y = rand()%10 + 1;
cout << x << " * " << y << " = ";
cin >> answer;
if(answer == (x * y))
right++;
}
eval(right);
}
int Div() //除法运算
{
int x,y,answer,right = 0;
srand(time(NULL));
for(int i = 0; i < 10; i++)
{
x = rand()%9 + 1; //生成1到9的随机数
y = rand()%9 + 1;
cout << x*y << " ÷ " << y << " = "; //为了可以整除
cin >> answer;
if(answer == x)//因为是(x*y)÷y,所以只要比较answer和x是否相等即可
right++;
}
eval(right);
}
void eval(int right) //评价
{
right = right * 10; //得到总分
if(right == 100)
cout << "你好厉害哇,优秀!\n";
else if((right == 80) || (right == 90))
cout << "很不错啦,良好!\n";
else if((right == 60) || (right == 70))
cout << "你合格了!\n";
else
cout << "你怎么没及格啊,要再接再厉呀!加油!\n";
}

寥若晨星666
- 粉丝: 273
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
前往页