第二次作业:熟悉使用工具
熟悉使用工具
GIT地址 | (https://github.com/77777776/Calculator.git) |
GIT用户名 | 77777776 |
学号后5位 | 61221 |
博客地址 | https://www.cnblogs.com/3093105614qq/ |
作业链接 | http://daohang.qq.com/?fr=favstart |
环境配置过程: | |
下载git,我用班上同学博客里的链接下的,速度快 | |
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,我只想说,,,,,我太难了。
感觉过程较多,作业完成情况较差。而自己对GitHub的认识了解不够,导致完成情况一度延后。经过这次作业,发现自己主动获取信息知识的能力不强,单元测试和效能测试没有完成,想去做但感觉能力不够,代码感觉也并不太完美,有问题的地方太多。看来只有以后抽时间补好这一课,自己要多去熟悉GitHub,和Git。