软件工程第二次作业——小学四则运算题目生成程序
目录
名称 | 内容 |
---|---|
课程班级 | 班级链接 |
作业要求 | 作业链接 |
作业目标 | 文中问题分析部分说明 |
本人学号 | 3180405308 |
题目要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2)程序要求能处理用户的输入,判断对错,累积分数
3)程序支持可以由用户自行选择加、减、乘、除运算
4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
代码分析
结构体
- 版本一
//含有一个二元算数式的三个值
typedef struct{
float x = 0,y = 0; //x,y为给定的随机数
float r = 0; //r为算数式结果
}Arithmetic;
- 版本二
//含有一个二元算数式的三个值的分数表示形式,如果是整数,那么分母皆为1即可
typedef struct{
int x1,x2;
int y1,y2;
int z1,z2;
}Arithmetic;
选择函数
- 版本一
//根据用户选择识别将要进行的四则运算类型
Arithmetic choose(Arithmetic n){
int choose = 0;
cout<<"请选择您需要进行的四则运算类型\n"<<"1.加法\t2.减法\t3.乘法\t4.除法\n";
cin>>choose;
switch (choose) {
case 1:
n = Addition();
break;
case 2:
n = Subtraction();
break;
case 3:
n = Multiplication();
break;
case 4:
n = Division();
break;
default:
break;
}
return n;
}
- 版本二
同版本一
转译用户输入结果
- 版本一
float conversion(string input){
bool flag = false, sign = false;
//flag标记用户输入中是否存在/号,有/号则true否则false
//sign标记用户输入是否为负数,是负数则true否则false
float a = 0, b = 0, result = 0;
for (int i = 0; i<input.length(); i++) {
if (input[i] != '/' && input[i] != '-') {
int num = input[i]-'0'; //将char型转换为int型
if (!flag) { //若果flag为false,则说明还未遇到/号
a *= 10; //a记录/号前的数值
a += num;
} else {
b *= 10; //b记录/号后的数值
b += num;
}
} else if (input[i] == '/'){
flag = true;
} else {
sign = true;
}
}
if (flag && sign) { //既有/号,又是负值
result = -a/b;
} else if (flag && !sign){ //有/号,但为正值
result = a/b;
} else if (!flag && sign){ //没有有/号,但为负值
result = -a;
} else { //没有有/号,且为正值
result = a;
}
return result;
}
- 版本二
版本二删去了输入识别函数整合进入判断对错函数
判断对错函数
- 版本一
bool judge(Arithmetic n){
bool flag = false; //flag用于记录用户输入答案是否正确,正确return true,否则return false
string input;
cin>>input;
if (n.r == conversion(input))
flag = true;
return flag;
}
- 版本二
bool judge(Arithmetic n){
bool ans = false; //ans用于记录用户输入答案是否正确,正确return true,否则return false
string input;
cin>>input;
bool flag = false, sign = false;
//flag标记用户输入中是否存在/号,有/号则true否则false
//sign标记用户输入是否为负数,是负数则true否则false
int a = 0, b = 0;
for (int i = 0; i<input.length(); i++) {
if (input[i] != '/' && input[i] != '-') {
int num = input[i]-'0'; //将char型转换为int型
if (!flag) { //若果flag为false,则说明还未遇到/号
a *= 10; //a记录/号前的数值
a += num;
} else {
b *= 10; //b记录/号后的数值
b += num;
}
} else if (input[i] == '/'){
flag = true;
} else {
sign = true;
}
}
if (flag && sign) { //既有/号,又是负值
if (n.z1 == -a && n.z2 == b)
ans = true;
} else if (flag && !sign){ //有/号,但为正值
if (n.z1 == a && n.z2 == b)
ans = true;
} else if (!flag && sign){ //没有有/号,但为负值
if (n.z1 == -a)
ans = true;
} else { //没有有/号,且为正值
if (n.z1 == a)
ans = true;
}
return ans;
}
Addition加法
- 版本一
Arithmetic Addition(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为0则进行整数四则运算,若为1则进行真分数的四则运算
n.x = rand()%size+1;
n.y = rand()%size+1;
n.r = n.x + n.y;
printf("%.0f + %.0f = ?\n",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) { //需要保证数值是真分数
n.x = a/b;
} else if (a==b){ //分子分母如果相同的话直接赋值为1
n.x = 1;
}else { //需要保证数值是真分数
n.x = b/a;
}
if (c<d) { //需要保证数值是真分数
n.y = c/d;
} else if (c==d){ //分子分母如果相同的话直接赋值为1
n.y = 1;
}else { //需要保证数值是真分数
n.y = d/c;
}
n.r = n.x + n.y;
if (n.r==0) { //结果为0说明两个真分数都是1/1
printf("%.0f + %.0f = ?\n",n.x,n.y);
} else if (n.x==1){ //仅有一个真分数,一个1/1
printf("%.0f + %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f + %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else { //除去其他可能,仅剩下两个数都是真分数的情况
printf("%.0f/%.0f + %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
- 版本二
Arithmetic Addition(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为0则进行整数四则运算,若为1则进行真分数的四则运算
n.x1 = rand()%size+1;
n.y1 = rand()%size+1;
n.z1 = n.x1 + n.y1;
n.x2 = 1;
n.y2 = 1;
n.z2 = 1;
printf("%d + %d = ?\n",n.x1,n.y1);
} else {
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
n.z2 = LCM(n.x2, n.y2);
n.z1 = n.x1*n.z2/n.y2 + n.y1*n.z2/n.x2;
printf("%d/%d + %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
}
return n;
}
Subtraction减法
- 版本一
Arithmetic Subtraction(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x = rand()%size+1;
n.y = rand()%size+1;
n.r = n.x - n.y;
printf("%.0f - %.0f = ?\n",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x - n.y;
if (n.r==0) {
printf("%.0f - %.0f = ?\n",n.x,n.y);
} else if (n.x==1){
printf("%.0f - %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f - %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f - %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
- 版本二
Arithmetic Subtraction(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x1 = rand()%size+1;
n.y1 = rand()%size+1;
n.z1 = n.x1 - n.y1;
n.x2 = 1;
n.y2 = 1;
n.z2 = 1;
printf("%d - %d = ?\n",n.x1,n.y1);
} else {
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
n.z2 = LCM(n.x2, n.y2);
n.z1 = n.x1*n.z2/n.y2 - n.y1*n.z2/n.x2;
printf("%d/%d - %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
}
return n;
}
Multiplication乘法
- 版本一
Arithmetic Multiplication(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x= rand()%size+1;
n.y= rand()%size+1;
n.r = n.x * n.y;
printf("%.0f * %.0f = ?\n",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x * n.y;
if (n.r==0) {
printf("%.0f * %.0f = ?\n",n.x,n.y);
} else if (n.x==1){
printf("%.0f * %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f * %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f * %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
- 版本二
Arithmetic Multiplication(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x1 = rand()%size+1;
n.y1 = rand()%size+1;
n.z1 = n.x1 * n.y1;
n.x2 = 1;
n.y2 = 1;
n.z2 = 1;
printf("%d * %d = ?\n",n.x1,n.y1);
} else {
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
int num = 0;
num = GCD(n.x1*n.y1, n.x2*n.y2);
n.z1 = n.x1*n.y1/num;
n.z2 = n.x2*n.y2/num;
printf("%d/%d * %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
}
return n;
}
Division除法
- 版本一
Arithmetic Division(){ //除法不需要考虑两个整数的,没有意义,直接两个真分数的除法
Arithmetic n;
srand((unsigned)time(NULL));
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x / n.y;
if (n.r==0) {
printf("%.0f / %.0f = ?\n",n.x,n.y);
} else if (n.x==1){
printf("%.0f / %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f / %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f / %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
return n;
}
- 版本二
Arithmetic Division(){ //除法不需要考虑两个整数的,没有意义,直接两个真分数的除法
Arithmetic n;
srand((unsigned)time(NULL));
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
int num = 0;
num = GCD(n.x1*n.y1, n.x2*n.y2);
n.z1 = n.x1*n.y1/num;
n.z2 = n.x2*n.y2/num;
printf("%d/%d * %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
return n;
}
main函数
版本一同版本二
int main(int argc, const char * argv[]) {
int score = 0, cnt = 0;
printf("请设置题目数量\n"); //此处用于测试,源码部分,这段内容注释掉
scanf("%d",&cnt);
// if (string(argv[1])=="-n") { //在terminals或者command中运行的时候,参数顺序存放在argv数组中
// string c = argv[2];
// cnt = stoi(c);
// }
for (int i = 0; i<cnt; i++) {
Arithmetic t;
t = choose(t);
if (judge(t) == true) {
score+=10;
cout<<"恭喜您回答正确!\n";
} else {
cout<<"很抱歉您的答案不正确,再想想哦~\n";
}
}
printf("满分为:%d 您的得分为:%d分\n",cnt*10,score);
return 0;
}
最大公约数函数和最小公倍数函数
//求最小公倍数
int LCM(int a, int b){
int tmp,r,n;
if (a < b) {
tmp = b;
b = a;
a = tmp;
}
r = a%b;
n = a*b;
while (r != 0) {
a = b;
b = r;
r = a%b;
}
return n/b;
}
//求最大公约数
int GCD(int a, int b){
int tmp,r,n;
if (a < b) {
tmp = b;
b = a;
a = tmp;
}
r = a%b;
n = a*b;
while (r != 0) {
a = b;
b = r;
r = a%b;
}
return b;
}
运行截图
- 在编译器中运行
- 在terminals终端中运行
个人小结
-
问题分析:真分数输出问题
-
问题分析:真分数输入问题
-
问题分析:浮点型数据精度值导致结果出错
-
问题分析:在command或者terminals中给exe文件或a.out文件传参问题
psp2.1 | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
---|---|---|---|
Planning | 计划 | 5 | 3 |
Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 5 | 5 |
Development | 开发 | 300 | 300 |
Analysis | 需求分析(包括学习新技术) | 15 | 45 |
Design Spec | 生成设计文档 | 5 | 20 |
Design Review | 设计复审 | 5 | 5 |
Coding Standard | 代码规范 | 3 | 3 |
Design | 具体设计 | 10 | 12 |
Coding | 具体编码 | 200 | 200 |
Code Review | 代码复审 | 10 | 20 |
Test | 测试(自我测试,修改代码,提交修改) | 10 | 60 |
Reporting | 报告 | 5 | 5 |
Test Report | 测试报告 | 5 | 5 |
Size Measurement | 计算工作量 | 20 | 30 |
Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 20 | 20 |
源码
- 版本一
//
// main.cpp
// 四则运算生成
//
// Created by Chen on 2020/11/2.
// Copyright © 2020 xxc. All rights reserved.
//
#include <iostream>
#include <string.h>
#define size 10
using namespace std;
//含有一个二元算数式的三个值
typedef struct{
float x = 0,y = 0; //x,y为给定的随机数
float r = 0; //r为算数式结果
}Arithmetic;
Arithmetic choose(Arithmetic n);//选择函数,根据用户选择 识别将要进行的四则运算类型
float conversion(string input);//用户输入转换
bool judge(Arithmetic n);//结果判断
Arithmetic Addition();//加法运算
Arithmetic Subtraction();//减法运算
Arithmetic Multiplication();//乘法运算
Arithmetic Division();//除法运算
int main(int argc, const char * argv[]) {
int score = 0, cnt = 0;
// printf("请设置题目数量\n");
// scanf("%d",&cnt);
if (string(argv[1])=="-n") { //在terminals或者command中运行的时候,参数顺序存放在argv数组中
string c = argv[2];
cnt = stoi(c);
}
for (int i = 0; i<cnt; i++) {
Arithmetic t;
t = choose(t);
if (judge(t) == true) {
score+=10;
cout<<"恭喜您回答正确!\n";
} else {
cout<<"很抱歉您的答案不正确,再想想哦~\n";
}
}
printf("满分为:%d 您的得分为:%d分\n",cnt*10,score);
return 0;
}
//函数
//选择函数,根据用户选择识别将要进行的四则运算类型
Arithmetic choose(Arithmetic n){
int choose = 0;
cout<<"请选择您需要进行的四则运算类型\n"<<"1.加法\t2.减法\t3.乘法\t4.除法\n";
cin>>choose;
switch (choose) {
case 1:
n = Addition();
break;
case 2:
n = Subtraction();
break;
case 3:
n = Multiplication();
break;
case 4:
n = Division();
break;
default:
break;
}
return n;
}
//转译用户输入结果
float conversion(string input){
bool flag = false, sign = false;
//flag标记用户输入中是否存在/号,有/号则true否则false
//sign标记用户输入是否为负数,是负数则true否则false
float a = 0, b = 0, result = 0;
for (int i = 0; i<input.length(); i++) {
if (input[i] != '/' && input[i] != '-') {
int num = input[i]-'0'; //将char型转换为int型
if (!flag) { //若果flag为false,则说明还未遇到/号
a *= 10; //a记录/号前的数值
a += num;
} else {
b *= 10; //b记录/号后的数值
b += num;
}
} else if (input[i] == '/'){
flag = true;
} else {
sign = true;
}
}
if (flag && sign) { //既有/号,又是负值
result = -a/b;
} else if (flag && !sign){ //有/号,但为正值
result = a/b;
} else if (!flag && sign){ //没有有/号,但为负值
result = -a;
} else { //没有有/号,且为正值
result = a;
}
return result;
}
//判断对错函数
bool judge(Arithmetic n){
bool flag = false; //flag用于记录用户输入答案是否正确,正确return true,否则return false
string input;
cin>>input;
if (n.r == conversion(input))
flag = true;
return flag;
}
//Addition加法
Arithmetic Addition(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为0则进行整数四则运算,若为1则进行真分数的四则运算
n.x = rand()%size+1;
n.y = rand()%size+1;
n.r = n.x + n.y;
printf("%.0f + %.0f = ?\n",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) { //需要保证数值是真分数
n.x = a/b;
} else if (a==b){ //分子分母如果相同的话直接赋值为1
n.x = 1;
}else { //需要保证数值是真分数
n.x = b/a;
}
if (c<d) { //需要保证数值是真分数
n.y = c/d;
} else if (c==d){ //分子分母如果相同的话直接赋值为1
n.y = 1;
}else { //需要保证数值是真分数
n.y = d/c;
}
n.r = n.x + n.y;
if (n.r==0) { //结果为0说明两个真分数都是1/1
printf("%.0f + %.0f = ?\n",n.x,n.y);
} else if (n.x==1){ //仅有一个真分数,一个1/1
printf("%.0f + %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f + %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else { //除去其他可能,仅剩下两个数都是真分数的情况
printf("%.0f/%.0f + %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
//Subtraction减法
Arithmetic Subtraction(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x = rand()%size+1;
n.y = rand()%size+1;
n.r = n.x - n.y;
printf("%.0f - %.0f = ?\n",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x - n.y;
if (n.r==0) {
printf("%.0f - %.0f = ?\n",n.x,n.y);
} else if (n.x==1){
printf("%.0f - %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f - %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f - %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
//Multiplication乘法
Arithmetic Multiplication(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x= rand()%size+1;
n.y= rand()%size+1;
n.r = n.x * n.y;
printf("%.0f * %.0f = ?\n",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x * n.y;
if (n.r==0) {
printf("%.0f * %.0f = ?\n",n.x,n.y);
} else if (n.x==1){
printf("%.0f * %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f * %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f * %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
//Division除法
Arithmetic Division(){ //除法不需要考虑两个整数的,没有意义,直接两个真分数的除法
Arithmetic n;
srand((unsigned)time(NULL));
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x / n.y;
if (n.r==0) {
printf("%.0f / %.0f = ?\n",n.x,n.y);
} else if (n.x==1){
printf("%.0f / %.0f/%.0f = ?\n",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f / %.0f = ?\n",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f / %.0f/%.0f = ?\n",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
return n;
}
- 版本二
//
// main.cpp
// 四则运算生成
//
// Created by Chen on 2020/11/2.
// Copyright © 2020 xxc. All rights reserved.
//
#include <iostream>
#include <string.h>
#define size 10
using namespace std;
//含有一个二元算数式的三个值以及一个标志符的结构体
typedef struct{
int x1,x2;
int y1,y2;
int z1,z2;
}Arithmetic;
Arithmetic choose(Arithmetic n);//选择函数,根据用户选择 识别将要进行的四则运算类型
int LCM(int a, int b);//最小公倍数
int GCD(int a, int b);//最大公约数
bool judge(Arithmetic n);//结果判断
Arithmetic Addition();//加法运算
Arithmetic Subtraction();//减法运算
Arithmetic Multiplication();//乘法运算
Arithmetic Division();//除法运算
int main(int argc, const char * argv[]) {
int score = 0, cnt = 0;
printf("请设置题目数量\n");
scanf("%d",&cnt);
// if (string(argv[1])=="-n") { //在terminals或者command中运行的时候,参数顺序存放在argv数组中
// string c = argv[2];
// cnt = stoi(c);
// }
for (int i = 0; i<cnt; i++) {
Arithmetic t;
t = choose(t);
if (judge(t) == true) {
score+=10;
cout<<"恭喜您回答正确!\n";
} else {
cout<<"很抱歉您的答案不正确,再想想哦~\n";
}
}
printf("满分为:%d 您的得分为:%d分\n",cnt*10,score);
return 0;
}
//函数
//选择函数,根据用户选择识别将要进行的四则运算类型
Arithmetic choose(Arithmetic n){
int choose = 0;
cout<<"请选择您需要进行的四则运算类型\n"<<"1.加法\t2.减法\t3.乘法\t4.除法\n";
cin>>choose;
switch (choose) {
case 1:
n = Addition();
break;
case 2:
n = Subtraction();
break;
case 3:
n = Multiplication();
break;
case 4:
n = Division();
break;
default:
break;
}
return n;
}
//判断对错函数
bool judge(Arithmetic n){
bool ans = false; //ans用于记录用户输入答案是否正确,正确return true,否则return false
string input;
cin>>input;
bool flag = false, sign = false;
//flag标记用户输入中是否存在/号,有/号则true否则false
//sign标记用户输入是否为负数,是负数则true否则false
int a = 0, b = 0;
for (int i = 0; i<input.length(); i++) {
if (input[i] != '/' && input[i] != '-') {
int num = input[i]-'0'; //将char型转换为int型
if (!flag) { //若果flag为false,则说明还未遇到/号
a *= 10; //a记录/号前的数值
a += num;
} else {
b *= 10; //b记录/号后的数值
b += num;
}
} else if (input[i] == '/'){
flag = true;
} else {
sign = true;
}
}
if (flag && sign) { //既有/号,又是负值
if (n.z1 == -a && n.z2 == b)
ans = true;
} else if (flag && !sign){ //有/号,但为正值
if (n.z1 == a && n.z2 == b)
ans = true;
} else if (!flag && sign){ //没有有/号,但为负值
if (n.z1 == -a)
ans = true;
} else { //没有有/号,且为正值
if (n.z1 == a)
ans = true;
}
return ans;
}
//求最小公倍数
int LCM(int a, int b){
int tmp,r,n;
if (a < b) {
tmp = b;
b = a;
a = tmp;
}
r = a%b;
n = a*b;
while (r != 0) {
a = b;
b = r;
r = a%b;
}
return n/b;
}
//求最大公约数
int GCD(int a, int b){
int tmp,r,n;
if (a < b) {
tmp = b;
b = a;
a = tmp;
}
r = a%b;
n = a*b;
while (r != 0) {
a = b;
b = r;
r = a%b;
}
return b;
}
//Addition加法
Arithmetic Addition(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为0则进行整数四则运算,若为1则进行真分数的四则运算
n.x1 = rand()%size+1;
n.y1 = rand()%size+1;
n.z1 = n.x1 + n.y1;
n.x2 = 1;
n.y2 = 1;
n.z2 = 1;
printf("%d + %d = ?\n",n.x1,n.y1);
} else {
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
n.z2 = LCM(n.x2, n.y2);
n.z1 = n.x1*n.z2/n.y2 + n.y1*n.z2/n.x2;
printf("%d/%d + %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
}
return n;
}
//Subtraction减法
Arithmetic Subtraction(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x1 = rand()%size+1;
n.y1 = rand()%size+1;
n.z1 = n.x1 - n.y1;
n.x2 = 1;
n.y2 = 1;
n.z2 = 1;
printf("%d - %d = ?\n",n.x1,n.y1);
} else {
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
n.z2 = LCM(n.x2, n.y2);
n.z1 = n.x1*n.z2/n.y2 - n.y1*n.z2/n.x2;
printf("%d/%d - %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
}
return n;
}
//Multiplication乘法
Arithmetic Multiplication(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x1 = rand()%size+1;
n.y1 = rand()%size+1;
n.z1 = n.x1 * n.y1;
n.x2 = 1;
n.y2 = 1;
n.z2 = 1;
printf("%d * %d = ?\n",n.x1,n.y1);
} else {
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
int num = 0;
num = GCD(n.x1*n.y1, n.x2*n.y2);
n.z1 = n.x1*n.y1/num;
n.z2 = n.x2*n.y2/num;
printf("%d/%d * %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
}
return n;
}
//Division除法
Arithmetic Division(){ //除法不需要考虑两个整数的,没有意义,直接两个真分数的除法
Arithmetic n;
srand((unsigned)time(NULL));
n.x1 = rand()%size+1;
n.x2 = rand()%size+n.x1;
n.y1 = rand()%size+1;
n.y2 = rand()%size+n.y1;
int num = 0;
num = GCD(n.x1*n.y1, n.x2*n.y2);
n.z1 = n.x1*n.y1/num;
n.z2 = n.x2*n.y2/num;
printf("%d/%d * %d/%d = ?\n",n.x1,n.x2,n.y1,n.y2);
return n;
}