实验6 函数与编译预处理(参考答案)

int answer, result;

switch (op) {

case 1:

printf(\ result = a + b; break; case 2:

printf(\ result = a - b; break; case 3:

printf(\ result = a * b; break; case 4:

if (b != 0) {

printf(\ result = a / b; }

else {

printf(\ }

break; default:

printf(\ break; }

scanf(\ if (result == answer) return 1; else

return 0; }

/* 函数功能: 打印结果正确与否的信息。

函数参数: 整型变量flag,标志结果正确与否 函数返回值:无 */

void Print(int flag) {

if (flag)

printf(\ else

printf(\ }

main() /*主函数*/ {

int a, b, answer, error, score, i, op;

srand(time(NULL)); error = 0; score = 0;

for (i=0; i<10; i++) {

a = rand() + 1; b = rand() + 1;

第 11 页 共 12 页

}

}

op = rand()%4 + 1;

answer = Compute(a, b, op); Print(answer); if (answer == 1)

score = score + 10; else

error++;

printf(\

程序运行结果如下:

4+6=10↙ Rright! 9-9=0↙ Rright! 7-8=-1↙ Rright! 2-7=-2↙ Rright! 4-10=-6↙ Rright! 7/1=7↙ Rright! 7+10=17↙ Rright! 5+7=11↙ Not correct! 3*8=24↙ Rright! 2/7=0↙ Rright!

score = 90, error numbers = 1

第 12 页 共 12 页

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4