小学四则运算编程实践
小学四则运算编程实践
要求:
从《构建之法》第一章的 “程序” 例子出发,像阿超那样,花二十分钟写一个能自动生成小学四则运算题目的命令行 “软件”,满足以下需求:
(以下参考博客链接:http://www.cnblogs.com/jiel/p/4810756.html)
- 使用C实现四则运算
- 参考 https://www.cnblogs.com/saiwa/articles/5253713.html学习参数传递,使用 -n 参数控制生成题目的个数,例如
Myapp -n 10 -o Exercise.txt
将生成10个题目,题目保存在Exeercise.txmt。 - 使用 -r 参数控制题目中数值(自然数、真分数和真分数分母)的范围,例如
Myapp -r 10
将生成10以内(不包括10)的四则运算题目。该参数可以设置为1或其他自然数。该参数必须给定,否则程序报错并给出帮助信息。 - 生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。
- 每道题目中出现的运算符个数不超过3个。
- 程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。例如,23 + 45 = 和45 + 23 = 是重复的题目,6 × 8 = 和8 × 6 = 也是重复的题目。3+(2+1)和1+2+3这两个题目是重复的,由于+是左结合的,1+2+3等价于(1+2)+3,也就是3+(1+2),也就是3+(2+1)。但是1+2+3和3+2+1是不重复的两道题,因为1+2+3等价于(1+2)+3,而3+2+1等价于(3+2)+1,它们之间不能通过有限次交换变成同一个题目。
生成的题目存入执行程序的当前目录下的Exercises.txt文件,格式如下:- 四则运算题目1
- 四则运算题目2
……
其中真分数在输入输出时采用如下格式,真分数五分之三表示为3/5,真分数二又八分之三表示为2’3/8。
6. 在生成题目的同时,计算出所有题目的答案,并存入执行程序的当前目录下的Answers.txt文件,格式如下:
1. 答案1
2. 答案2
特别的,真分数的运算如下例所示:1/6 + 1/8 = 7/24。
- 程序应能支持一万道题目的生成。
- 程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计,并会输出所有题目中重复的题目,输入参数如下:
Myapp -e.txt -a .txt -o Grade.txt
统计结果输出到文件Grade.txt,格式如下:
Correct: 5 (1, 3, 5, 7, 9)
Wrong: 5 (2, 4, 6, 8, 10)
Repeat:2
RepeatDetail:
(1) 2,45+32 Repeat 3,32+45
(2) 5,3+(2+1) Repeat 7,1+2+3
解释:
Correct: 5 ----5道题目正确,正确的题号 1,3,5,7,9
Wrong:5 -----5道题目错误,错误的题号 2,4,6,8,10
Repeat:2 2---组题目重复
(1) 第一组 题号2,题目 45+32 与题号3的题目重复,题号3为 32+45
(2)第二组 题号5,题目 3+(2+1) 与题号7的题目重复,题号7为 1+2+3
其中“:”后面的数字5表示对/错的题目的数量,括号内的是对/错题目的编号。为简单起见,假设输入的题目都是按照顺序编号的符合规范的题目。
基本实现代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_EXPRESSION_LEN 100
typedef struct {
int numerator;
int denominator;
} Fraction;
// 生成指定范围内的随机整数
int getRandomInt(int min, int max) {
return rand() % (max - min) + min;
}
// 生成真分数
Fraction generateFraction(int range) {
Fraction fraction;
fraction.numerator = getRandomInt(1, range);
fraction.denominator = getRandomInt(fraction.numerator + 1, range + 1);
return fraction;
}
// 生成四则运算表达式
void generateExpression(int range, char* expression) {
int operatorCount = 0;
char operators[3] = {'+', '-', '*', '/'};
Fraction fractions[4];
while (operatorCount < 3) {
int index = operatorCount * 2;
fractions[index] = generateFraction(range);
fractions[index + 1] = generateFraction(range);
// 判断是否需要生成真分数结果
if (operators[operatorCount] == '/') {
while (fractions[index].numerator % fractions[index].denominator == 0 ||
fractions[index].denominator % fractions[index].numerator == 0) {
fractions[index] = generateFraction(range);
fractions[index + 1] = generateFraction(range);
}
}
operatorCount++;
}
sprintf(expression, "%d/%d %c %d/%d %c %d/%d %c %d/%d",
fractions[0].numerator, fractions[0].denominator, operators[0],
fractions[1].numerator, fractions[1].denominator, operators[1],
fractions[2].numerator, fractions[2].denominator, operators[2],
fractions[3].numerator, fractions[3].denominator);
}
// 将真分数转换为字符串
void fractionToString(Fraction fraction, char* str) {
if (fraction.numerator == 0) {
strcpy(str, "0");
} else if (fraction.numerator < fraction.denominator) {
sprintf(str, "%d/%d", fraction.numerator, fraction.denominator);
} else {
int integerPart = fraction.numerator / fraction.denominator;
int numerator = fraction.numerator % fraction.denominator;
sprintf(str, "%d'%d/%d", integerPart, numerator, fraction.denominator);
}
}
// 计算真分数的加法
Fraction addFractions(Fraction fraction1, Fraction fraction2) {
Fraction result;
result.denominator = fraction1.denominator * fraction2.denominator;
result.numerator = fraction1.numerator * fraction2.denominator +
fraction2.numerator * fraction1.denominator;
return result;
}
// 计算真分数的减法
Fraction subtractFractions(Fraction fraction1, Fraction fraction2) {
Fraction result;
result.denominator = fraction1.denominator * fraction2.denominator;
result.numerator = fraction1.numerator * fraction2.denominator -
fraction2.numerator * fraction1.denominator;
return result;
}
// 计算真分数的乘法
Fraction multiplyFractions(Fraction fraction1, Fraction fraction2) {
Fraction result;
result.denominator = fraction1.denominator * fraction2.denominator;
result.numerator = fraction1.numerator * fraction2.numerator;
return result;
}
// 计算真分数的除法
Fraction divideFractions(Fraction fraction1, Fraction fraction2) {
Fraction result;
result.denominator = fraction1.denominator * fraction2.numerator;
result.numerator = fraction1.numerator * fraction2.denominator;
return result;
}
// 简化真分数
void simplifyFraction(Fraction* fraction) {
int gcd = 1;
int min = (fraction->numerator < fraction->denominator)
? fraction->numerator
: fraction->denominator;
for (int i = 1; i <= min; i++) {
if (fraction->numerator % i == 0 && fraction->denominator % i == 0) {
gcd = i;
}
}
fraction->numerator /= gcd;
fraction->denominator /= gcd;
}
// 计算四则运算表达式的结果
Fraction calculateResult(char* expression) {
char* token;
Fraction fractions[4];
char operators[3];
// 解析表达式
token = strtok(expression, " ");
for (int i = 0; i < 4; i++) {
sscanf(token, "%d/%d", &fractions[i].numerator, &fractions[i].denominator);
token = strtok(NULL, " ");
}
token = strtok(NULL, " ");
sscanf(token, "%c", &operators[0]);
token = strtok(NULL, " ");
sscanf(token, "%c", &operators[1]);
token = strtok(NULL, " ");
sscanf(token, "%c", &operators[2]);
// 计算结果
Fraction result;
if (operators[0] == '+') {
result = addFractions(fractions[0], fractions[1]);
} else {
result = subtractFractions(fractions[0], fractions[1]);
}
if (operators[1] == '*') {
result = multiplyFractions(result, fractions[2]);
} else {
result = divideFractions(result, fractions[2]);
}
if (operators[2] == '+') {
result = addFractions(result, fractions[3]);
} else {
result = subtractFractions(result, fractions[3]);
}
simplifyFraction(&result);
return result;
}
// 保存题目至文件
void saveExercisesToFile(char* exercises, char* filename) {
FILE* file = fopen(filename, "w");
if (file != NULL) {
fprintf(file, "%s", exercises);
fclose(file);
} else {
printf("Failed to open file: %s\n", filename);
}
}
// 保存答案至文件
void saveAnswersToFile(char* answers, char* filename) {
FILE* file = fopen(filename, "w");
if (file != NULL) {
fprintf(file, "%s", answers);
fclose(file);
} else {
printf("Failed to open file: %s\n", filename);
}
}
// 判断题目是否重复
int isExerciseRepeated(char* exercises[], int count, char* exercise) {
for (int i = 0; i < count; i++) {
if (strcmp(exercises[i], exercise) == 0) {
return 1;
}
}
return 0;
}
// 检查答案文件并统计结果
void checkAnswers(char* exercisefile, char* answerfile, char* gradefile) {
FILE* exerciseFile = fopen(exercisefile, "r");
FILE* answerFile = fopen(answerfile, "r");
FILE* gradeFile = fopen(gradefile, "w");
if (exerciseFile == NULL || answerFile == NULL || gradeFile == NULL) {
printf("Failed to open file.\n");
return;
}
char line[256];
int correctCount = 0;
int wrongCount = 0;
int repeatCount = 0;
char repeatedExercises[100][MAX_EXPRESSION_LEN];
char repeatedDetails[100][MAX_EXPRESSION_LEN];
while (fgets(line, sizeof(line), exerciseFile)) {
char exercise[MAX_EXPRESSION_LEN];
char userAnswer[MAX_EXPRESSION_LEN];
char correctAnswer[MAX_EXPRESSION_LEN];
sscanf(line, "%[^=]=%s", exercise, userAnswer);
fgets(line, sizeof(line), answerFile);
sscanf(line, "%[^\n]", correctAnswer);
if (strcmp(correctAnswer, userAnswer) == 0) {
correctCount++;
} else {
wrongCount++;
if (isExerciseRepeated(repeatedExercises, repeatCount, exercise)) {
strcpy(repeatedDetails[repeatCount], exercise);
strcat(repeatedDetails[repeatCount], " Repeat ");
strcat(repeatedDetails[repeatCount], repeatedExercises[repeatCount]);
repeatCount++;
} else {
strcpy(repeatedExercises[repeatCount], exercise);
}
}
}
fprintf(gradeFile, "Correct: %d (", correctCount);
for (int i = 0; i < repeatCount; i++) {
fprintf(gradeFile, "%d", i + 1);
if (i < repeatCount - 1) {
fprintf(gradeFile, ", ");
}
}
fprintf(gradeFile, ")\n");
fprintf(gradeFile, "Wrong: %d (", wrongCount);
for (int i = 0; i < repeatCount; i++) {
fprintf(gradeFile, "%d", i + 1);
if (i < repeatCount - 1) {
fprintf(gradeFile, ", ");
}
}
fprintf(gradeFile, ")\n");
fprintf(gradeFile, "Repeat: %d\n", repeatCount);
fprintf(gradeFile, "RepeatDetail:\n");
for (int i = 0; i < repeatCount; i++) {
fprintf(gradeFile, "(%d)\t%s\t%s\n", i + 1, repeatedExercises[i], repeatedDetails[i]);
}
fclose(exerciseFile);
fclose(answerFile);
fclose(gradeFile);
printf("Check answers complete. Results saved to %s\n", gradefile);
}
int main(int argc, char* argv[]) {
int exerciseCount = 0;
int range = 0;
char exercises[10000][MAX_EXPRESSION_LEN];
char answers[10000][MAX_EXPRESSION_LEN];
// 解析命令行参数
for (int i = 1; i < argc; i += 2) {
if (strcmp(argv[i], "-n") == 0) {
exerciseCount = atoi(argv[i + 1]);
} else if (strcmp(argv[i], "-o") == 0) {
strcpy(exercises[0], argv[i + 1]);
strcat(exercises[0], "\n");
strcpy(answers[0], argv[i + 1]);
strcat(answers[0], "\n");
} else if (strcmp(argv[i], "-r") == 0) {
range = atoi(argv[i + 1]);
}
}
if (exerciseCount <= 0 || range <= 0) {
printf("Invalid arguments.\n");
return 0;
}
srand(time(NULL));
// 生成题目和答案
for (int i = 1; i < exerciseCount; i++) {
char expression[MAX_EXPRESSION_LEN];
generateExpression(range, expression);
Fraction result = calculateResult(expression);
char exercise[MAX_EXPRESSION_LEN];
sprintf(exercise, "%d. %s = ", i, expression);
strcpy(exercises[i], exercise);
char answer[MAX_EXPRESSION_LEN];
fractionToString(result, answer);
strcpy(answers[i], answer);
}
// 保存题目和答案至文件
saveExercisesToFile(exercises[0], "Exercises.txt");
saveAnswersToFile(answers[0], "Answers.txt");
// 检查答案并统计结果
checkAnswers("Exercises.txt", "Answers.txt", "Grade.txt");
return 0;
}
在命令行中运行可执行文件,按照要求的参数格式来生成题目和检查答案。
例如,要生成10道题目并保存至Exercises.txt文件中,题目数值范围为10,可以执行以下命令:
Myapp -n 10 -o Exercises.txt -r 10
要检查答案并统计结果,可以执行以下命令:
Myapp -e Exercises.txt -a Answers.txt -o Grade.txt
生成的题目和答案将分别保存在Exercises.txt和Answers.txt文件中,检查结果将保存在Grade.txt文件中。