结对编程

题目要求:

算法设计思路
数据生成:利用rand()函数生成随机数来确定题目中的数字和运算符。num1、num2、num3通过rand() % 101生成 0 到 100 之间的整数,运算符op1和op2从{' + ', '-', '*', '/'}中随机选取,同时以rand() % 10 < 2的概率决定题目是否包含括号。
表达式计算:calculate函数负责计算四则运算表达式的值。根据是否有括号以及运算符的优先级进行计算。有括号时先计算括号内的表达式,然后再与括号外的数字和运算符进行运算;无括号时,按照先乘除后加减的顺序计算。在除法运算中,若除数为 0 则返回特定值(如 - 1)以表示错误情况。
条件检查:
计算结果范围:在isQuestionValid函数中,调用calculate函数得到计算结果result,检查其是否在 0 到 1000 之间,若不在此范围则题目不符合要求。
除法结果为整数:当题目中有除法运算时,检查被除数是否能被除数整除。例如op1 == '/' && (num1 % num2 != 0)不满足时,即num1除以num2的结果不是整数,该题目不符合要求。对于有括号的情况,同样检查括号内除法运算结果是否为整数。
避免连续相同运算符:检查op1和op2是否相同,若相同则不符合要求,通过do - while循环重新生成op2,直到op1和op2不同为止。
避免减法结果为负数:根据有无括号分别检查减法运算结果。有括号时,检查括号内减法(若有)以及整体减法结果是否为负数;无括号时,检查两个连续减法运算结果是否为负数。
避免乘法结果过大:检查乘法运算结果是否超过 1000。有括号时,考虑括号内计算结果与括号外数字相乘的情况;无括号时,检查两个数字相乘以及中间计算结果与第三个数字相乘的情况。
题目生成与输出:在generateQuestions函数中,不断生成新的数字、运算符和括号组合,调用isQuestionValid函数检查是否满足所有条件。若满足,则按照有括号或无括号的格式输出题目,直到生成指定数量的题目。

C++实现代码:

include

include

include

int calculate(int num1, int num2, int num3, char op1, char op2, bool hasBrackets) {
if (hasBrackets) {
int subResult = (op2 == '+') ? num2 + num3 : (op2 == '-') ? num2 - num3 : (op2 == '') ? num2 * num3 : num2 / num3;
return (op1 == '+') ? num1 + subResult : (op1 == '-') ? num1 - subResult : (op1 == '
') ? num1 * subResult : num1 / subResult;
}

if ((op1 == '+' || op1 == '-') && (op2 == '*' || op2 == '/')) {
    int subResult = (op2 == '*') ? num2 * num3 : num2 / num3;
    return (op1 == '+') ? num1 + subResult : num1 - subResult;
}

int temp = (op1 == '+') ? num1 + num2 : (op1 == '-') ? num1 - num2 : (op1 == '*') ? num1 * num2 : num1 / num2;
return (op2 == '+') ? temp + num3 : (op2 == '-') ? temp - num3 : (op2 == '*') ? temp * num3 : temp / num3;

}

bool isQuestionValid(int num1, int num2, int num3, char op1, char op2, bool hasBrackets) {
int result = calculate(num1, num2, num3, op1, op2, hasBrackets);
if (result < 0 || result > 1000) return false;

if ((op1 == '/' && (num1 % num2 != 0)) || (hasBrackets && op2 == '/' && (num2 % num3 != 0))) return false;
if (!hasBrackets && op2 == '/') {
    int temp = (op1 == '+') ? num1 + num2 : (op1 == '-') ? num1 - num2 : (op1 == '*') ? num1 * num2 : num1 / num2;
    if (temp % num3 != 0) return false;
}


if (op1 == op2) return false;


if ((op1 == '-' && num1 < num2) || (hasBrackets && op2 == '-' && num2 < num3)) return false;
if (!hasBrackets && op1 == '-' && op2 == '-') {
    int temp = num1 - num2;
    if (temp < num3) return false;
}


if ((op1 == '*' && ((hasBrackets && (num1 * (op2 == '+' ? num2 + num3 : num2 - num3) > 1000)) || (num1 * num2 > 1000))) ||
    (!hasBrackets && op2 == '*' && ((op1 == '+' || op1 == '-') ? ((num1 + num2) * num3 > 1000 || (num1 - num2) * num3 > 1000) : ((num1 * num2) * num3 > 1000 || (num1 / num2) * num3 > 1000))))
    return false;

return true;

}

void generateQuestions(int numQuestions) {
srand(time(0));
int count = 0;
while (count < numQuestions) {
int num1 = rand() % 101;
int num2 = rand() % 101;
int num3 = rand() % 101;
char operators[] = {'+', '-', '*', '/'};
char op1 = operators[rand() % 4];
char op2;
do {
op2 = operators[rand() % 4];
} while (op1 == op2);
bool hasBrackets = rand() % 10 < 2;

    if (isQuestionValid(num1, num2, num3, op1, op2, hasBrackets)) {
        if (hasBrackets) {
            std::cout << num1 << " " << op1 << " (" << num2 << " " << op2 << " " << num3 << ")" << std::endl;
        } else {
            std::cout << num1 << " " << op1 << " " << num2 << " " << op2 << " " << num3 << std::endl;
        }
        count++;
    }
}

}

int main() {
int numQuestions = 200;
if (numQuestions > 300) {
numQuestions = 300;
}
generateQuestions(numQuestions);
return 0;
}

运算结果:


结对编程作业体会:
实现了搭档之间的优势互补,在交流协作中有了更激烈的思想碰撞,也看到我们之间的思维差异和对问题考虑的不全面。通过与搭档的合作,不仅提高了代码质量和开发效率,还拓宽了我的编程思路,让我从不同角度学习和成长。学号2352537与学号2352524结对编程。

posted @ 2025-04-14 08:43  peng1257  阅读(43)  评论(0)    收藏  举报