计算与软件工程 作业2
作业要求<https://edu.cnblogs.com/campus/jssf/infor_computation17-31/homework/10402>
作业正文:
用C语言编写了简易程序,初步满足:
1.小学一、二、三、四、五年级的四则运算要求, 逐步实现各个年级的难度.
2.能够通过输入来选择不同年级,每个年级还得区分难,中,易三个等级
3.程序支持判断对错及累计得分.
4.一次可以出100道题目.
未能满足:
1.支持括号与多个运算符.
2.检查重复.
运行截图:
代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 int OTTest1() { int num = 0; int answer = 0; int result = 0; int m = 0; int n = 0; char op = 0; int count = N; while ( count-- ) { m = rand()%10 ; n = rand()%10 ; op = rand() % 4; switch ( op ) { case 0: op = '+'; answer = m+n; break; case 1: op = '-'; answer = m-n; break; case 2: op = '*'; answer = m*n; break; case 3: n++; op = '/'; answer = m/n; break; default: break; } int number =100-count; printf("\n第%d题:",number); printf("%d%c%d=?\n", m, op, n); printf("答案:"); scanf("%d", &result); if ( answer == result ) {printf("正确\n"); num++; } if ( answer != result ) printf("错误\n"); } printf("得分%d分",num); system("pause"); return num; } int OTTest2() { int num = 0; int answer = 0; int result = 0; int m = 0; int n = 0; char op = 0; int count = N; while ( count-- ) { m = rand()%50 ; n = rand()%50 ; op = rand() % 4; switch ( op ) { case 0: op = '+'; answer = m+n; break; case 1: op = '-'; answer = m-n; break; case 2: op = '*'; answer = m*n; break; case 3: n++; op = '/'; answer = m/n; break; default: break; } int number =100-count; printf("\n第%d题:",number); printf("%d%c%d=?\n", m, op, n); printf("答案:"); scanf("%d", &result); if ( answer == result ) { printf("正确\n"); num++;} if ( answer != result ) printf("错误\n"); } printf("得分%d分",num); system("pause"); return num; } int OTTest3() { int num = 0; int answer = 0; int result = 0; int m = 0; int n = 0; char op = 0; int count = N; while ( count-- ) { m = rand()%100 ; n = rand()%100 ; op = rand() % 4; switch ( op ) { case 0: op = '+'; answer = m+n; break; case 1: op = '-'; answer = m-n; break; case 2: op = '*'; answer = m*n; break; case 3: n++; op = '/'; answer = m/n; break; default: break; } int number =100-count; printf("\n第%d题:",number); printf("%d%c%d=?\n", m, op, n); printf("答案:"); scanf("%d", &result); if ( answer == result ){ printf("正确\n"); num++; } if ( answer != result ) printf("错误\n"); } printf("得分%d分",num); system("pause"); return num; } int main() { int grade,hard; printf("***********小学四则运算计算***********\n"); printf("\n选择年级:1,2,3,4,5\n"); scanf("%d", &grade); printf("\n选择难度:易=0,中=1,难=2\n"); scanf("%d", &hard); if (grade==1||grade==2) { switch ( hard ) { case 0: hard = '1'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest1()); return 0; break; case 1: hard = '2'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest2()); return 0; break; case 2: hard = '3'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest3()); return 0; break; default: break; } } if (grade==3) { switch ( hard ) { case 0: hard = '1'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest1()); return 0; break; case 1: hard = '2'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest2()); return 0; break; case 2: hard = '3'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest3()); return 0; break; default: break; } } if (grade==4) { switch ( hard ) { case 0: hard = '1'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest1()); return 0; break; case 1: hard = '2'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest2()); return 0; break; case 2: hard = '3'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest3()); return 0; break; default: break; } } if (grade==5) { switch ( hard ) { case 0: hard = '1'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest1()); return 0; break; case 1: hard = '2'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest2()); return 0; break; case 2: hard = '3'; srand( time(NULL) ); printf("开始做题:\n"); printf("答题结束,你总共答对%d道题目。\n", OTTest3()); return 0; break; default: break; } } }
作业2:
gitee托管链接:
https://gitee.com/hxf981221/gitee_yellow/blob/master/作业2.cpp#