博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四则运算的代码的改进(三)
阅读量:5153 次
发布时间:2019-06-13

本文共 8043 字,大约阅读时间需要 26 分钟。

题目:在原基础上能够输入答案判断对错,并且输出正确答案与错误答案的数目:

 

源代码:

#include
#include
using namespace std;float suan(float o1, char oc, float o2){ float r; switch (oc) { case '+': r = o1 + o2; break; case '-': r = o1 - o2; break; case '*': r = o1 * o2; break; case '/': r = o1 / o2; r = int((r + 0.5) * 100) / 100.0; break; } return r;}void main(){ int i, k, q, j, v, h, l, hl, d = 0, c = 0; //定义几个变量 float *o1, *o2, *o3, *r1, *r2; char *oc1, *oc2; char n1, n2; int a1, a2; char g[4] = { '+', '-', '*', '/' }; char y[5][5] = { ' ', '+', '-', '*', '/', '+', '=', '=', '<', '<', '-', '=', '=', '<', '<', '*', '>', '>', '=', '=', '/', '>', '>', '=', '=' }; srand((unsigned)time(NULL)); //本地时间b: cout << "请问您想输出多少道题目?" << endl; cin >> q; cout << "请输入操作数值的最大范围:" << endl; cin >> j; cout << "请问是否有无乘除法?(Y/N)" << endl; cin >> n1; cout << "请问是否有无括号?(Y/N)" << endl; cin >> n2; cout << "请输入每行运算式的个数:" << endl; cin >> v; cout << "请输入每行运算式之间的间隔:" << endl; cin >> a2; o1 = new float[q]; o2 = new float[q]; o3 = new float[q]; r1 = new float[q]; r2 = new float[q]; oc1 = new char[q]; oc2 = new char[q]; cout << "以下为随机选取的题目:" << endl << endl; for (i = 1; i <= q; i++) //定义一个可以改变题目数量的q { if (n1 == 'Y') { oc1[i] = g[rand() % 4]; oc2[i] = g[rand() % 4]; o1[i] = rand() % j; o2[i] = rand() % j; o3[i] = rand() % j; if (i == 1) //直接输出第一题 { if (n2 == 'N') //无括号 { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; for (hl = 1; hl < 5; hl++) { if (oc1[i] == y[hl][0]) { h = hl; } if (oc2[i] == y[0][hl]) { l = hl; } } switch (y[h][l]) { case '>': r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); break; case '=': r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); break; case '<': r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); break; } } else //有括号 { if (rand() % 2 == 0) //括号在前 { if (oc1[i] == '+' || oc1[i] == '-') { if (oc2[i] == '*' || oc2[i] == '/') { cout << '(' << o1[i] << oc1[i] << o2[i] << ')' << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } else { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } } if (oc1[i] == '*' || oc1[i] == '/') { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } } else //括号在后 { if (oc1[i] == '+' || oc1[i] == '-') { if (oc2[i] == '+' || oc2[i] == '-') { cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } else { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } } if (oc1[i] == '*' || oc1[i] == '/') { cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } } } } else { for (k = 0; k < i; k++) //避免重复start { if (o1[k] == o1[i] && o2[k] == o2[i] && o3[k] == o3[i] && oc1[k] == oc1[i] && oc2[k] == oc2[i]) { i--; break; } else { if (k == i - 1) //比较到最后一个数时 { //循环输出/*q*/道运算题 if (n2 == 'N') //无括号 { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; for (hl = 1; hl < 5; hl++) { if (oc1[i] == y[hl][0]) { h = hl; } if (oc2[i] == y[0][hl]) { l = hl; } } switch (y[h][l]) { case '>': r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); break; case '=': r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); break; case '<': r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); break; } } else //有括号 { if (rand() % 2 == 0) //括号在前 { if (oc1[i] == '+' || oc1[i] == '-') { if (oc2[i] == '*' || oc2[i] == '/') { cout << '(' << o1[i] << oc1[i] << o2[i] << ')' << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } else { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } } if (oc1[i] == '*' || oc1[i] == '/') { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } } else //括号在后 { if (oc1[i] == '+' || oc1[i] == '-') { if (oc2[i] == '+' || oc2[i] == '-') { cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } else { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } } if (oc1[i] == '*' || oc1[i] == '/') { cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } } } } } } //避免重复end } } else if (n1 == 'N') { oc1[i] = g[rand() % 2]; oc2[i] = g[rand() % 2]; o1[i] = rand() % j; o2[i] = rand() % j; o3[i] = rand() % j; if (i == 1) { if (n2 == 'N') { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } else { if (rand() % 2 == 0) { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } else { cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } } } else { for (k = 0; k < i; k++) //避免重复start { if (o1[k] == o1[i] && o2[k] == o2[i] && o3[k] == o3[i] && oc1[k] == oc1[i] && oc2[k] == oc2[i]) { i--; break; } else { if (k == i - 1) //比较到最后一个数时 { //循环输出/*q*/道运算题 if (n2 == 'N') { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } else { if (rand() % 2 == 0) { cout << o1[i] << oc1[i] << o2[i] << oc2[i] << o3[i] << "=\t"; r1[i] = suan(suan(o1[i], oc1[i], o2[i]), oc2[i], o3[i]); } else { cout << o1[i] << oc1[i] << '(' << o2[i] << oc2[i] << o3[i] << ')' << "=\t"; r1[i] = suan(o1[i], oc1[i], suan(o2[i], oc2[i], o3[i])); } } } } } //避免 重复end } } if (i % v == 0) { for (a1 = 1; a1 <= a2; a1++) { cout << endl; } } } cout << endl << "请输入答案:" << endl; for (i = 1; i <= q; i++) { cin >> r2[i]; } cout << endl << "以下是正确答案:" << endl; for (i = 1; i <= q; i++) { cout << r1[i] << "\t"; if (i % v == 0) { cout << endl; } } for (i = 1; i <= q; i++) { if (r1[i] == r2[i]) { d++; } else { c++; } } cout << endl << "您答对了" << d << "道题,答错了" << c << "道题!" << endl; cout << endl << "请重新测试:" << endl; goto b;}

 截图:

总结:此次实验内容对我有一定难度,因为以前学的不好所以好多函数调用不太懂,是我和同学合作完成,以后一定要多加练习。

转载于:https://www.cnblogs.com/TSbj/p/4357232.html

你可能感兴趣的文章
执行gem install dryrun错误
查看>>
Java SE之正则表达式一:概述
查看>>
HTML5简单入门系列(四)
查看>>
实现字符串反转
查看>>
转载:《TypeScript 中文入门教程》 5、命名空间和模块
查看>>
苹果开发中常用英语单词
查看>>
[USACO 1.4.3]等差数列
查看>>
Shader Overview
查看>>
Reveal 配置与使用
查看>>
Java中反射的学习与理解(一)
查看>>
C语言初学 俩数相除问题
查看>>
B/S和C/S架构的区别
查看>>
[Java] Java record
查看>>
jQuery - 控制元素显示、隐藏、切换、滑动的方法
查看>>
postgresql学习文档
查看>>
Struts2返回JSON数据的具体应用范例
查看>>
js深度克隆对象、数组
查看>>
socket阻塞与非阻塞,同步与异步
查看>>
团队工作第二天
查看>>
System类
查看>>