C language notes: 10.00 random number four arithmetic test system

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42124234/article/details/101521737

Problem scenarios and implementation code

Description:
write a program, a randomly addition (or subtraction, or multiplication, or division) is an integer between 1 and 10 students answer a few questions to
the question number (int n), and operators (char op) species it is determined by the input (a branch) for each question if the student answers correctly to display ok, carried out under a question, (branch b) If the answer is incorrect, the display no, return to this question redo, redo this question number up to 3 times, if the three did not answer, the answer is displayed, continue to the next problem

/*
编写一个程序,能随机出几道1~10之间的整数的加法(或减法,或乘法,或除法)题给小学生作答
出题数(int n), 和运算符(char op)种类 由输入决定,(分支一)对于每道题如果学生给的答案正确,显示ok,
进行下一道题, (分支二)如果答案不正确,则显示no,返回重做此题,重做此题的次数最多3次,如果3次均没有
答对,则显示答案,继续下一道题 
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Cal(int a,char op,int b);//返回计算正确答案的函数 
int main(void)
{
 int n,k;//n为答题数量,k为每次答错题的限制 
 char op;
 int ans,s_ans;//ans存放正确答案,s_ans存放学生输入的答案 
    printf("请输入运算符(+,-,*,/):\n");
    scanf("%c",&op);
    printf("请输入题目数:\n");
    scanf("%d",&n);
    srand(time(NULL));//用机器时间设置随机种子
    int id=1;//题目序号 
 while(n--)
 {
  int a=rand()%10+1;
  int b=rand()%10+1;
  int ans=Cal(a,op,b);
  k=3;
  while(k--)
  {
   printf("题目%d:%d %c %d = ",id,a,op,b);//显示题目 
   scanf("%d",&s_ans);
   if(ans==s_ans)
   {
    break;//答案正确退出本次答题, 进入下一次答题 
   }
   else
   {
    printf("答案错误!还有%d次答题机会!\n",k);
   }
  }
  id++;
  if(ans!=s_ans)
  {
   printf("答题机会用完,显示答案:%d %c %d = %d\n\n",a,op,b,ans);
  }
  else
  {
   printf("回答正确!\n\n");
  }
 } 
 return 0;
}
int Cal(int a,char op,int b)
{
 switch(op)
 {
  case '+':
   return a+b;
   break;
  case '-':
   return a-b;
   break;
  case '*':
   return a*b;
   break;
  case '/':
   return a/b;
   break;
 }
}

The result:
Here Insert Picture Description
the realization of ideas:
the operator inputted by the user, randomly generated operands, it is possible to use the rand stdlib.h () produces an integer between 0 to 32767, then use the remainder thought, rand ()% 10 + 1 can be mapped to the generated random number between 1 and 10, in order to prevent the random number generated each time have to repeat the same portions, and then used in the time.h header file srand (time (NULL)) function (Time () ) setting a random seed (random seed with the current time of the machine: i.e., to the seeds as an initialization condition, then some algorithms have been generated by iteration a new random number, since the changing time, and then with complex algorithms, random number generated same probability is very low), then the custom function Cal () calculates and returns the correct result.

To generate a random number in the specified range (assuming [a, b]) there is an algorithm:
RAND ()% (A-B +. 1) + A

Improvement and Implementation Code

The code implements two operands randomly generated, but the arithmetic operators not achieved randomly generated, then how to achieve it?
First, there are four operators, we may specify +, -, *, / 4 corresponding number between 0 and 3, and then using a random function generated integer between [0,3], then the switch case statement makes a character type of mapping, so we customized a random number generation function char operation Rand_Op (void), instead of the need for the operator user input line of source code.

/*
编写一个程序,能随机出几道1~10之间的整数的加法(或减法,或乘法,或除法)题给小学生作答
出题数(int n)由输入决定,题目操作数和运算符随机产生,(分支一)对于每道题如果学生给的答案正确,显示ok,
进行下一道题, (分支二)如果答案不正确,则显示no,返回重做此题,重做此题的次数最多3次,如果3次均没有
答对,则显示答案,继续下一道题 
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Cal(int a,char op,int b);//返回计算正确答案的函数 
char Rand_Op(void);
int main(void)
{
 int n,k;//n为答题数量,k为每次答错题的限制 
 int ans,s_ans;//ans存放正确答案,s_ans存放学生输入的答案 
//    printf("请输入运算符(+,-,*,/):\n");
//    scanf("%c",&op);
    printf("请输入题目数:\n");
    scanf("%d",&n);
    srand(time(NULL));//用机器时间设置随机种子
    int id=1;//题目序号 
 while(n--)
 {
  int a=rand()%10+1;
  int b=rand()%10+1;
  char op=Rand_Op();
  int ans=Cal(a,op,b);
  k=3;
  while(k--)
  {
   printf("题目%d:%d %c %d = ",id,a,op,b);//显示题目 
   scanf("%d",&s_ans);
   if(ans==s_ans)
   {
    break;//答案正确退出本次答题, 进入下一次答题 
   }
   else
   {
    printf("答案错误!还有%d次答题机会!\n",k);
   }
  }
  id++;
  if(ans!=s_ans)
  {
   printf("答题机会用完,显示答案:%d %c %d = %d\n\n",a,op,b,ans);
  }
  else
  {
   printf("回答正确!\n\n");
  }
 } 
 return 0;
}
int Cal(int a,char op,int b)
{
 switch(op)
 {
  case '+':
   return a+b;
   break;
  case '-':
   return a-b;
   break;
  case '*':
   return a*b;
   break;
  case '/':
   return a/b;
   break;
 }
}
char Rand_Op(void)
{
 int n;
 srand(time(NULL));//用系统当前时间设置rand()随机序列种子,保证每次运行随机序列不一样 
 n=rand()%4;//随机产生【0~3】之间的值,从小到大分别表示加减乘除 
// printf("%d\n",n);
 switch(n)//做一个运算符映射 
 {
  case 0:
   return '+';
  case 1:
   return '-';
  case 2:
   return '*';
  case 3:
   return '/';
 }
}

operation result:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_42124234/article/details/101521737