第八周结对编程作业,随机300道四则运算
学生学号:2352616、2352812
1.要求:
两个运算符,100 以内的数字,不需要写答案。
需要检查答案是否正确,并且保证答案在 0..1000 之间
2.思想:(1)根据数学排列思想保证含二个运算符四则运算式子的多样性
(2)调用rand()函数来随机产生0-100的随机数
(3)采用判断语句排除在进行除法运算时除数为0的情况
(4)判断答案时,看运算所得答案与输入答案是否相同
3.程序代码:
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_QUESTIONS 300
typedef struct
{
int a, b, c;
char op1, op2;
int correct_answer;
} Question;
Question questions[MAX_QUESTIONS];
int generated_count = 0;
int get_priority(char op)
{
if (op == '*' || op == '/') return 2;
return 1;
}
int apply_op(int a, char op, int b, int *error)
{
*error = 0;
switch(op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/':
if (b == 0 || a % b != 0) *error = 1;
return a / b;
default: *error = 1; return 0;
}
}
void generate_questions()
{
srand(time(NULL));
char ops[] = {'+', '-', '*', '/'};
while (generated_count < MAX_QUESTIONS) {
char op1 = ops[rand() % 4];
char op2 = ops[rand() % 4];
int a, b, c, temp, result, error;
int p1 = get_priority(op1);
int p2 = get_priority(op2);
if (p2 > p1) {
if (op2 == '/') {
do { c = rand() % 100 + 1; } while (c == 0);
b = (rand() % (100 / c + 1)) * c;
} else {
b = rand() % 101;
c = rand() % 101;
}
temp = apply_op(b, op2, c, &error);
if (error) continue;
if (op1 == '/') {
if (temp == 0) continue;
if (temp > 0) {
a = (rand() % (100 / temp + 1)) * temp;
} else {
a = 0;
}
} else {
a = rand() % 101;
}
result = apply_op(a, op1, temp, &error);
} else {
if (op1 == '/') {
do { b = rand() % 100 + 1; } while (b == 0);
a = (rand() % (100 / b + 1)) * b;
} else {
a = rand() % 101;
b = rand() % 101;
}
temp = apply_op(a, op1, b, &error);
if (error) continue;
if (op2 == '/') {
if (temp == 0) {
c = rand() % 100 + 1;
} else {
int abs_temp = abs(temp);
int factors[100], fc = 0;
for (int i = 1; i <= abs_temp && i <= 100; i++)
if (abs_temp % i == 0) factors[fc++] = i;
if (fc == 0) continue;
c = factors[rand() % fc];
}
} else {
c = rand() % 101;
}
result = apply_op(temp, op2, c, &error);
}
if (error || result < 0 || result > 1000) continue;
questions[generated_count].a = a;
questions[generated_count].b = b;
questions[generated_count].c = c;
questions[generated_count].op1 = op1;
questions[generated_count].op2 = op2;
questions[generated_count].correct_answer = result;
generated_count++;
}
}
void check_answers()
{
int correct_count = 0;
for (int i = 0; i < MAX_QUESTIONS; i++) {
printf("题目 %d/%d: %d %c %d %c %d = ",
i+1, MAX_QUESTIONS,
questions[i].a, questions[i].op1,
questions[i].b, questions[i].op2,
questions[i].c);
int user_answer;
if (scanf("%d", &user_answer) != 1) {
printf("输入格式错误,请重新输入!\n");
while (getchar() != '\n');
i--;
continue;
}
if (user_answer == questions[i].correct_answer) {
printf("正确!\n");
correct_count++;
} else {
printf("错误!正确答案是:%d\n", questions[i].correct_answer);
}
}
printf("\n测试完成!正确率:%.2f%%\n",
(float)correct_count / MAX_QUESTIONS * 100);
}
int main()
{
generate_questions();
printf("已生成%d道四则运算题,开始答题:\n", MAX_QUESTIONS);
check_answers();
return 0;
}
4.运算结果:
5.结对编程作业体会:
在这次结对编程中完成了随机300道的四则运算,该程序有效实现了三元四则运算题目的生成与测试功能,正确处理了运算符优先级、除法整除及结果范围限制,不仅高效地完成了项目目标,更让我们深刻地体会到结对编程时两个人思维的碰撞对于自己的帮助。