本文实例为大家分享了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;}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。
新闻热点
疑难解答
图片精选