C程序设计-小学生计算机教学辅助系统(四则运算)
(ps: 学校上学期c语言实验的一个报告,难度上确实不大,但最近整理代码时看到了之前写的,所以就记录下来)
题目要求:
任务 1: 随机产生 1 - 10 之间正整数,出乘法问题,直到学生答对为止。
任务 2: 在任务 1 基础上,当学生答错时,最多给三次机会。
任务 3: 在任务 1 基础上,连做 10 道乘法题,不给机会重做,统计总分、正确率。
任务 4: 在任务 3 基础上,随机产生 10 道四则运算题,不给机会重做,统计总分、正确率。
任务 5: 在任务 4 基础上,若回答正确率低于 75 % ,则再做 10 道题,直到正确率高于 75 % 为止。
任务 6: 在任务 5 基础上,随机生成1~4间随机数,来为学生输入的每一个正确或错误的答案输出不同评价。
代码实现
目录
- 题目要求:
- 代码实现
- main函数 / 菜单
- 部分功能
- 判断两个数是否相同
- 产生随机数函数
- 返回分数 / 计算正答率
- 任务
- 任务一
- 任务二
- 任务三
- 任务四
- 任务五
- 任务六
- 完整代码
完整代码跳转↑
main函数 / 菜单
void menu()
{
printf("******************************************************************************\n");
printf("小学生计算机辅助系统\n");
printf(" * 1. 任务 1:随机产生 1 - 10 之间正整数,出乘法问题,直到学生答对为止。\n");
printf(" * 2. 任务 2:在任务 1 基础上,当学生答错时,最多给三次机会。 \n");
printf(" * 3. 任务 3:在任务 1 基础上,连做 10 道乘法题,不给机会重做,统计总分、正确率。\n");
printf(" * 4. 任务 4:在任务 3 基础上,随机产生 10 道四则运算题,不给机会重做,统计总分、正确率。 \n");
printf(" * 5. 任务 5:在任务 4 基础上,若回答正确率低于 75 % ,则再做 10 道题,直到正确率高于 75 % 为止。\n");
printf(" * 6. 任务 6:在任务 5 基础上,随机生成1~4间随机数,来为学生输入的每一个正确或错误的答案输出不同评价。 \n");
printf(" * 0. 退出程序\n");
printf("***************************************************************************** \n");
printf("请输入任务选项:");
}
int main()
{
int input, flag = 0;
menu();
while (~scanf("%d", &input))
{
switch (input) {
case 1:
taskOne();
break;
case 2:
taskTwo();
break;
case 3:
taskThree();
break;
case 4:
taskFour();
break;
case 5:
taskFive();
break;
case 6:
taskSix();
break;
case 0:
printf("程序退出\n");
flag = 1;
break;
default:
printf("命令错误,程序已结束\n");
break;
}
if (flag == 1)
break;
else
printf("请输入任务选项:");
}
return 0;
}
部分功能
判断两个数是否相同
int Pending(int a, int b)
{
if (a == b)
{
printf("Right!\n");
rightCount++;
}
else
{
printf("Wrong!\n");
}
return 0;
}
产生随机数函数
int Srand1()//产生1~10
{
srand((unsigned int)time(NULL));
int i;
i = rand() % 10 + 1;
return i;
}
int Srand2()//产生1~10
{
int i;
i = rand() % 10 + 1;
return i;
}
返回分数 / 计算正答率
//传入算对的题数,返回分数
int StatisticScore(int num)
{
int score;
score = num * 10;
return score;
}
//计算答题正确率
double StatisticRate(double num)
{
double rate;
rate = num / 10;
return rate;
}
任务
任务一
由于任务1要求可以重试,所以错误时提示词不一样(要求…)这里不用一开始实现的Pending()
,额外写一个
随机产生 1 - 10 之间正整数,出乘法问题,直到学生答对为止。
//任务一
//计算两个数是否相同
int PendingOne(int a, int b)
{
if (a == b)
{
printf("Right!\n");
rightCount++;
}
else
{
printf("Wrong! Please try again\n");
}
return 0;
}
void taskOne()
{
int a, b, sum, input;
do
{
a = Srand1();
b = Srand2();
sum = a * b;
printf("%d * %d = ", a, b);
scanf("%d", &input);
PendingOne(sum, input);
} while (input != sum);
printf("任务结束\n");
}
任务二
在任务 1 基础上,当学生答错时,最多给三次机会。
void taskTwo()
{
srand((unsigned)time(NULL));
int flag, count;
flag = 0;
count = 0;
double a, b, sum, input;
int c;//用于四则运算switch语句
do
{
a = Srand1();
b = Srand2();
c = rand() % 4;//c用作随机生成四则运算的标志
switch (c)
{
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
sum = a + b;
printf("%.2f+%.2f = ", a, b);
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
if (sum == input) {
flag++;
printf("Right\n");
}
else {
count++;
//最多尝试3次
if (count != 3)
printf("Wrong! Please try again.\n");
else {
printf("Wrong! You have tried three times! Test over!\n");
break;
}
}
} while (flag != 1);
flag = count = 0;
}
任务三
在任务 1 基础上,连做 10 道乘法题,不给机会重做,统计总分、正确率。
void taskThree()
{
int a, b, sum, input, flag = 0;
rightCount = 0;//回答正确个数
do {
flag++;
a = Srand1();
b = Srand2();
sum = a * b;
printf("%d * %d = ", a, b);
scanf("%d", &input);
Pending(sum, input);
} while (flag != 10);
int score = StatisticScore(rightCount);//计算总分
double rate = StatisticRate(rightCount);//计算正答率
printf("得分: %d , 正确率:%.2f\n", score, rate);
}
任务四
在任务 3 基础上,随机产生 10 道四则运算题,不给机会重做,统计总分、正确率。
void taskFour()
{
int c, parameter, sum, flag = 0;
double a, b;
int score;//分数
rightCount = 0;//正答题数
double input, rate;
do {
flag++;
a = Srand1();
b = Srand2();
c = rand() % 4;
switch (c) {
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
sum = a + c;
printf("%.2f+%.2f = ", a, b);
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
Pending(sum, input);
} while (flag != 10);
//计算并输出分数和正确率
score = StatisticScore(rightCount);
rate = StatisticRate(rightCount);
printf("得分为: %d ,正确率为: %.2f \n", score, rate);
}
任务五
在任务 4 基础上,若回答正确率低于 75 % ,则再做 10 道题,直到正确率高于 75 % 为止。
void taskFive()
{
int c, flag;
double a, b, sum, input;
rightCount = 0;
double rate = 0;
do {
flag = 0;
do {
a = Srand1();
//a = (double)a;
b = Srand2();
//b = (double)b;
c = rand() % 4;
switch (c) {
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
sum = a + b;
printf("%.2f+%.2f = ", a, b);
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
flag++;
Pending(sum, input);
} while (flag != 10);
int score = StatisticScore(rightCount);
// score = 1.0 * rightCount;
rate = StatisticRate(rightCount);
printf("分数:%d 正确率:%.2f \n\n", score, rate);
} while (rate <= 0.75);
}
任务六
在任务 5 基础上,随机生成1~4间随机数,来为学生输入的每一个正确或错误的答案输出不同评价。
void taskSix()
{
int c, flag;
double a, b, sum, input;
rightCount = 0;
double rate = 0;
do {
flag = 0;
do {
a = Srand1();
b = Srand2();
c = rand() % 4;
switch (c) {
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
printf("%.2f+%.2f = ", a, b);
sum = a + b;
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
flag++;
PendingSix(sum, input);
} while (flag != 10);
int score = StatisticScore(rightCount);
// score = 1.0 * rightCount;
rate = StatisticRate(rightCount);
printf("分数:%d 正确率:%.2f \n\n", score, rate);
} while (rate <= 0.75);
}
完整代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//定义正答题数 后面会用到
int rightCount = 0;
void menu()
{
printf("******************************************************************************\n");
printf("小学生计算机辅助系统\n");
printf(" * 1. 任务 1:随机产生 1 - 10 之间正整数,出乘法问题,直到学生答对为止。\n");
printf(" * 2. 任务 2:在任务 1 基础上,当学生答错时,最多给三次机会。 \n");
printf(" * 3. 任务 3:在任务 1 基础上,连做 10 道乘法题,不给机会重做,统计总分、正确率。\n");
printf(" * 4. 任务 4:在任务 3 基础上,随机产生 10 道四则运算题,不给机会重做,统计总分、正确率。 \n");
printf(" * 5. 任务 5:在任务 4 基础上,若回答正确率低于 75 % ,则再做 10 道题,直到正确率高于 75 % 为止。\n");
printf(" * 6. 任务 6:在任务 5 基础上,随机生成1~4间随机数,来为学生输入的每一个正确或错误的答案输出不同评价。 \n");
printf(" * 0. 退出程序\n");
printf("***************************************************************************** \n");
printf("请输入任务选项:");
}
//判断两个数是否相等
int Pending(int a, int b)
{
if (a == b)
{
printf("Right!\n");
rightCount++;
}
else
{
printf("Wrong!\n");
}
return 0;
}
//产生随机数函数
int Srand1()//产生1~10
{
srand((unsigned int)time(NULL));
int i;
i = rand() % 10 + 1;
return i;
}
int Srand2()//产生1~10
{
int i;
i = rand() % 10 + 1;
return i;
}
//传入算对的题数,返回分数
int StatisticScore(int num)
{
int score;
score = num * 10;
return score;
}
//计算答题正确率
double StatisticRate(double num)
{
double rate;
rate = num / 10;
return rate;
}
//任务一
int PendingOne(int a, int b)
{
if (a == b)
{
printf("Right!\n");
rightCount++;
}
else
{
printf("Wrong! Please try again\n");
}
return 0;
}
//任务一
void taskOne()
{
int a, b, sum, input;
do
{
a = Srand1();
b = Srand2();
sum = a * b;
printf("%d * %d = ", a, b);
scanf("%d", &input);
PendingOne(sum, input);
} while (input != sum);
printf("任务结束\n");
}
//任务二
void taskTwo()
{
srand((unsigned)time(NULL));
int flag, count;
flag = 0;
count = 0;
double a, b, sum, input;
int c;//用于四则运算switch语句
do
{
a = Srand1();
b = Srand2();
c = rand() % 4;//c用作随机生成四则运算的标志
switch (c)
{
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
sum = a + b;
printf("%.2f+%.2f = ", a, b);
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
if (sum == input) {
flag++;
printf("Right\n");
}
else {
count++;
//最多尝试3次
if (count != 3)
printf("Wrong! Please try again.\n");
else {
printf("Wrong! You have tried three times! Test over!\n");
break;
}
}
} while (flag != 1);
flag = count = 0;
}
//任务三
void taskThree()
{
int a, b, sum, input, flag = 0;
rightCount = 0;//回答正确个数
do {
flag++;
a = Srand1();
b = Srand2();
sum = a * b;
printf("%d * %d = ", a, b);
scanf("%d", &input);
Pending(sum, input);
} while (flag != 10);
int score = StatisticScore(rightCount);//计算总分
double rate = StatisticRate(rightCount);//计算正答率
printf("得分: %d , 正确率:%.2f\n", score, rate);
}
//任务四
void taskFour()
{
int c, parameter, sum, flag = 0;
double a, b;
int score;//分数
rightCount = 0;//正答题数
double input, rate;
do {
flag++;
a = Srand1();
b = Srand2();
c = rand() % 4;
switch (c) {
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
sum = a + c;
printf("%.2f+%.2f = ", a, b);
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
Pending(sum, input);
} while (flag != 10);
//计算并输出分数和正确率
score = StatisticScore(rightCount);
rate = StatisticRate(rightCount);
printf("得分为: %d ,正确率为: %.2f \n", score, rate);
}
//任务五
void taskFive()
{
int c, flag;
double a, b, sum, input;
rightCount = 0;
double rate = 0;
do {
flag = 0;
do {
a = Srand1();
//a = (double)a;
b = Srand2();
//b = (double)b;
c = rand() % 4;
switch (c) {
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
sum = a + b;
printf("%.2f+%.2f = ", a, b);
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
flag++;
Pending(sum, input);
} while (flag != 10);
int score = StatisticScore(rightCount);
// score = 1.0 * rightCount;
rate = StatisticRate(rightCount);
printf("分数:%d 正确率:%.2f \n\n", score, rate);
} while (rate <= 0.75);
}
//任务六所使用的判断函数
int PendingSix(int a, int b)
{
int c = rand() % 4;
if (a == b)
{
switch (c)
{
case 0:
printf("Very good!\n");
break;
case 1:
printf("Excellent!\n");
break;
case 2:
printf("Nice work!\n");
break;
default:
printf("Keep up the good work!\n");
break;
}
rightCount++;
}
else
{
switch (c)
{
case 0:
printf("No. Please try again.\n");
break;
case 1:
printf("Wrong. Try once more.\n");
break;
case 2:
printf("Don't give up.\n");
break;
default:
printf("Not correct. Keep trying\n");
break;
}
}
return 0;
}
//任务六
void taskSix()
{
int c, flag;
double a, b, sum, input;
rightCount = 0;
double rate = 0;
do {
flag = 0;
do {
a = Srand1();
b = Srand2();
c = rand() % 4;
switch (c) {
case 0:
printf("%.2f/%.2f = ", a, b);
sum = a / b;
break;
case 1:
printf("%.2f*%.2f = ", a, b);
sum = a * b;
break;
case 2:
printf("%.2f+%.2f = ", a, b);
sum = a + b;
break;
default:
printf("%.2f-%.2f = ", a, b);
sum = a - b;
}
scanf("%lf", &input);
flag++;
PendingSix(sum, input);
} while (flag != 10);
int score = StatisticScore(rightCount);
// score = 1.0 * rightCount;
rate = StatisticRate(rightCount);
printf("分数:%d 正确率:%.2f \n\n", score, rate);
} while (rate <= 0.75);
}
int main()
{
int input, flag;
flag = 0;
menu();
while (~scanf("%d", &input))
{
switch (input) {
case 1:
taskOne();
break;
case 2:
taskTwo();
break;
case 3:
taskThree();
break;
case 4:
taskFour();
break;
case 5:
taskFive();
break;
case 6:
taskSix();
break;
case 0:
printf("程序退出\n");
flag = 1;
break;
default:
printf("命令错误,程序已结束\n");
break;
}
if (flag == 1)
break;
else
printf("请输入任务选项:");
}
return 0;
}