小学生测验(C语言写)

项目一 小学生测验         16学时

问题描述:面向小学1~2年级学生,随机选择两个整数的加减法形成算式要求学生解答。

1.基本要求

(1)电脑随机出10道题,每题10分,程序结束时显示学生得分;

(2)确保算式没有超出1~2年级的水平,只允许进行50以内的加减法,不允许两数之和或差超出0~50的范围,负数更是不允许的;

(3)每道题学生有三次机会输入答案,当学生输入错误答案时,提醒学生重新输入,如果三次机会结束则输出正确答案;

(4)对于每道题,学生第一次输入正确答案得10分,第二次输入正确答案得7分,第三次输入正确答案得5分,否则不得分;

 (5)总成绩90以上显示“SMART”,80-90显示“GOOD”,70-80显示“OK”,60-70显示“PASS”,60以下“TRY AGAIN”
 


第一个是头文件

#include 
#include 
#include 
#define NUM 10
typedef struct
{
	int a[NUM];
	int b[NUM];
	char oper[NUM];
	int ans[NUM];
}StuTest;
StuTest CreateStuTest();
int add(int a,int b);
int sub(int a,int b);
void PutAndJudge(StuTest S);

第二个是实现文件

#include "StudentTest.h"
StuTest CreateStuTest(){
		StuTest S;
		srand((unsigned)time(NULL));
		for(int i=0;i<10;i++){
			S.oper[i] = (rand()%2)?'+':'-';		
			do{
				S.a[i] = rand()%50;
				S.b[i] = rand()%50;
				switch(S.oper[i]){
					case '+':S.ans[i] = add(S.a[i],S.b[i]);break;
					case '-':S.ans[i] = sub(S.a[i], S.b[i]); break;
				}
			}while(S.ans[i]>50||S.ans[i]<0);
		}
		return S;
}

int add(int a,int b){
		return a + b;
}

int sub(int a, int b){
		return a - b;
}

void PutAndJudge(StuTest S){
		int ans,count = 0, score = 0;
		for (int i = 0; i < 10; i++){
			count = 0;
			do{
				printf("%d %c %d = ", S.a[i], S.oper[i], S.b[i]);
				scanf("%d", &ans);
				if (ans == S.ans[i]){
					switch (count){
						case 0: score += 10; break;
						case 1: score += 7; break;
						case 2: score += 5; break;
					}
					printf("恭喜你回答正确,目前的得分 %d分\n\n",score);
					break;
				}
				else {
					count++;
					if (count<3) printf("答案错误,请重新输入!你还有%d次机会\n\n",3-count);
					else printf("答案错误,本题正确答案是 %d\n将会继续下一题\n\n",S.ans[i]);
				}
			} while (count < 3);
		}
		printf("-------------------------------\n");
		printf("您本次测试的总分是 %d 分", score);
		if (score >= 90) printf("  S M A R T  \n");
		else if (score < 90 && score >= 80)printf("  G O O D  \n");
		else if (score < 80 && score >= 70)printf("  O K  \n");
		else if (score < 70 && score >= 60)printf("  P A S S  \n");
		else printf("  TRY AGAIN  \n");
}

第三个是主函数

#include "StudentTest.h"
int main(){
		printf("小学生测验系统\n");
		printf("-------------------------------\n");
		StuTest S = CreateStuTest();
		PutAndJudge(S);
		system("pause"); 
}

补上一个单文件的代码(以下代码可直接运行)

#include 
#include 
#include 
#define NUM 10
typedef struct
{
	int a[NUM];
	int b[NUM];
	char oper[NUM];
	int ans[NUM];
}StuTest;

StuTest CreateStuTest();
int add(int a,int b);
int sub(int a,int b);
void PutAndJudge(StuTest S);

StuTest CreateStuTest(){
		StuTest S;
		srand((unsigned)time(NULL));
		for(int i=0;i<10;i++){
			S.oper[i] = (rand()%2)?'+':'-';		
			do{
				S.a[i] = rand()%50;
				S.b[i] = rand()%50;
				switch(S.oper[i]){
					case '+':S.ans[i] = add(S.a[i],S.b[i]);break;
					case '-':S.ans[i] = sub(S.a[i], S.b[i]); break;
				}
			}while(S.ans[i]>50||S.ans[i]<0);
		}
		return S;
}
 
int add(int a,int b){
		return a + b;
}
 
int sub(int a, int b){
		return a - b;
}
 
void PutAndJudge(StuTest S){
		int ans,count = 0, score = 0;
		for (int i = 0; i < 10; i++){
			count = 0;
			do{
				printf("%d %c %d = ", S.a[i], S.oper[i], S.b[i]);
				scanf("%d", &ans);
				if (ans == S.ans[i]){
					switch (count){
						case 0: score += 10; break;
						case 1: score += 7; break;
						case 2: score += 5; break;
					}
					printf("恭喜你回答正确,目前的得分 %d分\n\n",score);
					break;
				}
				else {
					count++;
					if (count<3) printf("答案错误,请重新输入!你还有%d次机会\n\n",3-count);
					else printf("答案错误,本题正确答案是 %d\n将会继续下一题\n\n",S.ans[i]);
				}
			} while (count < 3);
		}
		printf("-------------------------------\n");
		printf("您本次测试的总分是 %d 分", score);
		if (score >= 90) printf("  S M A R T  \n");
		else if (score < 90 && score >= 80)printf("  G O O D  \n");
		else if (score < 80 && score >= 70)printf("  O K  \n");
		else if (score < 70 && score >= 60)printf("  P A S S  \n");
		else printf("  TRY AGAIN  \n");
}

int main(){
		printf("小学生测验系统\n");
		printf("-------------------------------\n");
		StuTest S = CreateStuTest();
		PutAndJudge(S);
		system("pause"); 
}

附上源代码下载链接

https://download.csdn.net/download/qq_26154945/10903609

你可能感兴趣的:(小学生测验(C语言写))