| 博客班级 | https://edu.cnblogs.com/campus/ahgc/AHPU-SE-19/homework/11376 |
| 作业要求 | https://edu.cnblogs.com/campus/ahgc/AHPU-SE-19/homework/11376 |
| 作业目标 | 编写出一个能自动生成小学四则运算题目的程序 |
| 学号 | 3190704212|
代码
include <stdio.h>
include <ctype.h>
include <time.h>
include <stdlib.h>
include <windows.h>
define IDNO_LENGTH 6
define CALC_KIND 4
char getSignal();
int getResult(int,int,char);
void takeTest();
void falsePrint(int);
void correctPrint(int);
//main函数
void main()
{
while(1)
{
takeTest();
}
}
//获取随机运算符
char getSignal()
{
char signal[CALC_KIND]={'+','-','*','/'};
srand((unsigned)time(NULL));
return signal[rand()%4];
}
//获取随机数
int random(double start, double end)
{
return (int)(start+(end-start)*rand()/(RAND_MAX+ 1.0));
}
//运算
int getResult(int num1,int num2,char signal)
{
int res;
switch(signal)
{
case '+':
res=num1+num2;break;
case '-':
res=num1-num2;break;
case '*':
res=num1*num2;break;
case '/':
res=num1/num2;break;
default:
res=0;
printf("运算符不符合规则
");
}
return res;
}
//列出题目
void takeTest()
{
int giveRes;
int num1;
int num2;
char signal;
int printFlag;
srand((unsigned)time(NULL));
signal=getSignal();
num1=random(0,100);
num2=random(1,100);
if(signal=='-')
{
if(num1<num2)
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
}
if(signal=='/')
{
if(num2==0)
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
}
printf("%d%c%d=",num1,signal,num2);
scanf("%d",&giveRes);
fflush(stdin);
if(getResult(num1,num2,signal)==giveRes)
{
printFlag=random(1,5);
correctPrint(printFlag);
}
else
{
printFlag=random(1,4);
falsePrint(printFlag);
printf("正确答案:%d
",getResult(num1,num2,signal));
}
}
//回答正确打印信息
void correctPrint(int num)
{
switch(num)
{
case 1:
printf("You're right!
");break;
case 2:
printf("It's OK!
");break;
case 3:
printf("Correct!
");break;
case 4:
printf("Perfect!
");break;
case 5:
printf("Good!
");break;
default:
printf("Powerful!
");break;
}
}
//回答错误打印信息
void falsePrint(int num)
{
switch(num)
{
case 1:
printf("You're lost!");break;
case 2:
printf("Failure!");break;
case 3:
printf("Come on!");break;
case 4:
printf("It's wrong!");break;
default:
printf("Incorrect!");break;
}
}
运行图片
]()
psp2.1 | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
---|---|---|---|
Planning | 计划 | 10 | 8 |
Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 10 | 10 |
Development | 开发 | 120 | 110 |
Analysis | 需求分析(包括学习新技术) | 15 | 10 |
Design Spec | 生成设计文档 | 5 | 5 |
Design Review | 设计复审 | 5 | 5 |
Coding Standard | 代码规范 | 3 | 2 |
Design | 具体设计 | 10 | 15 |
Coding | 具体编码 | 30 | 25 |
Code Review | 代码复审 | 5 | 7 |
Test | 测试(自我测试,修改代码,提交修改) | 10 | 15 |
Reporting | 报告 | 9 | 6 |
Test Report | 测试报告 | 3 | 2 |
Size Measurement | 计算工作量 | 2 | 1 |
Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 3 | 3 |