当前位置:主页 > 软件编程 > C代码 >

C语言实现小学生随机出题测试计分

时间:2021-05-13 08:12:44 | 栏目:C代码 | 点击:

本文实例为大家分享了C语言实现随机出题测试计分的具体代码,供大家参考,具体内容如下

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

int test();
int checknum(int a, int b);
int checkresult(int x);

int a = -1, b, c, x;
static int k = 0, grade;

void main() {
  printf("请作答下列十道题,回车键开始开始作答!!!");
  getchar();
  do {
    int i = test();
    if (i == -1) {
      break;
    }
    else if (checkresult(i) == 0) {
      a = -1;
    }
  } while (k);
  getchar(); getchar(); getchar();
}

int test() {
  srand((unsigned int)time(NULL));
  while (checknum(a, b) == 0)
  {
    a = rand() % 51;
    b = rand() % 51;
    c = rand() % 2;
  }
  k++;
  if (k == 11) {
    printf("答题结束,您本次测试总分是 %d ,谢谢", grade);
    return -1;
  }
  else if (c == 0) {
    printf("第%d题: %d+%d=", k, a, b);
    return a + b;
  }
  else if (c == 1) {
    printf("第%d题: %d-%d=", k, a, b);
    return a - b;
  }
  return -1;
}
int checknum(int a, int b) {
  if ((a + b) > 50 || (a - b) < 0 || a > 50 || b > 50 || a < 0 || b < 0) {
    return 0;
  }
  return 1;
}
int checkresult(int result) {
  int i;
  for (i = 1; i++; i <= 3) {
    scanf_s("%d", &x);
    if (x != result) {
      if (i == 4) {
        if (c == 0) {
          printf("三次回答错误,正确答案是%d,请回答下一题\n", a + b);
        }
        else {
          printf("三次回答错误,正确答案是%d,请回答下一题\n", a - b);
        }
        return 0;
      }
      printf("回答错误,请重新计算并输入结果:");
    }
    else {
      switch (i) {
      case 2:
        grade += 10;
        break;
      case 3:
        grade += 7;
        break;
      case 4:
        grade += 5;
        break;
      default:
        grade += 0;
        break;
      }
      return 0;
    }
  }
  return 0;
}

您可能感兴趣的文章:

相关文章