,功能为:完成10道题的解答,每道题10分,测完10道题后,给出总成绩。
说明:
(1)这个题目我要求得很笼统,目的是想看看谁在设计系统的时候考虑得更周全;
(2)据库函数中有一个函数rand可以生成伪随机数,结合srand函数后可以生成真随机数,通过下列说明(含程序或程序段)验证这两个函数的使用并尽可能灵活地应用到本系统的设计中。
C语言问题2: 为小学生设计一个测验简单四则运算的系统
答案:3 悬赏:10 手机版
解决时间 2021-03-03 12:21
- 提问者网友:别再叽里呱啦
- 2021-03-02 18:58
最佳答案
- 五星知识达人网友:轮獄道
- 2021-03-02 19:53
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define YES 1
#define NO 0
#define NUM_PROBLEM 10 //题目的数量
#define LIMIT_ADDSUB 100 //加法和减法操作数的上限
#define LIMIT_MULDIV 20 //乘法和除法操作数的上限
int expressionCount = 0;
struct expression
{
int num1;
int num2;
char op;
}expr[NUM_PROBLEM];
int createProblem(void);
int createAddProblem(void);
int createSubProblem(void);
int createMulProblem(void);
int createDivProblem(void);
int expressionExist(int num1, int num2, char op);
int main()
{
int i, answer, result;
float score;
srand((unsigned)time(NULL));
score = 0.0;
for (i = 1; i <= NUM_PROBLEM; ++i)
{
printf("第%2d题: ", i);
result = createProblem();
scanf("%d", &answer);
getchar();
if (result == answer)
score += 100.0/NUM_PROBLEM;
}
printf("Final Score:%.0f\n", score);
return 0;}
int createProblem(void)
{
char op;
int result;
op = rand()%4+1;
switch (op)
{
case 1: //加法
result = createAddProblem();
break;
case 2: //减法
result = createSubProblem();
break;
case 3: //乘法
result = createMulProblem();
break;
case 4: //除法
result = createDivProblem();
break;
default:
exit(1);
}
return result;}
int createAddProblem(void)
{
int num1, num2;
do
{
num1 = rand()%LIMIT_ADDSUB+1;
num2 = rand()%LIMIT_ADDSUB+1;
} while (expressionExist(num1, num2, '+'));
printf("%3d + %3d = ", num1, num2);
return (num1+num2);}
int createSubProblem(void)
{
int num1, num2, temp;
do
{
num1 = rand()%LIMIT_ADDSUB+1;
num2 = rand()%LIMIT_ADDSUB+1;
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
} while (expressionExist(num1, num2, '-'));
printf("%3d - %3d = ", num1, num2);
return (num1-num2);}
int createMulProblem(void)
{
int num1, num2;
do
{
num1 = rand()%LIMIT_MULDIV+1;
num2 = rand()%LIMIT_MULDIV+1;
} while (expressionExist(num1, num2, '*'));
printf("%3d * %3d = ", num1, num2);
return (num1*num2);}
int createDivProblem(void)
{
int num1, num2;
do
{
num1 = rand()%LIMIT_MULDIV+1;
num2 = rand()%LIMIT_MULDIV+1;
} while (expressionExist(num1, num2, '/'));
printf("%3d / %3d = ", num1*num2, num1);
return num2;}
int expressionExist(int num1, int num2, char op)
{
int i;
for (i = 0; i < expressionCount; ++i)
{
if (expr[i].num1 == num1 && expr[i].num2 == num2 && expr[i].op == op)
return YES;
}
expr[i].num1 = num1;
expr[i].num2 = num2;
expr[i].op = op;
++expressionCount;
return NO;}
先拿去调试,把问题和需要补充的功能发给我,明天再帮你完善。
#include <stdlib.h>
#include <time.h>
#define YES 1
#define NO 0
#define NUM_PROBLEM 10 //题目的数量
#define LIMIT_ADDSUB 100 //加法和减法操作数的上限
#define LIMIT_MULDIV 20 //乘法和除法操作数的上限
int expressionCount = 0;
struct expression
{
int num1;
int num2;
char op;
}expr[NUM_PROBLEM];
int createProblem(void);
int createAddProblem(void);
int createSubProblem(void);
int createMulProblem(void);
int createDivProblem(void);
int expressionExist(int num1, int num2, char op);
int main()
{
int i, answer, result;
float score;
srand((unsigned)time(NULL));
score = 0.0;
for (i = 1; i <= NUM_PROBLEM; ++i)
{
printf("第%2d题: ", i);
result = createProblem();
scanf("%d", &answer);
getchar();
if (result == answer)
score += 100.0/NUM_PROBLEM;
}
printf("Final Score:%.0f\n", score);
return 0;}
int createProblem(void)
{
char op;
int result;
op = rand()%4+1;
switch (op)
{
case 1: //加法
result = createAddProblem();
break;
case 2: //减法
result = createSubProblem();
break;
case 3: //乘法
result = createMulProblem();
break;
case 4: //除法
result = createDivProblem();
break;
default:
exit(1);
}
return result;}
int createAddProblem(void)
{
int num1, num2;
do
{
num1 = rand()%LIMIT_ADDSUB+1;
num2 = rand()%LIMIT_ADDSUB+1;
} while (expressionExist(num1, num2, '+'));
printf("%3d + %3d = ", num1, num2);
return (num1+num2);}
int createSubProblem(void)
{
int num1, num2, temp;
do
{
num1 = rand()%LIMIT_ADDSUB+1;
num2 = rand()%LIMIT_ADDSUB+1;
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
} while (expressionExist(num1, num2, '-'));
printf("%3d - %3d = ", num1, num2);
return (num1-num2);}
int createMulProblem(void)
{
int num1, num2;
do
{
num1 = rand()%LIMIT_MULDIV+1;
num2 = rand()%LIMIT_MULDIV+1;
} while (expressionExist(num1, num2, '*'));
printf("%3d * %3d = ", num1, num2);
return (num1*num2);}
int createDivProblem(void)
{
int num1, num2;
do
{
num1 = rand()%LIMIT_MULDIV+1;
num2 = rand()%LIMIT_MULDIV+1;
} while (expressionExist(num1, num2, '/'));
printf("%3d / %3d = ", num1*num2, num1);
return num2;}
int expressionExist(int num1, int num2, char op)
{
int i;
for (i = 0; i < expressionCount; ++i)
{
if (expr[i].num1 == num1 && expr[i].num2 == num2 && expr[i].op == op)
return YES;
}
expr[i].num1 = num1;
expr[i].num2 = num2;
expr[i].op = op;
++expressionCount;
return NO;}
先拿去调试,把问题和需要补充的功能发给我,明天再帮你完善。
全部回答
- 1楼网友:等灯
- 2021-03-02 22:13
私信我 我给你写
- 2楼网友:蕴藏春秋
- 2021-03-02 21:06
你说呢...
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯