从《构建之法》第一章的 “程序” 例子出发,像阿超那样,花二十分钟写一个能自动生成小学四则运算题目的命令行 “软件”,满足以下需求:
(以下参考博客链接:http://www.cnblogs.com/jiel/p/4810756.html)
include <stdio.h>
include <stdlib.h>
include <time.h>
void generate_arithmetic_problem(int* num1, int* num2, char* op) {
*num1 = rand() % 10 + 1;
*num2 = rand() % 10 + 1;
*op = ((rand() % 2 == 0) ? '+' : ('-' + rand() % 2));
}
int main() {
int num1, num2, result;
char op;
int count = 0;
srand(time(NULL));
while (count < 10) {
generate_arithmetic_problem(&num1, &num2, &op);
printf("问题:%d %c %d = ", num1, op, num2);
scanf("%d", &result);
if (result == (num1 * op + num2)) {
printf("答案正确!\n");
count++;
} else {
printf("答案错误!\n");
}
}
return 0;
}
标签:rand,选做,num1,num2,int,四则运算,result,编程,op From: https://www.cnblogs.com/Augenstem/p/17841687.html