题目要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
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;
选择函数
//根据用户选择识别将要进行的四则运算类型
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函数
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;
}
运行截图
- 在编译器中运行
- 在terminals终端中运行
个人小结
问题分析:真分数输出问题
问题分析:真分数输入问题
问题分析:浮点型数据精度值导致结果出错
问题分析:在command或者terminals中给exe文件或a.out文件传参问题
源码
//
// 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;
}