小学生四则运算程序

预计花费时间40分钟

实际花费时间35分钟

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 10

int Test()
{
int num = 0;
int answer = 0;
int result = 0;
int m = 0;
int n = 0;
char op = 0;

int count = N;
while ( count-- )
{
m = rand() % 10;
n = rand() % 10;
op = rand() % 4;

switch ( op )
{
case 0:
op = '+';
answer = m+n;
break;
case 1:
op = '-';
answer = m-n;
break;
case 2:
op = '*';
answer = m*n;
break;
case 3:
n++;
op = '/';
answer = m/n;
break;
default:
break;
}

printf("\n题目:");
printf("%d%c%d=?\n", m, op, n);
printf("请输入答案:");
scanf("%d", &result);

if ( answer == result )
num++;
}

return num;
}

int main()
{
srand( time(NULL) );

printf("开始做题:\n");
printf("答题结束,你总共答对%d道题目。\n", Test());
return 0;
}

posted @ 2015-03-26 11:34  7Oliver7-25郑泽成  阅读(167)  评论(5编辑  收藏  举报