# 第二次作业：熟悉使用工具

## 熟悉使用工具

GIT地址 (https://github.com/77777776/Calculator.git)
GIT用户名 77777776

vs2019（版本感觉都无所谓）

• 1.用rand函数生成运算符，并写成函数
• 2.先用rand函数生成1~100的随机数
• 3.调用运算符函数以及数字 ，然后输出题目

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
char operate() {
char op;
int a = rand() % 4 + 1;
switch (a) {
case 1:op = '+'; break;
case 2:op = '-'; break;
case 3:op = '/'; break;
case 4:op = '*'; break;
}
return op;
}//生成运算符
void calculate(int b)
{
int f;
char p1 = operate();
char p2 = operate();
int e[3];
for (int i = 0; i <= 2; i++)
{
e[i] = rand() % 100 + 1;
}
if (p1 == '+' && p2 == '+')//                                    +/////////
{
cout << e[0] << "+" << e[1] << "+" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] + e[1] + e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] + e[1] + e[2] << endl;
}
}
else if (p1 == '+' && p2 == '-') {
if (e[0] + e[1] - e[2] < 0)
{
while (e[0] + e[1] - e[2] < 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "+" << e[1] << "-" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] + e[1] - e[2])
cout << "(正确)" << endl;
else cout << "错误    正解:" << e[0] + e[1] - e[2] << endl;
}
}
else if (p1 == '+' && p2 == '*')
{
cout << e[0] << "+" << e[1] << "*" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] + e[1] * e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] + e[1] * e[2] << endl;
}
}
else if (p1 == '+' && p2 == '/')
{
if (e[1] % e[2] != 0)
{
while (e[1] % e[2] != 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "+" << e[1] << "/" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] + e[1] / e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] + e[1] / e[2] << endl;
}

}
else if (p1 == '-' && p2 == '-')///                               -////////////
{
if (e[0] - e[1] - e[2] < 0)
{
while (e[0] - e[1] - e[2] < 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "-" << e[1] << "-" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] - e[1] - e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] - e[1] - e[2] << endl;
}
}
else if (p1 == '-' && p2 == '+')
{
if (e[0] - e[1] + e[2] < 0)
{
while (e[0] - e[1] + e[2] < 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "-" << e[1] << "+" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] - e[1] + e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] - e[1] + e[2] << endl;
}
}
else if (p1 == '-' && p2 == '*')
{
if (e[0] - e[1] * e[2] < 0)
{
while (e[0] - e[1] * e[2] < 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "-" << e[1] << "*" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] - e[1] * e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] - e[1] * e[2] << endl;
}
}
else if (p1 == '-' && p2 == '/')
{
if (e[0] - e[1] / e[2] < 0 || e[1] % e[2] != 0)
{
while (e[0] - e[1] + e[2] < 0 || e[1] % e[2] != 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "-" << e[1] << "/" << e[2] << "="; if (b==1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] - e[1] / e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] - e[1] / e[2] << endl;
}
}
else if (p1 == '*' && p2 == '+')//                      *////////
{
cout << e[0] << "*" << e[1] << "+" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] * e[1] + e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] * e[1] + e[2] << endl;
}
}
else if (p1 == '*' && p2 == '-')
{
if (e[0] * e[1] - e[2] < 0)
{
while (e[0] * e[1] - e[2] < 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "*" << e[1] << "-" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] * e[1] - e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] * e[1] - e[2] << endl;
}
}
else if (p1 == '*' && p2 == '*')
{
cout << e[0] << "*" << e[1] << "*" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] * e[1] * e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] * e[1] * e[2] << endl;
}
}
else if (p1 == '*' && p2 == '/')
{

if ((e[0] * e[1]) % e[2] != 0)
{
while ((e[0] * e[1]) % e[2] != 0)
{
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
}
cout << e[0] << "*" << e[1] << "/" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] * e[1] / e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] * e[1] / e[2] << endl;
}
}
else if (p1 == '/' && p2 == '+')//                        除/////////////
{
if (e[0] % e[1] != 0)
{
while (e[0] % e[1] != 0)
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
cout << e[0] << "/" << e[1] << "+" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] / e[1] + e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] / e[1] + e[2] << endl;
}
}
else if (p1 == '/' && p2 == '-')
{
if (e[0] % e[1] != 0 && e[0] / e[1] - e[2] < 0)
{
while (e[0] % e[1] != 0 || e[0] / e[1] - e[2] < 0)
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
cout << e[0] << "/" << e[1] << "+" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] / e[1] + e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] / e[1] + e[2] << endl;
}
}
else if (p1 == '/' && p2 == '*')
{
if (e[0] % e[1] != 0)
{
while (e[0] % e[1] != 0)
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
cout << e[0] << "/" << e[1] << "*" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] / e[1] * e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] / e[1] * e[2] << endl;
}
}
else if (p1 == '/' && p2 == '/')
{
if (e[0] % e[1] != 0)
{
while (e[0] % e[1] != 0 || (e[0] / e[1]) % e[2] != 0)
for (int i = 0; i <= 2; i++) { e[i] = rand() % 100 + 1; }
}
cout << e[0] << "/" << e[1] << "/" << e[2] << "="; if (b == 1) cout << endl;
if (b == 2) {
cin >> f;
if (f == e[0] / e[1] / e[2])
cout << "(正确)" << endl;
else cout << "错误   正解:" << e[0] / e[1] / e[2] << endl;
}
}
}//生成算式
int main()
{
int x, y;
char s ='y';
srand(time(NULL));
while(s=='y')
{
cout << "输入题目数量:";
cin >> x;
cout << "仅输出题目 扣1    输入题目同时解答 扣2" << endl;
cin >> y;
for (int j = 1; j <= x; j++) { calculate(y); }
cout << "---------是否需要继续出题-----------" << endl;
cout << "         是 按y;  否 按n"<<endl;
cin >> s;
}
return  0;
}


wdnmd,我只想说，，，，，我太难了。

posted @ 2019-09-22 22:42  牙齿白的  阅读(208)  评论(1编辑  收藏  举报